2
0

Date.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package;
  23. import haxe.Int64;
  24. import java.util.Calendar;
  25. import java.util.GregorianCalendar;
  26. import java.util.TimeZone;
  27. @:coreApi class Date {
  28. private var date:Calendar;
  29. private var dateUTC:Calendar;
  30. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  31. date = new GregorianCalendar(year, month, day, hour, min, sec);
  32. dateUTC = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
  33. dateUTC.setTimeInMillis(date.getTimeInMillis());
  34. }
  35. public inline function getTime():Float {
  36. return cast date.getTimeInMillis();
  37. }
  38. public inline function getHours():Int {
  39. return date.get(Calendar.HOUR_OF_DAY);
  40. }
  41. public inline function getMinutes():Int {
  42. return date.get(Calendar.MINUTE);
  43. }
  44. public inline function getSeconds():Int {
  45. return date.get(Calendar.SECOND);
  46. }
  47. public inline function getFullYear():Int {
  48. return date.get(Calendar.YEAR);
  49. }
  50. public inline function getMonth():Int {
  51. return date.get(Calendar.MONTH);
  52. }
  53. public inline function getDate():Int {
  54. return date.get(Calendar.DAY_OF_MONTH);
  55. }
  56. public inline function getDay():Int {
  57. // SUNDAY in Java == 1, MONDAY == 2, ...
  58. return cast date.get(Calendar.DAY_OF_WEEK) - 1;
  59. }
  60. public inline function getUTCHours():Int {
  61. return dateUTC.get(Calendar.HOUR_OF_DAY);
  62. }
  63. public inline function getUTCMinutes():Int {
  64. return dateUTC.get(Calendar.MINUTE);
  65. }
  66. public inline function getUTCSeconds():Int {
  67. return dateUTC.get(Calendar.SECOND);
  68. }
  69. public inline function getUTCFullYear():Int {
  70. return dateUTC.get(Calendar.YEAR);
  71. }
  72. public inline function getUTCMonth():Int {
  73. return dateUTC.get(Calendar.MONTH);
  74. }
  75. public inline function getUTCDate():Int {
  76. return dateUTC.get(Calendar.DAY_OF_MONTH);
  77. }
  78. public inline function getUTCDay():Int {
  79. // SUNDAY in Java == 1, MONDAY == 2, ...
  80. return cast dateUTC.get(Calendar.DAY_OF_WEEK) - 1;
  81. }
  82. public inline function getTimezoneOffset():Int {
  83. return -Std.int(date.get(Calendar.ZONE_OFFSET) / 60000);
  84. }
  85. public function toString():String {
  86. var m = getMonth() + 1;
  87. var d = getDate();
  88. var h = getHours();
  89. var mi = getMinutes();
  90. var s = getSeconds();
  91. return getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  92. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  93. }
  94. static public function now():Date {
  95. var d = new Date(0, 0, 0, 0, 0, 0);
  96. d.date = Calendar.getInstance();
  97. d.dateUTC.setTimeInMillis(d.date.getTimeInMillis());
  98. return d;
  99. }
  100. static public function fromTime(t:Float):Date {
  101. var d = new Date(0, 0, 0, 0, 0, 0);
  102. d.date.setTimeInMillis(cast t);
  103. d.dateUTC.setTimeInMillis(cast t);
  104. return d;
  105. }
  106. static public function fromString(s:String):Date {
  107. switch (s.length) {
  108. case 8: // hh:mm:ss
  109. var k = s.split(":");
  110. return Date.fromTime(Std.parseInt(k[0]) * 3600000. + Std.parseInt(k[1]) * 60000. + Std.parseInt(k[2]) * 1000.);
  111. case 10: // YYYY-MM-DD
  112. var k = s.split("-");
  113. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  114. case 19: // YYYY-MM-DD hh:mm:ss
  115. var k = s.split(" ");
  116. var y = k[0].split("-");
  117. var t = k[1].split(":");
  118. return new Date(Std.parseInt(y[0]), Std.parseInt(y[1]) - 1, Std.parseInt(y[2]), Std.parseInt(t[0]), Std.parseInt(t[1]), Std.parseInt(t[2]));
  119. default:
  120. throw "Invalid date format : " + s;
  121. }
  122. }
  123. }