Date.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import neko.Lib;
  26. @:core_api @:final class Date {
  27. private var __t : Dynamic;
  28. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void {
  29. __t = date_set_day(0,year,month+1,day);
  30. __t = date_set_hour(__t,hour,min,sec);
  31. }
  32. public function getTime() : Float {
  33. return int32_to_float(__t) * 1000;
  34. }
  35. public function getFullYear() : Int {
  36. return date_get_day(__t).y;
  37. }
  38. public function getMonth() : Int {
  39. return date_get_day(__t).m -1;
  40. }
  41. public function getDate() : Int {
  42. return date_get_day(__t).d;
  43. }
  44. public function getHours() : Int {
  45. return date_get_hour(__t).h;
  46. }
  47. public function getMinutes() : Int {
  48. return date_get_hour(__t).m;
  49. }
  50. public function getSeconds() : Int {
  51. return date_get_hour(__t).s;
  52. }
  53. public function getDay() : Int {
  54. return Std.parseInt( new String(date_format(__t,untyped "%w".__s)) );
  55. }
  56. public function toString():String {
  57. return new String(date_format(__t,null));
  58. }
  59. public static function now() : Date {
  60. return new1(date_now());
  61. }
  62. public static function fromTime( t : Float ) : Date {
  63. t /= 1000;
  64. var i1 = untyped __dollar__int((t%65536));
  65. var i2 = untyped __dollar__int(t/65536);
  66. var i = int32_add(i1,int32_shl(i2,16));
  67. return new1(i);
  68. }
  69. public static function fromString( s : String ) : Date {
  70. return new1(date_new(untyped s.__s));
  71. }
  72. private static function new1(t : Dynamic) : Date {
  73. var d = new Date(2005,1,1,0,0,0);
  74. d.__t = t;
  75. return d;
  76. }
  77. static var date_new = Lib.load("std","date_new",1);
  78. static var date_now = Lib.load("std","date_now",0);
  79. static var date_format = Lib.load("std","date_format",2);
  80. static var date_set_hour = Lib.load("std","date_set_hour",4);
  81. static var date_set_day = Lib.load("std","date_set_day",4);
  82. static var date_get_day : Dynamic -> {y:Int, m:Int, d:Int} = Lib.load("std","date_get_day",1);
  83. static var date_get_hour : Dynamic -> {h:Int, m:Int, s:Int} = Lib.load("std","date_get_hour",1);
  84. static var int32_to_float = Lib.load("std","int32_to_float",1);
  85. static var int32_add = Lib.load("std","int32_add",2);
  86. static var int32_shl = Lib.load("std","int32_shl",2);
  87. static function __string() : String { return untyped "Date".__s; }
  88. }