Date.hx 3.7 KB

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