Date.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C)2005-2018 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. /**
  36. Creates a new date object from the given arguments.
  37. The behaviour of a Date instance is only consistent across platforms if
  38. the the arguments describe a valid date.
  39. - month: 0 to 11
  40. - day: 1 to 31
  41. - hour: 0 to 23
  42. - min: 0 to 59
  43. - sec: 0 to 59
  44. **/
  45. function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
  46. /**
  47. Returns the timestamp (in milliseconds) of the date. It might
  48. only have a per-second precision depending on the platforms.
  49. For measuring time differences with millisecond accuracy on
  50. all platforms, see `haxe.Timer.stamp`.
  51. **/
  52. function getTime() : Float;
  53. /**
  54. Returns the hours of `this` Date (0-23 range).
  55. **/
  56. function getHours() : Int;
  57. /**
  58. Returns the minutes of `this` Date (0-59 range).
  59. **/
  60. function getMinutes() : Int;
  61. /**
  62. Returns the seconds of `this` Date (0-59 range).
  63. **/
  64. function getSeconds() : Int;
  65. /**
  66. Returns the full year of `this` Date (4-digits).
  67. **/
  68. function getFullYear() : Int;
  69. /**
  70. Returns the month of `this` Date (0-11 range).
  71. **/
  72. function getMonth() : Int;
  73. /**
  74. Returns the day of `this` Date (1-31 range).
  75. **/
  76. function getDate() : Int;
  77. /**
  78. Returns the day of the week of `this` Date (0-6 range) where `0` is Sunday.
  79. **/
  80. function getDay() : Int;
  81. /**
  82. Returns a string representation of `this` Date, by using the
  83. standard format [YYYY-MM-DD HH:MM:SS]. See `DateTools.format` for
  84. other formating rules.
  85. **/
  86. function toString():String;
  87. /**
  88. Returns a Date representing the current local time.
  89. **/
  90. static function now() : Date;
  91. /**
  92. Returns a Date from timestamp (in milliseconds) `t`.
  93. **/
  94. static function fromTime( t : Float ) : Date;
  95. /**
  96. Returns a Date from a formatted string `s`, with the following accepted
  97. formats:
  98. - `"YYYY-MM-DD hh:mm:ss"`
  99. - `"YYYY-MM-DD"`
  100. - `"hh:mm:ss"`
  101. The first two formats are expressed in local time, the third in UTC
  102. Epoch.
  103. **/
  104. static function fromString( s : String ) : Date;
  105. #if flash
  106. private static function __init__() : Void 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()
  146. +"-"+(if( m < 10 ) "0"+m else ""+m)
  147. +"-"+(if( d < 10 ) "0"+d else ""+d)
  148. +" "+(if( h < 10 ) "0"+h else ""+h)
  149. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  150. +":"+(if( s < 10 ) "0"+s else ""+s);
  151. };
  152. }
  153. #end
  154. }