Date.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C)2005-2012 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. @:keep public function toString():String {
  54. return new String(date_format(__t,null));
  55. }
  56. public static function now() : Date {
  57. return new1(date_now());
  58. }
  59. public static function fromTime( t : Float ) : Date {
  60. t /= 1000;
  61. var i1 = untyped __dollar__int((t%65536));
  62. var i2 = untyped __dollar__int(t/65536);
  63. var i = int32_add(i1,int32_shl(i2,16));
  64. return new1(i);
  65. }
  66. public static function fromString( s : String ) : Date {
  67. return new1(date_new(untyped s.__s));
  68. }
  69. private static function new1(t : Dynamic) : Date {
  70. var d = new Date(2005,1,1,0,0,0);
  71. d.__t = t;
  72. return d;
  73. }
  74. static var date_new = Lib.load("std","date_new",1);
  75. static var date_now = Lib.load("std","date_now",0);
  76. static var date_format = Lib.load("std","date_format",2);
  77. static var date_set_hour = Lib.load("std","date_set_hour",4);
  78. static var date_set_day = Lib.load("std","date_set_day",4);
  79. static var date_get_day : Dynamic -> {y:Int, m:Int, d:Int} = Lib.load("std","date_get_day",1);
  80. static var date_get_hour : Dynamic -> {h:Int, m:Int, s:Int} = Lib.load("std","date_get_hour",1);
  81. static var int32_to_float = Lib.load("std","int32_to_float",1);
  82. static var int32_add = Lib.load("std","int32_add",2);
  83. static var int32_shl = Lib.load("std","int32_shl",2);
  84. @:keep static function __string() : String { return untyped "Date".__s; }
  85. }