Date.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @:coreApi class Date {
  23. private var mSeconds:Float;
  24. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  25. mSeconds = untyped __global__.__hxcpp_new_date(year, month, day, hour, min, sec);
  26. }
  27. public function getTime():Float {
  28. return mSeconds * 1000.0;
  29. }
  30. public function getHours():Int {
  31. return untyped __global__.__hxcpp_get_hours(mSeconds);
  32. }
  33. public function getMinutes():Int {
  34. return untyped __global__.__hxcpp_get_minutes(mSeconds);
  35. }
  36. public function getSeconds():Int {
  37. return untyped __global__.__hxcpp_get_seconds(mSeconds);
  38. }
  39. public function getFullYear():Int {
  40. return untyped __global__.__hxcpp_get_year(mSeconds);
  41. }
  42. public function getMonth():Int {
  43. return untyped __global__.__hxcpp_get_month(mSeconds);
  44. }
  45. public function getDate():Int {
  46. return untyped __global__.__hxcpp_get_date(mSeconds);
  47. }
  48. public function getDay():Int {
  49. return untyped __global__.__hxcpp_get_day(mSeconds);
  50. }
  51. public function getUTCHours():Int {
  52. return untyped __global__.__hxcpp_get_utc_hours(mSeconds);
  53. }
  54. public function getUTCMinutes():Int {
  55. return untyped __global__.__hxcpp_get_utc_minutes(mSeconds);
  56. }
  57. public function getUTCSeconds():Int {
  58. return untyped __global__.__hxcpp_get_utc_seconds(mSeconds);
  59. }
  60. public function getUTCFullYear():Int {
  61. return untyped __global__.__hxcpp_get_utc_year(mSeconds);
  62. }
  63. public function getUTCMonth():Int {
  64. return untyped __global__.__hxcpp_get_utc_month(mSeconds);
  65. }
  66. public function getUTCDate():Int {
  67. return untyped __global__.__hxcpp_get_utc_date(mSeconds);
  68. }
  69. public function getUTCDay():Int {
  70. return untyped __global__.__hxcpp_get_utc_day(mSeconds);
  71. }
  72. public function getTimezoneOffset():Int {
  73. return -Std.int((untyped __global__.__hxcpp_timezone_offset(mSeconds)) / 60);
  74. }
  75. public function toString():String {
  76. return untyped __global__.__hxcpp_to_string(mSeconds);
  77. }
  78. public static function now():Date {
  79. return fromTime(untyped __global__.__hxcpp_date_now() * 1000.0);
  80. }
  81. private static function new1(t:Dynamic):Date {
  82. return new Date(2005, 1, 1, 0, 0, 0);
  83. }
  84. public static function fromTime(t:Float):Date {
  85. var result = new Date(0, 0, 0, 0, 0, 0);
  86. result.mSeconds = t * 0.001;
  87. return result;
  88. }
  89. public static function fromString(s:String):Date {
  90. switch (s.length) {
  91. case 8: // hh:mm:ss
  92. var k = s.split(":");
  93. return Date.fromTime(Std.parseInt(k[0]) * 3600000. + Std.parseInt(k[1]) * 60000. + Std.parseInt(k[2]) * 1000.);
  94. case 10: // YYYY-MM-DD
  95. var k = s.split("-");
  96. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  97. case 19: // YYYY-MM-DD hh:mm:ss
  98. var k = s.split(" ");
  99. var y = k[0].split("-");
  100. var t = k[1].split(":");
  101. 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]));
  102. default:
  103. throw "Invalid date format : " + s;
  104. }
  105. }
  106. }