Date.hx 3.6 KB

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