Date.hx 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Date {
  26. private var mSeconds:Float;
  27. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void {
  28. mSeconds = untyped __global__.__hxcpp_new_date(year,month,day,hour,min,sec);
  29. }
  30. public function getTime() : Float {
  31. return mSeconds * 1000.0;
  32. }
  33. public function getHours() : Int { return untyped __global__.__hxcpp_get_hours(mSeconds); }
  34. public function getMinutes() : Int { return untyped __global__.__hxcpp_get_minutes(mSeconds); }
  35. public function getSeconds() : Int { return untyped __global__.__hxcpp_get_seconds(mSeconds); }
  36. public function getFullYear() : Int { return untyped __global__.__hxcpp_get_year(mSeconds); }
  37. public function getMonth() : Int { return untyped __global__.__hxcpp_get_month(mSeconds); }
  38. public function getDate() : Int { return untyped __global__.__hxcpp_get_date(mSeconds); }
  39. public function getDay() : Int { return untyped __global__.__hxcpp_get_day(mSeconds); }
  40. public function toString():String { return untyped __global__.__hxcpp_to_string(mSeconds); }
  41. public static function now() : Date {
  42. return fromTime( untyped __global__.__hxcpp_date_now()*1000.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. return null;
  68. }
  69. }