/bin/std/Date.hx

http://github.com/Yoomee/clippy · Haxe · 192 lines · 97 code · 18 blank · 77 comment · 6 complexity · ee4456d4f5ca84a70f2e534367902494 MD5 · raw file

  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. #if (cpp && !xmldoc)
  30. typedef Date = cpp.CppDate__;
  31. #else
  32. extern class Date
  33. {
  34. /**
  35. Creates a new date object.
  36. **/
  37. function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void;
  38. /**
  39. Returns the timestamp of the date. It's the number of milliseconds
  40. elapsed since 1st January 1970. It might only have a per-second precision
  41. depending on the platforms.
  42. **/
  43. function getTime() : Float;
  44. /**
  45. Returns the hours value of the date (0-23 range).
  46. **/
  47. function getHours() : Int;
  48. /**
  49. Returns the minutes value of the date (0-59 range).
  50. **/
  51. function getMinutes() : Int;
  52. /**
  53. Returns the seconds of the date (0-59 range).
  54. **/
  55. function getSeconds() : Int;
  56. /**
  57. Returns the full year of the date.
  58. **/
  59. function getFullYear() : Int;
  60. /**
  61. Returns the month of the date (0-11 range).
  62. **/
  63. function getMonth() : Int;
  64. /**
  65. Returns the day of the date (1-31 range).
  66. **/
  67. function getDate() : Int;
  68. /**
  69. Returns the week day of the date (0-6 range).
  70. **/
  71. function getDay() : Int;
  72. /**
  73. Returns a string representation for the Date, by using the
  74. standard format [YYYY-MM-DD HH:MM:SS]. See [DateTools.format] for
  75. other formating rules.
  76. **/
  77. function toString():String;
  78. /**
  79. Returns a Date representing the current local time.
  80. **/
  81. static function now() : Date;
  82. /**
  83. Returns a Date from a timestamp [t] which is the number of
  84. milliseconds elapsed since 1st January 1970.
  85. **/
  86. static function fromTime( t : Float ) : Date;
  87. /**
  88. Returns a Date from a formated string of one of the following formats :
  89. [YYYY-MM-DD hh:mm:ss] or [YYYY-MM-DD] or [hh:mm:ss]. The first two formats
  90. are expressed in local time, the third in UTC Epoch.
  91. **/
  92. static function fromString( s : String ) : Date;
  93. #if flash_lite
  94. /** flash lite only **/
  95. function getLocaleLongDate():String;
  96. /** flash lite only **/
  97. function getLocaleShortDate():String;
  98. /** flash lite only **/
  99. function getLocaleTime():String;
  100. #end
  101. #if !php
  102. private static function __init__() : Void untyped {
  103. #if neko
  104. Date = neko.NekoDate__;
  105. neko.Boot.__classes.Date = Date;
  106. #else
  107. Date.now = function() {
  108. return __new__(Date);
  109. };
  110. Date.fromTime = function(t){
  111. var d : Date = __new__(Date);
  112. #if flash9
  113. d.setTime(t);
  114. #else
  115. d["setTime"]( t );
  116. #end
  117. return d;
  118. };
  119. Date.fromString = function(s : String) {
  120. switch( s.length ) {
  121. case 8: // hh:mm:ss
  122. var k = s.split(":");
  123. var d : Date = __new__(Date);
  124. #if flash9
  125. d.setTime(0);
  126. d.setUTCHours(k[0]);
  127. d.setUTCMinutes(k[1]);
  128. d.setUTCSeconds(k[2]);
  129. #else
  130. d["setTime"](0);
  131. d["setUTCHours"](k[0]);
  132. d["setUTCMinutes"](k[1]);
  133. d["setUTCSeconds"](k[2]);
  134. #end
  135. return d;
  136. case 10: // YYYY-MM-DD
  137. var k = s.split("-");
  138. return new Date(cast k[0],cast k[1] - 1,cast k[2],0,0,0);
  139. case 19: // YYYY-MM-DD hh:mm:ss
  140. var k = s.split(" ");
  141. var y = k[0].split("-");
  142. var t = k[1].split(":");
  143. return new Date(cast y[0],cast y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  144. default:
  145. throw "Invalid date format : " + s;
  146. }
  147. };
  148. Date.prototype["toString"] = function() {
  149. var date : Date = this;
  150. var m = date.getMonth() + 1;
  151. var d = date.getDate();
  152. var h = date.getHours();
  153. var mi = date.getMinutes();
  154. var s = date.getSeconds();
  155. return date.getFullYear()
  156. +"-"+(if( m < 10 ) "0"+m else ""+m)
  157. +"-"+(if( d < 10 ) "0"+d else ""+d)
  158. +" "+(if( h < 10 ) "0"+h else ""+h)
  159. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  160. +":"+(if( s < 10 ) "0"+s else ""+s);
  161. };
  162. #if flash9
  163. #elseif flash
  164. Date.prototype[__unprotect__("__class__")] = Date;
  165. Date[__unprotect__("__name__")] = ["Date"];
  166. #else
  167. Date.prototype.__class__ = Date;
  168. Date.__name__ = ["Date"];
  169. #end
  170. #end
  171. }
  172. #end
  173. }
  174. #end // !cpp