2
0

Date.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 php.Global.*;
  23. import php.Syntax.*;
  24. @:coreApi final class Date {
  25. private var __t:Float;
  26. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  27. __t = mktime(hour, min, sec, month + 1, day, year);
  28. }
  29. public function getTime():Float {
  30. return __t * 1000.0;
  31. }
  32. private function getPhpTime():Float {
  33. return __t;
  34. }
  35. public function getFullYear():Int {
  36. return int(date("Y", int(__t)));
  37. }
  38. public function getMonth():Int {
  39. var m:Int = int(date("n", int(__t)));
  40. return -1 + m;
  41. }
  42. public function getDate():Int {
  43. return int(date("j", int(__t)));
  44. }
  45. public function getHours():Int {
  46. return int(date("G", int(__t)));
  47. }
  48. public function getMinutes():Int {
  49. return int(date("i", int(__t)));
  50. }
  51. public function getSeconds():Int {
  52. return int(date("s", int(__t)));
  53. }
  54. public function getDay():Int {
  55. return int(date("w", int(__t)));
  56. }
  57. public function getUTCFullYear():Int {
  58. return int(gmdate("Y", int(__t)));
  59. }
  60. public function getUTCMonth():Int {
  61. var m:Int = int(gmdate("n", int(__t)));
  62. return -1 + m;
  63. }
  64. public function getUTCDate():Int {
  65. return int(gmdate("j", int(__t)));
  66. }
  67. public function getUTCHours():Int {
  68. return int(gmdate("G", int(__t)));
  69. }
  70. public function getUTCMinutes():Int {
  71. return int(gmdate("i", int(__t)));
  72. }
  73. public function getUTCSeconds():Int {
  74. return int(gmdate("s", int(__t)));
  75. }
  76. public function getUTCDay():Int {
  77. return int(gmdate("w", int(__t)));
  78. }
  79. public function getTimezoneOffset():Int {
  80. return -Std.int(int(date("Z", int(__t))) / 60);
  81. }
  82. public function toString():String {
  83. return date("Y-m-d H:i:s", int(__t));
  84. }
  85. public static function now():Date {
  86. return fromPhpTime(round(microtime(true), 3));
  87. }
  88. static function fromPhpTime(t:Float):Date {
  89. var d = new Date(2000, 1, 1, 0, 0, 0);
  90. d.__t = t;
  91. return d;
  92. }
  93. public static function fromTime(t:Float):Date {
  94. var d = new Date(2000, 1, 1, 0, 0, 0);
  95. d.__t = t / 1000;
  96. return d;
  97. }
  98. public static function fromString(s:String):Date {
  99. switch (s.length) {
  100. case 8: // hh:mm:ss
  101. var k = s.split(":");
  102. return Date.fromTime(Std.parseInt(k[0]) * 3600000. + Std.parseInt(k[1]) * 60000. + Std.parseInt(k[2]) * 1000.);
  103. case 10: // YYYY-MM-DD
  104. var k = s.split("-");
  105. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  106. case 19: // YYYY-MM-DD hh:mm:ss
  107. var k = s.split(" ");
  108. var y = k[0].split("-");
  109. var t = k[1].split(":");
  110. 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]));
  111. default:
  112. throw "Invalid date format : " + s;
  113. }
  114. }
  115. }