Date.hx 3.5 KB

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