DateTools.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C)2005-2016 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. ```haxe
  109. var t = DateTools.format(Date.now(), "%Y-%m-%d_%H:%M:%S");
  110. // 2016-07-08_14:44:05
  111. var t = DateTools.format(Date.now(), "%r");
  112. // 02:44:05 PM
  113. var t = DateTools.format(Date.now(), "%T");
  114. // 14:44:05
  115. var t = DateTools.format(Date.now(), "%F");
  116. // 2016-07-08
  117. ```
  118. **/
  119. public static function format( d : Date, f : String ) : String {
  120. #if (neko && !(macro || interp))
  121. return new String(untyped date_format(d.__t, f.__s));
  122. #elseif php
  123. return untyped __call__("strftime",f,d.__t);
  124. #else
  125. return __format(d,f);
  126. #end
  127. }
  128. /**
  129. Returns the result of adding timestamp `t` to Date `d`.
  130. This is a convenience function for calling
  131. `Date.fromTime(d.getTime() + t)`.
  132. **/
  133. public static inline function delta( d : Date, t : Float ) : Date {
  134. return Date.fromTime( d.getTime() + t );
  135. }
  136. static var DAYS_OF_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  137. /**
  138. Returns the number of days in the month of Date `d`.
  139. This method handles leap years.
  140. **/
  141. public static function getMonthDays( d : Date ) : Int {
  142. var month = d.getMonth();
  143. var year = d.getFullYear();
  144. if (month != 1)
  145. return DAYS_OF_MONTH[month];
  146. var isB = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
  147. return if (isB) 29 else 28;
  148. }
  149. /**
  150. Converts a number of seconds to a timestamp.
  151. **/
  152. public static inline function seconds( n : Float ) : Float {
  153. return n * 1000.0;
  154. }
  155. /**
  156. Converts a number of minutes to a timestamp.
  157. **/
  158. #if as3 @:extern #end public static inline function minutes( n : Float ) : Float {
  159. return n * 60.0 * 1000.0;
  160. }
  161. /**
  162. Converts a number of hours to a timestamp.
  163. **/
  164. public static inline function hours( n : Float ) : Float {
  165. return n * 60.0 * 60.0 * 1000.0;
  166. }
  167. /**
  168. Converts a number of days to a timestamp.
  169. **/
  170. public static inline function days( n : Float ) : Float {
  171. return n * 24.0 * 60.0 * 60.0 * 1000.0;
  172. }
  173. /**
  174. Separate a date-time into several components
  175. **/
  176. public static function parse( t : Float ) {
  177. var s = t / 1000;
  178. var m = s / 60;
  179. var h = m / 60;
  180. return {
  181. ms : t % 1000,
  182. seconds : Std.int(s % 60),
  183. minutes : Std.int(m % 60),
  184. hours : Std.int(h % 24),
  185. days : Std.int(h / 24),
  186. };
  187. }
  188. /**
  189. Build a date-time from several components
  190. **/
  191. public static function make( o : { ms : Float, seconds : Int, minutes : Int, hours : Int, days : Int } ) {
  192. return o.ms + 1000.0 * (o.seconds + 60.0 * (o.minutes + 60.0 * (o.hours + 24.0 * o.days)));
  193. }
  194. #if (js || flash || php || cpp || python)
  195. /**
  196. Retrieve Unix timestamp value from Date components. Takes same argument sequence as the Date constructor.
  197. **/
  198. public static #if (js || flash || php) inline #end function makeUtc(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ):Float {
  199. #if (js || flash || python)
  200. return untyped Date.UTC(year, month, day, hour, min, sec);
  201. #elseif php
  202. return untyped __call__("gmmktime", hour, min, sec, month + 1, day, year) * 1000;
  203. #elseif cpp
  204. return untyped __global__.__hxcpp_utc_date(year,month,day,hour,min,sec)*1000.0 ;
  205. #else
  206. //TODO
  207. return 0.;
  208. #end
  209. }
  210. #end
  211. }