2
0

Date.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package;
  23. import python.lib.datetime.DateTime;
  24. import python.lib.datetime.TimeDelta;
  25. import python.Syntax;
  26. @:coreApi class Date
  27. {
  28. // since January 1, 1970, 00:00:00 GMT
  29. //static var BASE = new DateTime(1970, 1, 1);
  30. private var date:DateTime;
  31. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
  32. {
  33. if (year < DateTime.min.year) year = DateTime.min.year;
  34. if (day == 0) day = 1;
  35. date = new DateTime(year, month+1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc);
  36. }
  37. public inline function getTime() : Float
  38. {
  39. return datetimeTimestamp(date);
  40. }
  41. public inline function getHours() : Int
  42. {
  43. return date.hour;
  44. }
  45. public inline function getMinutes() : Int
  46. {
  47. return date.minute;
  48. }
  49. public inline function getSeconds() : Int
  50. {
  51. return date.second;
  52. }
  53. public inline function getFullYear() : Int
  54. {
  55. return date.year;
  56. }
  57. public inline function getMonth() : Int
  58. {
  59. return date.month-1;
  60. }
  61. public inline function getDate() : Int
  62. {
  63. return date.day;
  64. }
  65. public inline function getDay() : Int
  66. {
  67. return date.isoweekday();
  68. }
  69. public function toString():String
  70. {
  71. inline function st (x) return Std.string(x);
  72. var m = getMonth()+1;
  73. var d = getDate();
  74. var h = getHours();
  75. var mi = getMinutes();
  76. var s = getSeconds();
  77. return st(getFullYear())
  78. +"-"+(if( m < 10 ) "0"+st(m) else ""+st(m))
  79. +"-"+(if( d < 10 ) "0"+st(d) else ""+st(d))
  80. +" "+(if( h < 10 ) "0"+st(h) else ""+st(h))
  81. +":"+(if( mi < 10 ) "0"+st(mi) else ""+st(mi))
  82. +":"+(if( s < 10 ) "0"+st(s) else ""+st(s));
  83. }
  84. static public function now() : Date
  85. {
  86. var d = new Date(1970, 0, 1, 0, 0, 0);
  87. d.date = DateTime.now();
  88. return d;
  89. }
  90. static public function fromTime( t : Float ) : Date
  91. {
  92. var d = new Date(1970, 0, 1, 0, 0, 0);
  93. d.date = DateTime.fromtimestamp(t/1000.0, python.lib.datetime.Timezone.utc);
  94. return d;
  95. }
  96. static function UTC( year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Float
  97. {
  98. var dt = new DateTime(year, month+1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc);
  99. return datetimeTimestamp(dt);
  100. }
  101. static function datetimeTimestamp(dt:DateTime):Float {
  102. var dt2 = new DateTime(1970, 1, 1, 0, 0, 0, 0, python.lib.datetime.Timezone.utc);
  103. var timedelta = new TimeDelta(0, 1);
  104. return Syntax.binop(Syntax.binop(dt, "-", dt2) * 1000, "/", timedelta);
  105. }
  106. static public function fromString( s : String ) : Date
  107. {
  108. switch( s.length )
  109. {
  110. case 8: // hh:mm:ss
  111. var k = s.split(":");
  112. var d : Date = new Date(0, 0, 0, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]));
  113. return d;
  114. case 10: // YYYY-MM-DD
  115. var k = s.split("-");
  116. return new Date(Std.parseInt(k[0]),Std.parseInt(k[1]) - 1,Std.parseInt(k[2]),0,0,0);
  117. case 19: // YYYY-MM-DD hh:mm:ss
  118. var k = s.split(" ");
  119. var y = k[0].split("-");
  120. var t = k[1].split(":");
  121. 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]));
  122. default:
  123. throw "Invalid date format : " + s;
  124. }
  125. }
  126. }