Date.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C)2005-2012 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 cs.system.DateTime;
  24. import cs.system.TimeSpan;
  25. import haxe.Int64;
  26. @:coreApi class Date
  27. {
  28. private var date:DateTime;
  29. public function new(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ) : Void
  30. {
  31. if (day <= 0) day = 1;
  32. if (year <= 0) year = 1;
  33. date = new DateTime(year, month + 1, day, hour, min, sec);
  34. }
  35. public inline function getTime() : Float
  36. {
  37. return (cast(date.Ticks, Float) / cast(TimeSpan.TicksPerMillisecond, Float));
  38. }
  39. public inline function getHours() : Int
  40. {
  41. return date.Hour;
  42. }
  43. public inline function getMinutes() : Int
  44. {
  45. return date.Minute;
  46. }
  47. public inline function getSeconds() : Int
  48. {
  49. return date.Second;
  50. }
  51. public inline function getFullYear() : Int
  52. {
  53. return date.Year;
  54. }
  55. public inline function getMonth() : Int
  56. {
  57. return date.Month - 1;
  58. }
  59. public inline function getDate() : Int
  60. {
  61. return date.Day;
  62. }
  63. public inline function getDay() : Int
  64. {
  65. return cast(date.DayOfWeek, Int);
  66. }
  67. public function toString():String
  68. {
  69. var m = getMonth() + 1;
  70. var d = getDate();
  71. var h = getHours();
  72. var mi = getMinutes();
  73. var s = getSeconds();
  74. return (getFullYear())
  75. +"-"+(if( m < 10 ) "0"+m else ""+m)
  76. +"-"+(if( d < 10 ) "0"+d else ""+d)
  77. +" "+(if( h < 10 ) "0"+h else ""+h)
  78. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  79. +":"+(if( s < 10 ) "0"+s else ""+s);
  80. }
  81. static public function now() : Date
  82. {
  83. var d = new Date(0, 0, 0, 0, 0, 0);
  84. d.date = DateTime.Now;
  85. return d;
  86. }
  87. static public function fromTime( t : Float ) : Date
  88. {
  89. var d = new Date(0, 0, 0, 0, 0, 0);
  90. d.date = new DateTime(cast(t * cast(TimeSpan.TicksPerMillisecond, Float), Int64));
  91. return d;
  92. }
  93. static public function fromString( s : String ) : Date
  94. {
  95. switch( s.length )
  96. {
  97. case 8: // hh:mm:ss
  98. var k = s.split(":");
  99. var d : Date = new Date(1, 1, 1, Std.parseInt(k[0]), Std.parseInt(k[1]), Std.parseInt(k[2]));
  100. return d;
  101. case 10: // YYYY-MM-DD
  102. var k = s.split("-");
  103. return new Date(Std.parseInt(k[0]),Std.parseInt(k[1]) - 1,Std.parseInt(k[2]),0,0,0);
  104. case 19: // YYYY-MM-DD hh:mm:ss
  105. var k = s.split(" ");
  106. var y = k[0].split("-");
  107. var t = k[1].split(":");
  108. 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]));
  109. default:
  110. throw "Invalid date format : " + s;
  111. }
  112. }
  113. private static function fromNative( d : cs.system.DateTime ) : Date
  114. {
  115. var date = new Date(0, 0, 0, 0, 0, 0);
  116. date.date = d;
  117. return date;
  118. }
  119. }