Date.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. The Date class is used for date manipulation. There is some extra functions
  27. available in the [DateTools] class.
  28. **/
  29. extern class Date
  30. {
  31. /**
  32. Creates a new date object.
  33. **/
  34. function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
  35. /**
  36. Returns the timestamp of the date. It's the number of milliseconds
  37. elapsed since 1st January 1970. It might only have a per-second precision
  38. depending on the platforms.
  39. **/
  40. function getTime() : Float;
  41. /**
  42. Returns the hours value of the date (0-23 range).
  43. **/
  44. function getHours() : Int;
  45. /**
  46. Returns the minutes value of the date (0-59 range).
  47. **/
  48. function getMinutes() : Int;
  49. /**
  50. Returns the seconds of the date (0-59 range).
  51. **/
  52. function getSeconds() : Int;
  53. /**
  54. Returns the full year of the date.
  55. **/
  56. function getFullYear() : Int;
  57. /**
  58. Returns the month of the date (0-11 range).
  59. **/
  60. function getMonth() : Int;
  61. /**
  62. Returns the day of the date (1-31 range).
  63. **/
  64. function getDate() : Int;
  65. /**
  66. Returns the week day of the date (0-6 range).
  67. **/
  68. function getDay() : Int;
  69. /**
  70. Returns a string representation for the Date, by using the
  71. standard format [YYYY-MM-DD HH:MM:SS]. See [DateTools.format] for
  72. other formating rules.
  73. **/
  74. function toString():String;
  75. /**
  76. Returns a Date representing the current local time.
  77. **/
  78. static function now() : Date;
  79. /**
  80. Returns a Date from a timestamp [t] which is the number of
  81. milliseconds elapsed since 1st January 1970.
  82. **/
  83. static function fromTime( t : Float ) : Date;
  84. /**
  85. Returns a Date from a formated string of one of the following formats :
  86. [YYYY-MM-DD hh:mm:ss] or [YYYY-MM-DD] or [hh:mm:ss]. The first two formats
  87. are expressed in local time, the third in UTC Epoch.
  88. **/
  89. static function fromString( s : String ) : Date;
  90. #if (js || flash)
  91. private static function __init__() : Void untyped {
  92. var d #if !swf_mark : Dynamic #end = Date;
  93. d.now = function() {
  94. return __new__(Date);
  95. };
  96. d.fromTime = function(t){
  97. var d : Date = __new__(Date);
  98. #if flash9
  99. d.setTime(t);
  100. #else
  101. d["setTime"]( t );
  102. #end
  103. return d;
  104. };
  105. d.fromString = function(s : String) {
  106. switch( s.length ) {
  107. case 8: // hh:mm:ss
  108. var k = s.split(":");
  109. var d : Date = __new__(Date);
  110. #if flash9
  111. d.setTime(0);
  112. d.setUTCHours(k[0]);
  113. d.setUTCMinutes(k[1]);
  114. d.setUTCSeconds(k[2]);
  115. #else
  116. d["setTime"](0);
  117. d["setUTCHours"](k[0]);
  118. d["setUTCMinutes"](k[1]);
  119. d["setUTCSeconds"](k[2]);
  120. #end
  121. return d;
  122. case 10: // YYYY-MM-DD
  123. var k = s.split("-");
  124. return new Date(cast k[0],cast k[1] - 1,cast k[2],0,0,0);
  125. case 19: // YYYY-MM-DD hh:mm:ss
  126. var k = s.split(" ");
  127. var y = k[0].split("-");
  128. var t = k[1].split(":");
  129. return new Date(cast y[0],cast y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  130. default:
  131. throw "Invalid date format : " + s;
  132. }
  133. };
  134. d.prototype[#if as3 "toStringHX" #else "toString" #end] = function() {
  135. var date : Date = __this__;
  136. var m = date.getMonth() + 1;
  137. var d = date.getDate();
  138. var h = date.getHours();
  139. var mi = date.getMinutes();
  140. var s = date.getSeconds();
  141. return date.getFullYear()
  142. +"-"+(if( m < 10 ) "0"+m else ""+m)
  143. +"-"+(if( d < 10 ) "0"+d else ""+d)
  144. +" "+(if( h < 10 ) "0"+h else ""+h)
  145. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  146. +":"+(if( s < 10 ) "0"+s else ""+s);
  147. };
  148. #if flash9
  149. #elseif flash
  150. d.prototype[__unprotect__("__class__")] = d;
  151. d[__unprotect__("__name__")] = ["Date"];
  152. #elseif js
  153. d.prototype.__class__ = $hxClasses['Date'] = d;
  154. d.__name__ = ["Date"];
  155. #end
  156. }
  157. #end
  158. }