Date.hx 3.7 KB

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