Date.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C)2005-2019 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. import python.lib.datetime.Datetime;
  23. import python.lib.datetime.Timedelta;
  24. import python.lib.datetime.Timezone;
  25. import python.Syntax;
  26. @:coreApi class Date {
  27. private var date:Datetime;
  28. private var dateUTC:Datetime;
  29. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  30. if (year < Datetime.min.year)
  31. year = Datetime.min.year;
  32. if (day == 0)
  33. day = 1;
  34. date = makeLocal(new Datetime(year, month + 1, day, hour, min, sec, 0));
  35. dateUTC = date.astimezone(Timezone.utc);
  36. }
  37. public inline function getTime():Float {
  38. return date.timestamp() * 1000;
  39. }
  40. public inline function getHours():Int {
  41. return date.hour;
  42. }
  43. public inline function getMinutes():Int {
  44. return date.minute;
  45. }
  46. public inline function getSeconds():Int {
  47. return date.second;
  48. }
  49. public inline function getFullYear():Int {
  50. return date.year;
  51. }
  52. public inline function getMonth():Int {
  53. return date.month - 1;
  54. }
  55. public inline function getDate():Int {
  56. return date.day;
  57. }
  58. public inline function getDay():Int {
  59. return date.isoweekday() % 7;
  60. }
  61. public inline function getUTCHours():Int {
  62. return dateUTC.hour;
  63. }
  64. public inline function getUTCMinutes():Int {
  65. return dateUTC.minute;
  66. }
  67. public inline function getUTCSeconds():Int {
  68. return dateUTC.second;
  69. }
  70. public inline function getUTCFullYear():Int {
  71. return dateUTC.year;
  72. }
  73. public inline function getUTCMonth():Int {
  74. return dateUTC.month - 1;
  75. }
  76. public inline function getUTCDate():Int {
  77. return dateUTC.day;
  78. }
  79. public inline function getUTCDay():Int {
  80. return dateUTC.isoweekday() % 7;
  81. }
  82. public function getTimezoneOffset():Int {
  83. return -Std.int(Syntax.binop(date.utcoffset(), "/", new Timedelta(0, 60)));
  84. }
  85. public function toString():String {
  86. return date.strftime("%Y-%m-%d %H:%M:%S");
  87. }
  88. static public function now():Date {
  89. var d = new Date(2000, 0, 1, 0, 0, 0);
  90. d.date = makeLocal(Datetime.now());
  91. d.dateUTC = d.date.astimezone(Timezone.utc);
  92. return d;
  93. }
  94. static public function fromTime(t:Float):Date {
  95. var d = new Date(2000, 0, 1, 0, 0, 0);
  96. d.date = makeLocal(Datetime.fromtimestamp(t / 1000.0));
  97. d.dateUTC = d.date.astimezone(Timezone.utc);
  98. return d;
  99. }
  100. static function makeLocal(date:Datetime):Datetime {
  101. try {
  102. return date.astimezone();
  103. } catch (e:Dynamic) {
  104. // No way in vanilla Python <=3.5 to get the local timezone
  105. // Additionally dates close to the epoch <86400 will throw on astimezone
  106. var tzinfo = Datetime.now(Timezone.utc).astimezone().tzinfo;
  107. return date.replace({tzinfo: tzinfo});
  108. }
  109. }
  110. static function UTC(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Float {
  111. return new Datetime(year, month + 1, day, hour, min, sec, 0, python.lib.datetime.Timezone.utc).timestamp() * 1000;
  112. }
  113. static public function fromString(s:String):Date {
  114. switch (s.length) {
  115. case 8: // hh:mm:ss
  116. var k = s.split(":");
  117. return Date.fromTime(Std.parseInt(k[0]) * 3600000. + Std.parseInt(k[1]) * 60000. + Std.parseInt(k[2]) * 1000.);
  118. case 10: // YYYY-MM-DD
  119. var k = s.split("-");
  120. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  121. case 19: // YYYY-MM-DD hh:mm:ss
  122. var k = s.split(" ");
  123. var y = k[0].split("-");
  124. var t = k[1].split(":");
  125. 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]));
  126. default:
  127. throw "Invalid date format : " + s;
  128. }
  129. }
  130. }