Date.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C)2005-2017 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. **/
  50. function getTime() : Float;
  51. /**
  52. Returns the hours of `this` Date (0-23 range).
  53. **/
  54. function getHours() : Int;
  55. /**
  56. Returns the minutes of `this` Date (0-59 range).
  57. **/
  58. function getMinutes() : Int;
  59. /**
  60. Returns the seconds of `this` Date (0-59 range).
  61. **/
  62. function getSeconds() : Int;
  63. /**
  64. Returns the full year of `this` Date (4-digits).
  65. **/
  66. function getFullYear() : Int;
  67. /**
  68. Returns the month of `this` Date (0-11 range).
  69. **/
  70. function getMonth() : Int;
  71. /**
  72. Returns the day of `this` Date (1-31 range).
  73. **/
  74. function getDate() : Int;
  75. /**
  76. Returns the day of the week of `this` Date (0-6 range) where `0` is Sunday.
  77. **/
  78. function getDay() : Int;
  79. /**
  80. Returns a string representation of `this` Date, by using the
  81. standard format [YYYY-MM-DD HH:MM:SS]. See `DateTools.format` for
  82. other formating rules.
  83. **/
  84. function toString():String;
  85. /**
  86. Returns a Date representing the current local time.
  87. **/
  88. static function now() : Date;
  89. /**
  90. Returns a Date from timestamp (in milliseconds) `t`.
  91. **/
  92. static function fromTime( t : Float ) : Date;
  93. /**
  94. Returns a Date from a formated string `s`, with the following accepted
  95. formats:
  96. - `"YYYY-MM-DD hh:mm:ss"`
  97. - `"YYYY-MM-DD"`
  98. - `"hh:mm:ss"`
  99. The first two formats are expressed in local time, the third in UTC
  100. Epoch.
  101. **/
  102. static function fromString( s : String ) : Date;
  103. #if flash
  104. private static function __init__() : Void untyped {
  105. var d : Dynamic = Date;
  106. d.now = function() {
  107. return __new__(Date);
  108. };
  109. d.fromTime = function(t){
  110. var d : Date = __new__(Date);
  111. d.setTime(t);
  112. return d;
  113. };
  114. d.fromString = function(s : String) {
  115. switch( s.length ) {
  116. case 8: // hh:mm:ss
  117. var k = s.split(":");
  118. var d : Date = __new__(Date);
  119. d.setTime(0);
  120. d.setUTCHours(k[0]);
  121. d.setUTCMinutes(k[1]);
  122. d.setUTCSeconds(k[2]);
  123. return d;
  124. case 10: // YYYY-MM-DD
  125. var k = s.split("-");
  126. return new Date(cast k[0],cast k[1] - 1,cast k[2],0,0,0);
  127. case 19: // YYYY-MM-DD hh:mm:ss
  128. var k = s.split(" ");
  129. var y = k[0].split("-");
  130. var t = k[1].split(":");
  131. return new Date(cast y[0],cast y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  132. default:
  133. throw "Invalid date format : " + s;
  134. }
  135. };
  136. d.prototype[#if (as3 || no_flash_override) "toStringHX" #else "toString" #end] = function() {
  137. var date : Date = __this__;
  138. var m = date.getMonth() + 1;
  139. var d = date.getDate();
  140. var h = date.getHours();
  141. var mi = date.getMinutes();
  142. var s = date.getSeconds();
  143. return date.getFullYear()
  144. +"-"+(if( m < 10 ) "0"+m else ""+m)
  145. +"-"+(if( d < 10 ) "0"+d else ""+d)
  146. +" "+(if( h < 10 ) "0"+h else ""+h)
  147. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  148. +":"+(if( s < 10 ) "0"+s else ""+s);
  149. };
  150. }
  151. #end
  152. }