Date.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. package;
  23. import haxe.Int64;
  24. @:SuppressWarnings("deprecation")
  25. @:coreApi class Date {
  26. private var date:java.util.Date;
  27. public function new(year:Int, month:Int, day:Int, hour:Int, min:Int, sec:Int):Void {
  28. // issue #1769
  29. year = year != 0 ? year - 1900 : 0;
  30. date = new java.util.Date(year, month, day, hour, min, sec);
  31. }
  32. public inline function getTime():Float {
  33. return cast date.getTime();
  34. }
  35. public inline function getHours():Int {
  36. return date.getHours();
  37. }
  38. public inline function getMinutes():Int {
  39. return date.getMinutes();
  40. }
  41. public inline function getSeconds():Int {
  42. return date.getSeconds();
  43. }
  44. public inline function getFullYear():Int {
  45. return date.getYear() + 1900;
  46. }
  47. public inline function getMonth():Int {
  48. return date.getMonth();
  49. }
  50. public inline function getDate():Int {
  51. return date.getDate();
  52. }
  53. public inline function getDay():Int {
  54. return date.getDay();
  55. }
  56. public function toString():String {
  57. var m = date.getMonth() + 1;
  58. var d = date.getDate();
  59. var h = date.getHours();
  60. var mi = date.getMinutes();
  61. var s = date.getSeconds();
  62. return (date.getYear() + 1900) + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  63. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  64. }
  65. static public function now():Date {
  66. var d = new Date(0, 0, 0, 0, 0, 0);
  67. d.date = new java.util.Date();
  68. return d;
  69. }
  70. static public function fromTime(t:Float):Date {
  71. var d = new Date(0, 0, 0, 0, 0, 0);
  72. d.date = new java.util.Date(cast(t, Int64));
  73. return d;
  74. }
  75. static public function fromString(s:String):Date {
  76. switch (s.length) {
  77. case 8: // hh:mm:ss
  78. var k = s.split(":");
  79. var d:Date = new Date(0, 0, 0, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]));
  80. return d;
  81. case 10: // YYYY-MM-DD
  82. var k = s.split("-");
  83. return new Date(Std.parseInt(k[0]), Std.parseInt(k[1]) - 1, Std.parseInt(k[2]), 0, 0, 0);
  84. case 19: // YYYY-MM-DD hh:mm:ss
  85. var k = s.split(" ");
  86. var y = k[0].split("-");
  87. var t = k[1].split(":");
  88. return new Date(Std.parseInt(y[0]), Std.parseInt(y[1]) - 1, Std.parseInt(y[2]), Std.parseInt(t[0]), Std.parseInt(t[1]), Std.parseInt(t[2]));
  89. default:
  90. throw "Invalid date format : " + s;
  91. }
  92. }
  93. }