Date.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  23. The Date class provides a basic structure for date and time related
  24. information. Date instances can be created by
  25. - `new Date()` for a specific date,
  26. - `Date.now()` to obtain information about the current time,
  27. - `Date.fromTime()` with a given timestamp or
  28. - `Date.fromString()` by parsing from a String.
  29. There are some extra functions available in the `DateTools` class.
  30. In the context of Haxe dates, a timestamp is defined as the number of
  31. milliseconds elapsed since 1st January 1970.
  32. **/
  33. extern class Date {
  34. /**
  35. Creates a new date object from the given arguments.
  36. The behaviour of a Date instance is only consistent across platforms if
  37. the the arguments describe a valid date.
  38. - month: 0 to 11
  39. - day: 1 to 31
  40. - hour: 0 to 23
  41. - min: 0 to 59
  42. - sec: 0 to 59
  43. **/
  44. function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void;
  45. /**
  46. Returns the timestamp (in milliseconds) of the date. It might
  47. only have a per-second precision depending on the platforms.
  48. For measuring time differences with millisecond accuracy on
  49. all platforms, see `haxe.Timer.stamp`.
  50. **/
  51. function getTime():Float;
  52. /**
  53. Returns the hours of `this` Date (0-23 range).
  54. **/
  55. function getHours():Int;
  56. /**
  57. Returns the minutes of `this` Date (0-59 range).
  58. **/
  59. function getMinutes():Int;
  60. /**
  61. Returns the seconds of `this` Date (0-59 range).
  62. **/
  63. function getSeconds():Int;
  64. /**
  65. Returns the full year of `this` Date (4-digits).
  66. **/
  67. function getFullYear():Int;
  68. /**
  69. Returns the month of `this` Date (0-11 range).
  70. **/
  71. function getMonth():Int;
  72. /**
  73. Returns the day of `this` Date (1-31 range).
  74. **/
  75. function getDate():Int;
  76. /**
  77. Returns the day of the week of `this` Date (0-6 range) where `0` is Sunday.
  78. **/
  79. function getDay():Int;
  80. /**
  81. Returns a string representation of `this` Date, by using the
  82. standard format [YYYY-MM-DD HH:MM:SS]. See `DateTools.format` for
  83. other formating rules.
  84. **/
  85. function toString():String;
  86. /**
  87. Returns a Date representing the current local time.
  88. **/
  89. static function now():Date;
  90. /**
  91. Returns a Date from timestamp (in milliseconds) `t`.
  92. **/
  93. static function fromTime(t:Float):Date;
  94. /**
  95. Returns a Date from a formatted string `s`, with the following accepted
  96. formats:
  97. - `"YYYY-MM-DD hh:mm:ss"`
  98. - `"YYYY-MM-DD"`
  99. - `"hh:mm:ss"`
  100. The first two formats are expressed in local time, the third in UTC
  101. Epoch.
  102. **/
  103. static function fromString(s:String):Date;
  104. #if flash
  105. private static function __init__():Void
  106. untyped {
  107. var d:Dynamic = Date;
  108. d.now = function() {
  109. return __new__(Date);
  110. };
  111. d.fromTime = function(t) {
  112. var d:Date = __new__(Date);
  113. d.setTime(t);
  114. return d;
  115. };
  116. d.fromString = function(s:String) {
  117. switch (s.length) {
  118. case 8: // hh:mm:ss
  119. var k = s.split(":");
  120. var d:Date = __new__(Date);
  121. d.setTime(0);
  122. d.setUTCHours(k[0]);
  123. d.setUTCMinutes(k[1]);
  124. d.setUTCSeconds(k[2]);
  125. return d;
  126. case 10: // YYYY-MM-DD
  127. var k = s.split("-");
  128. return new Date(cast k[0], cast k[1] - 1, cast k[2], 0, 0, 0);
  129. case 19: // YYYY-MM-DD hh:mm:ss
  130. var k = s.split(" ");
  131. var y = k[0].split("-");
  132. var t = k[1].split(":");
  133. return new Date(cast y[0], cast y[1] - 1, cast y[2], cast t[0], cast t[1], cast t[2]);
  134. default:
  135. throw "Invalid date format : " + s;
  136. }
  137. };
  138. d.prototype[#if (as3 || no_flash_override) "toStringHX" #else "toString" #end] = function() {
  139. var date:Date = __this__;
  140. var m = date.getMonth() + 1;
  141. var d = date.getDate();
  142. var h = date.getHours();
  143. var mi = date.getMinutes();
  144. var s = date.getSeconds();
  145. return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  146. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  147. };
  148. }
  149. #end
  150. }