Date.hx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 toString():String {
  52. return untyped __global__.__hxcpp_to_string(mSeconds);
  53. }
  54. public static function now():Date {
  55. return fromTime(untyped __global__.__hxcpp_date_now() * 1000.0);
  56. }
  57. private static function new1(t:Dynamic):Date {
  58. return new Date(2005, 1, 1, 0, 0, 0);
  59. }
  60. public static function fromTime(t:Float):Date {
  61. var result = new Date(0, 0, 0, 0, 0, 0);
  62. result.mSeconds = t * 0.001;
  63. return result;
  64. }
  65. public static function fromString(s:String):Date {
  66. switch (s.length) {
  67. case 8: // hh:mm:ss
  68. var k = s.split(":");
  69. var d:Date = new Date(0, 0, 0, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]));
  70. return d;
  71. case 10: // YYYY-MM-DD
  72. var k = s.split("-");
  73. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  74. case 19: // YYYY-MM-DD hh:mm:ss
  75. var k = s.split(" ");
  76. var y = k[0].split("-");
  77. var t = k[1].split(":");
  78. 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]));
  79. default:
  80. throw "Invalid date format : " + s;
  81. }
  82. }
  83. }