Date.hx 2.8 KB

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