Date.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 @:final class Date
  26. {
  27. private var __t : Float;
  28. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void {
  29. __t = untyped __call__("mktime", hour, min, sec, month+1, day, year);
  30. }
  31. public function getTime() : Float {
  32. return __t*1000;
  33. }
  34. private function getPhpTime() : Float {
  35. return __t;
  36. }
  37. public function getFullYear() : Int {
  38. return untyped __call__("intval", __call__("date", "Y", this.__t));
  39. }
  40. public function getMonth() : Int {
  41. var m : Int = untyped __call__("intval", __call__("date", "n", this.__t));
  42. return -1 + m;
  43. }
  44. public function getDate() : Int {
  45. return untyped __call__("intval", __call__("date", "j", this.__t));
  46. }
  47. public function getHours() : Int {
  48. return untyped __call__("intval", __call__("date", "G", this.__t));
  49. }
  50. public function getMinutes() : Int {
  51. return untyped __call__("intval", __call__("date", "i", this.__t));
  52. }
  53. public function getSeconds() : Int {
  54. return untyped __call__("intval", __call__("date", "s", this.__t));
  55. }
  56. public function getDay() : Int {
  57. return untyped __call__("intval", __call__("date", "w", this.__t));
  58. }
  59. public function toString():String {
  60. return untyped __call__("date", "Y-m-d H:i:s", this.__t);
  61. }
  62. public static function now() : Date {
  63. return fromPhpTime(untyped __call__("time"));
  64. }
  65. static function fromPhpTime( t : Float ) : Date {
  66. var d = new Date(2000,1,1,0,0,0);
  67. d.__t = t;
  68. return d;
  69. }
  70. public static function fromTime( t : Float ) : Date {
  71. var d = new Date(2000,1,1,0,0,0);
  72. d.__t = t/1000;
  73. return d;
  74. }
  75. public static function fromString( s : String ) : Date {
  76. return fromPhpTime(untyped __call__("strtotime", s));
  77. }
  78. }