Date.hx 2.5 KB

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