2
0

Date.hx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C)2005-2017 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 { return untyped __global__.__hxcpp_get_hours(mSeconds); }
  31. public function getMinutes() : Int { return untyped __global__.__hxcpp_get_minutes(mSeconds); }
  32. public function getSeconds() : Int { return untyped __global__.__hxcpp_get_seconds(mSeconds); }
  33. public function getFullYear() : Int { return untyped __global__.__hxcpp_get_year(mSeconds); }
  34. public function getMonth() : Int { return untyped __global__.__hxcpp_get_month(mSeconds); }
  35. public function getDate() : Int { return untyped __global__.__hxcpp_get_date(mSeconds); }
  36. public function getDay() : Int { return untyped __global__.__hxcpp_get_day(mSeconds); }
  37. public function toString():String { return untyped __global__.__hxcpp_to_string(mSeconds); }
  38. public static function now() : Date {
  39. return fromTime( untyped __global__.__hxcpp_date_now()*1000.0);
  40. }
  41. private static function new1(t : Dynamic) : Date {
  42. return new Date(2005,1,1,0,0,0);
  43. }
  44. public static function fromTime( t : Float ) : Date {
  45. var result = new Date(0,0,0,0,0,0);
  46. result.mSeconds = t*0.001;
  47. return result;
  48. }
  49. public static function fromString( s : String ) : Date {
  50. switch( s.length ) {
  51. case 8: // hh:mm:ss
  52. var k = s.split(":");
  53. var d : Date = new Date(0,0,0,Std.parseInt(k[0]),Std.parseInt(k[1]),Std.parseInt(k[2]));
  54. return d;
  55. case 10: // YYYY-MM-DD
  56. var k = s.split("-");
  57. return new Date(Std.parseInt(k[0]),Std.parseInt(k[1])-1,Std.parseInt(k[2]),0,0,0);
  58. case 19: // YYYY-MM-DD hh:mm:ss
  59. var k = s.split(" ");
  60. var y = k[0].split("-");
  61. var t = k[1].split(":");
  62. return new Date(Std.parseInt(y[0]),Std.parseInt(y[1]) - 1,Std.parseInt(y[2]),
  63. Std.parseInt(t[0]),Std.parseInt(t[1]),Std.parseInt(t[2]));
  64. default:
  65. throw "Invalid date format : " + s;
  66. }
  67. }
  68. }