Date.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 cs.system.DateTime;
  23. import cs.system.DateTimeKind;
  24. import cs.system.TimeSpan;
  25. import haxe.Int64;
  26. #if core_api_serialize
  27. @:meta(System.Serializable)
  28. #end
  29. @:coreApi class Date {
  30. @:readOnly private static var epochTicks:Int64 = new DateTime(1970, 1, 1).Ticks;
  31. private var date:DateTime;
  32. private var dateUTC:DateTime;
  33. @:overload public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  34. if (day <= 0)
  35. day = 1;
  36. if (year <= 0)
  37. year = 1;
  38. date = new DateTime(year, month + 1, day, hour, min, sec, DateTimeKind.Local);
  39. dateUTC = date.ToUniversalTime();
  40. }
  41. @:overload private function new(native:DateTime) {
  42. if (native.Kind == DateTimeKind.Utc) {
  43. dateUTC = native;
  44. date = dateUTC.ToLocalTime();
  45. } else {
  46. date = native;
  47. dateUTC = date.ToUniversalTime();
  48. }
  49. }
  50. public inline function getTime():Float {
  51. #if (net_ver < 35)
  52. return cast(cs.system.TimeZone.CurrentTimeZone.ToUniversalTime(date).Ticks - epochTicks, Float) / cast(TimeSpan.TicksPerMillisecond, Float);
  53. #else
  54. return cast(cs.system.TimeZoneInfo.ConvertTimeToUtc(date).Ticks - epochTicks, Float) / cast(TimeSpan.TicksPerMillisecond, Float);
  55. #end
  56. }
  57. public inline function getHours():Int {
  58. return date.Hour;
  59. }
  60. public inline function getMinutes():Int {
  61. return date.Minute;
  62. }
  63. public inline function getSeconds():Int {
  64. return date.Second;
  65. }
  66. public inline function getFullYear():Int {
  67. return date.Year;
  68. }
  69. public inline function getMonth():Int {
  70. return date.Month - 1;
  71. }
  72. public inline function getDate():Int {
  73. return date.Day;
  74. }
  75. public inline function getDay():Int {
  76. return cast(date.DayOfWeek, Int);
  77. }
  78. public inline function getUTCHours():Int {
  79. return dateUTC.Hour;
  80. }
  81. public inline function getUTCMinutes():Int {
  82. return dateUTC.Minute;
  83. }
  84. public inline function getUTCSeconds():Int {
  85. return dateUTC.Second;
  86. }
  87. public inline function getUTCFullYear():Int {
  88. return dateUTC.Year;
  89. }
  90. public inline function getUTCMonth():Int {
  91. return dateUTC.Month - 1;
  92. }
  93. public inline function getUTCDate():Int {
  94. return dateUTC.Day;
  95. }
  96. public inline function getUTCDay():Int {
  97. return cast(dateUTC.DayOfWeek, Int);
  98. }
  99. public inline function getTimezoneOffset():Int {
  100. return Std.int((cast(dateUTC.Ticks - date.Ticks, Float) / cast(TimeSpan.TicksPerMillisecond, Float)) / 60000.);
  101. }
  102. public function toString():String {
  103. return date.ToString("yyyy-MM-dd HH\\:mm\\:ss");
  104. }
  105. static public inline function now():Date {
  106. return new Date(DateTime.Now);
  107. }
  108. static public inline function fromTime(t:Float):Date {
  109. #if (net_ver < 35)
  110. return new Date(cs.system.TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(cast(t * cast(TimeSpan.TicksPerMillisecond, Float), Int64) + epochTicks)));
  111. #else
  112. return new Date(cs.system.TimeZoneInfo.ConvertTimeFromUtc(new DateTime(cast(t * cast(TimeSpan.TicksPerMillisecond, Float), Int64) + epochTicks),
  113. cs.system.TimeZoneInfo.Local));
  114. #end
  115. }
  116. static public function fromString(s:String):Date {
  117. switch (s.length) {
  118. case 8: // hh:mm:ss
  119. var k = s.split(":");
  120. return new Date(new DateTime(1970, 1, 1, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]), DateTimeKind.Utc));
  121. case 10: // YYYY-MM-DD
  122. var k = s.split("-");
  123. return new Date(new DateTime(Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]), 0, 0, 0, DateTimeKind.Local));
  124. case 19: // YYYY-MM-DD hh:mm:ss
  125. var k = s.split(" ");
  126. var y = k[0].split("-");
  127. var t = k[1].split(":");
  128. return new Date(new DateTime(Std.parseInt(y[0]), Std.parseInt(y[1]), Std.parseInt(y[2]), Std.parseInt(t[0]), Std.parseInt(t[1]), Std.parseInt(t[2]), DateTimeKind.Local));
  129. default:
  130. throw "Invalid date format : " + s;
  131. }
  132. }
  133. private static inline function fromNative(d:cs.system.DateTime):Date {
  134. return new Date(d);
  135. }
  136. }