Date.hx 2.5 KB

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