Date.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C)2005-2012 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. package;
  23. import cs.system.DateTime;
  24. import haxe.Int64;
  25. @:coreApi class Date
  26. {
  27. private var date:DateTime;
  28. /**
  29. Creates a new date object.
  30. **/
  31. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
  32. {
  33. if (day <= 0) day = 1;
  34. if (year <= 0) year = 1;
  35. date = new DateTime(year, month + 1, day, hour, min, sec);
  36. }
  37. /**
  38. Returns the timestamp of the date. It's the number of milliseconds
  39. elapsed since 1st January 1970. It might only have a per-second precision
  40. depending on the platforms.
  41. **/
  42. public inline function getTime() : Float
  43. {
  44. return (cast(date.Ticks, Float) / TimeSpan.TicksPerMillisecond);
  45. }
  46. /**
  47. Returns the hours value of the date (0-23 range).
  48. **/
  49. public inline function getHours() : Int
  50. {
  51. return date.Hour;
  52. }
  53. /**
  54. Returns the minutes value of the date (0-59 range).
  55. **/
  56. public inline function getMinutes() : Int
  57. {
  58. return date.Minute;
  59. }
  60. /**
  61. Returns the seconds of the date (0-59 range).
  62. **/
  63. public inline function getSeconds() : Int
  64. {
  65. return date.Second;
  66. }
  67. /**
  68. Returns the full year of the date.
  69. **/
  70. public inline function getFullYear() : Int
  71. {
  72. return date.Year;
  73. }
  74. /**
  75. Returns the month of the date (0-11 range).
  76. **/
  77. public inline function getMonth() : Int
  78. {
  79. return date.Month - 1;
  80. }
  81. /**
  82. Returns the day of the date (1-31 range).
  83. **/
  84. public inline function getDate() : Int
  85. {
  86. return date.Day;
  87. }
  88. /**
  89. Returns the week day of the date (0-6 range).
  90. **/
  91. public function getDay() : Int
  92. {
  93. var ret = cast(date.DayOfWeek, Int) - 1;
  94. if (ret == -1)
  95. ret = 6;
  96. return ret;
  97. }
  98. /**
  99. Returns a string representation for the Date, by using the
  100. standard format [YYYY-MM-DD HH:MM:SS]. See [DateTools.format] for
  101. other formating rules.
  102. **/
  103. public function toString():String
  104. {
  105. var m = getMonth() + 1;
  106. var d = getDate();
  107. var h = getHours();
  108. var mi = getMinutes();
  109. var s = getSeconds();
  110. return (getFullYear())
  111. +"-"+(if( m < 10 ) "0"+m else ""+m)
  112. +"-"+(if( d < 10 ) "0"+d else ""+d)
  113. +" "+(if( h < 10 ) "0"+h else ""+h)
  114. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  115. +":"+(if( s < 10 ) "0"+s else ""+s);
  116. }
  117. /**
  118. Returns a Date representing the current local time.
  119. **/
  120. static public function now() : Date
  121. {
  122. var d = new Date(0, 0, 0, 0, 0, 0);
  123. d.date = DateTime.Now;
  124. return d;
  125. }
  126. /**
  127. Returns a Date from a timestamp [t] which is the number of
  128. milliseconds elapsed since 1st January 1970.
  129. **/
  130. static public function fromTime( t : Float ) : Date
  131. {
  132. var d = new Date(0, 0, 0, 0, 0, 0);
  133. d.date = new DateTime(cast(t, Int64));
  134. return d;
  135. }
  136. /**
  137. Returns a Date from a formated string of one of the following formats :
  138. [YYYY-MM-DD hh:mm:ss] or [YYYY-MM-DD] or [hh:mm:ss]. The first two formats
  139. are expressed in local time, the third in UTC Epoch.
  140. **/
  141. static public function fromString( s : String ) : Date
  142. {
  143. switch( s.length )
  144. {
  145. case 8: // hh:mm:ss
  146. var k = s.split(":");
  147. var d : Date = new Date(1, 1, 1, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]));
  148. return d;
  149. case 10: // YYYY-MM-DD
  150. var k = s.split("-");
  151. return new Date(Std.parseInt(k[0]),Std.parseInt(k[1]) - 1,Std.parseInt(k[2]),0,0,0);
  152. case 19: // YYYY-MM-DD hh:mm:ss
  153. var k = s.split(" ");
  154. var y = k[0].split("-");
  155. var t = k[1].split(":");
  156. return new Date(Std.parseInt(y[0]),Std.parseInt(y[1]) - 1,Std.parseInt(y[2]),Std.parseInt(t[0]),Std.parseInt(t[1]),Std.parseInt(t[2]));
  157. default:
  158. throw "Invalid date format : " + s;
  159. }
  160. }
  161. private static function fromNative( d : cs.system.DateTime ) : Date
  162. {
  163. var date = new Date(0, 0, 0, 0, 0, 0);
  164. date.date = d;
  165. return date;
  166. }
  167. }