DateTools.hx 6.3 KB

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