Date.hx 3.6 KB

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