Date.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 neko.Lib;
  23. @:coreApi final class Date {
  24. private var __t:Dynamic;
  25. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  26. __t = date_set_day(0, year, month + 1, day);
  27. __t = date_set_hour(__t, hour, min, sec);
  28. }
  29. public function getTime():Float {
  30. return int32_to_float(__t) * 1000;
  31. }
  32. public function getFullYear():Int {
  33. return date_get_day(__t).y;
  34. }
  35. public function getMonth():Int {
  36. return date_get_day(__t).m - 1;
  37. }
  38. public function getDate():Int {
  39. return date_get_day(__t).d;
  40. }
  41. public function getHours():Int {
  42. return date_get_hour(__t).h;
  43. }
  44. public function getMinutes():Int {
  45. return date_get_hour(__t).m;
  46. }
  47. public function getSeconds():Int {
  48. return date_get_hour(__t).s;
  49. }
  50. public function getDay():Int {
  51. return Std.parseInt(new String(date_format(__t, untyped "%w".__s)));
  52. }
  53. public function getUTCFullYear():Int {
  54. return date_get_utc_day(__t).y;
  55. }
  56. public function getUTCMonth():Int {
  57. return date_get_utc_day(__t).m - 1;
  58. }
  59. public function getUTCDate():Int {
  60. return date_get_utc_day(__t).d;
  61. }
  62. public function getUTCHours():Int {
  63. return date_get_utc_hour(__t).h;
  64. }
  65. public function getUTCMinutes():Int {
  66. return date_get_utc_hour(__t).m;
  67. }
  68. public function getUTCSeconds():Int {
  69. return date_get_utc_hour(__t).s;
  70. }
  71. public function getUTCDay():Int {
  72. return Std.parseInt(new String(date_utc_format(__t, untyped "%w".__s)));
  73. }
  74. public function getTimezoneOffset():Int {
  75. return -date_get_tz(__t);
  76. }
  77. @:keep public function toString():String {
  78. return new String(date_format(__t, null));
  79. }
  80. public static function now():Date {
  81. return new1(date_now());
  82. }
  83. public static function fromTime(t:Float):Date {
  84. t /= 1000;
  85. var i1 = untyped __dollar__int((t % 65536));
  86. var i2 = untyped __dollar__int(t / 65536);
  87. var i = int32_add(i1, int32_shl(i2, 16));
  88. return new1(i);
  89. }
  90. public static function fromString(s:String):Date {
  91. return new1(date_new(untyped s.__s));
  92. }
  93. private static function new1(t:Dynamic):Date {
  94. var d = new Date(2005, 1, 1, 0, 0, 0);
  95. d.__t = t;
  96. return d;
  97. }
  98. static var date_new = Lib.load("std", "date_new", 1);
  99. static var date_now = Lib.load("std", "date_now", 0);
  100. static var date_format = Lib.load("std", "date_format", 2);
  101. static var date_utc_format = Lib.load("std", "date_utc_format", 2);
  102. static var date_set_hour = Lib.load("std", "date_set_hour", 4);
  103. static var date_set_day = Lib.load("std", "date_set_day", 4);
  104. static var date_get_day:Dynamic->{y: Int, m: Int, d: Int} = Lib.load("std", "date_get_day", 1);
  105. static var date_get_hour:Dynamic->{h: Int, m: Int, s: Int} = Lib.load("std", "date_get_hour", 1);
  106. static var date_get_utc_day:Dynamic->{y: Int, m: Int, d: Int} = Lib.load("std", "date_get_utc_day", 1);
  107. static var date_get_utc_hour:Dynamic->{h: Int, m: Int, s: Int} = Lib.load("std", "date_get_utc_hour", 1);
  108. static var date_get_tz = Lib.load("std", "date_get_tz", 1);
  109. static var int32_to_float = Lib.load("std", "int32_to_float", 1);
  110. static var int32_add = Lib.load("std", "int32_add", 2);
  111. static var int32_shl = Lib.load("std", "int32_shl", 2);
  112. @:keep static function __string():String {
  113. return untyped "Date".__s;
  114. }
  115. }