DateTools.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C)2005-2015 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. /**
  23. The DateTools class contains some extra functionalities for handling `Date`
  24. instances and timestamps.
  25. In the context of Haxe dates, a timestamp is defined as the number of
  26. milliseconds elapsed since 1st January 1970.
  27. **/
  28. class DateTools {
  29. #if php
  30. #elseif (neko && !(macro || interp))
  31. static var date_format = neko.Lib.load("std","date_format",2);
  32. #else
  33. private static function __format_get( d : Date, e : String ) : String {
  34. return switch( e ){
  35. case "%":
  36. "%";
  37. case "C":
  38. untyped StringTools.lpad(Std.string(Std.int(d.getFullYear()/100)),"0",2);
  39. case "d":
  40. untyped StringTools.lpad(Std.string(d.getDate()),"0",2);
  41. case "D":
  42. __format(d,"%m/%d/%y");
  43. case "e":
  44. untyped Std.string(d.getDate());
  45. case "F":
  46. __format(d,"%Y-%m-%d");
  47. case "H","k":
  48. untyped StringTools.lpad(Std.string(d.getHours()),if( e == "H" ) "0" else " ",2);
  49. case "I","l":
  50. var hour = d.getHours()%12;
  51. untyped StringTools.lpad(Std.string(hour == 0 ? 12 : hour),if( e == "I" ) "0" else " ",2);
  52. case "m":
  53. untyped StringTools.lpad(Std.string(d.getMonth()+1),"0",2);
  54. case "M":
  55. untyped StringTools.lpad(Std.string(d.getMinutes()),"0",2);
  56. case "n":
  57. "\n";
  58. case "p":
  59. untyped if( d.getHours() > 11 ) "PM"; else "AM";
  60. case "r":
  61. __format(d,"%I:%M:%S %p");
  62. case "R":
  63. __format(d,"%H:%M");
  64. case "s":
  65. Std.string(Std.int(d.getTime()/1000));
  66. case "S":
  67. untyped StringTools.lpad(Std.string(d.getSeconds()),"0",2);
  68. case "t":
  69. "\t";
  70. case "T":
  71. __format(d,"%H:%M:%S");
  72. case "u":
  73. untyped{
  74. var t = d.getDay();
  75. if( t == 0 ) "7"; else Std.string(t);
  76. }
  77. case "w":
  78. untyped Std.string(d.getDay());
  79. case "y":
  80. untyped StringTools.lpad(Std.string(d.getFullYear()%100),"0",2);
  81. case "Y":
  82. untyped Std.string(d.getFullYear());
  83. default:
  84. throw "Date.format %"+e+"- not implemented yet.";
  85. }
  86. }
  87. private static function __format( d : Date, f : String ) : String {
  88. var r = new StringBuf();
  89. var p = 0;
  90. while( true ){
  91. var np = f.indexOf("%", p);
  92. if( np < 0 )
  93. break;
  94. r.addSub(f,p,np-p);
  95. r.add( __format_get(d, f.substr(np+1,1) ) );
  96. p = np+2;
  97. }
  98. r.addSub(f,p,f.length-p);
  99. return r.toString();
  100. }
  101. #end
  102. /**
  103. Format the date `d` according to the format `f`. The format is
  104. compatible with the `strftime` standard format, except that there is no
  105. support in Flash and JS for day and months names (due to lack of proper
  106. internationalization API). On Haxe/Neko/Windows, some formats are not
  107. supported.
  108. **/
  109. public static function format( d : Date, f : String ) : String {
  110. #if (neko && !(macro || interp))
  111. return new String(untyped date_format(d.__t, f.__s));
  112. #elseif php
  113. return untyped __call__("strftime",f,d.__t);
  114. #else
  115. return __format(d,f);
  116. #end
  117. }
  118. /**
  119. Returns the result of adding timestamp `t` to Date `d`.
  120. This is a convenience function for calling
  121. `Date.fromTime(d.getTime() + t)`.
  122. **/
  123. public static inline function delta( d : Date, t : Float ) : Date {
  124. return Date.fromTime( d.getTime() + t );
  125. }
  126. static var DAYS_OF_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  127. /**
  128. Returns the number of days in the month of Date `d`.
  129. This method handles leap years.
  130. **/
  131. public static function getMonthDays( d : Date ) : Int {
  132. var month = d.getMonth();
  133. var year = d.getFullYear();
  134. if (month != 1)
  135. return DAYS_OF_MONTH[month];
  136. var isB = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
  137. return if (isB) 29 else 28;
  138. }
  139. /**
  140. Converts a number of seconds to a timestamp.
  141. **/
  142. public static inline function seconds( n : Float ) : Float {
  143. return n * 1000.0;
  144. }
  145. /**
  146. Converts a number of minutes to a timestamp.
  147. **/
  148. public static inline function minutes( n : Float ) : Float {
  149. return n * 60.0 * 1000.0;
  150. }
  151. /**
  152. Converts a number of hours to a timestamp.
  153. **/
  154. public static inline function hours( n : Float ) : Float {
  155. return n * 60.0 * 60.0 * 1000.0;
  156. }
  157. /**
  158. Converts a number of days to a timestamp.
  159. **/
  160. public static inline function days( n : Float ) : Float {
  161. return n * 24.0 * 60.0 * 60.0 * 1000.0;
  162. }
  163. /**
  164. Separate a date-time into several components
  165. **/
  166. public static function parse( t : Float ) {
  167. var s = t / 1000;
  168. var m = s / 60;
  169. var h = m / 60;
  170. return {
  171. ms : t % 1000,
  172. seconds : Std.int(s % 60),
  173. minutes : Std.int(m % 60),
  174. hours : Std.int(h % 24),
  175. days : Std.int(h / 24),
  176. };
  177. }
  178. /**
  179. Build a date-time from several components
  180. **/
  181. public static function make( o : { ms : Float, seconds : Int, minutes : Int, hours : Int, days : Int } ) {
  182. return o.ms + 1000.0 * (o.seconds + 60.0 * (o.minutes + 60.0 * (o.hours + 24.0 * o.days)));
  183. }
  184. #if (js || flash || php || cpp || python)
  185. /**
  186. Retrieve Unix timestamp value from Date components. Takes same argument sequence as the Date constructor.
  187. **/
  188. public static #if (js || flash || php) inline #end function makeUtc(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ):Float {
  189. #if (js || flash || python)
  190. return untyped Date.UTC(year, month, day, hour, min, sec);
  191. #elseif php
  192. return untyped __call__("gmmktime", hour, min, sec, month + 1, day, year) * 1000;
  193. #elseif cpp
  194. return untyped __global__.__hxcpp_utc_date(year,month,day,hour,min,sec)*1000.0 ;
  195. #else
  196. //TODO
  197. return 0.;
  198. #end
  199. }
  200. #end
  201. }