DateTools.hx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C)2005-2017 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. static var DAY_SHORT_NAMES = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  34. static var DAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  35. static var MONTH_SHORT_NAMES = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  36. static var MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  37. private static function __format_get( d : Date, e : String ) : String {
  38. return switch( e ){
  39. case "%":
  40. "%";
  41. case "a":
  42. DAY_SHORT_NAMES[d.getDay()];
  43. case "A":
  44. DAY_NAMES[d.getDay()];
  45. case "b","h":
  46. MONTH_SHORT_NAMES[d.getMonth()];
  47. case "B":
  48. MONTH_NAMES[d.getMonth()];
  49. case "C":
  50. untyped StringTools.lpad(Std.string(Std.int(d.getFullYear()/100)),"0",2);
  51. case "d":
  52. untyped StringTools.lpad(Std.string(d.getDate()),"0",2);
  53. case "D":
  54. __format(d,"%m/%d/%y");
  55. case "e":
  56. untyped Std.string(d.getDate());
  57. case "F":
  58. __format(d,"%Y-%m-%d");
  59. case "H","k":
  60. untyped StringTools.lpad(Std.string(d.getHours()),if( e == "H" ) "0" else " ",2);
  61. case "I","l":
  62. var hour = d.getHours()%12;
  63. untyped StringTools.lpad(Std.string(hour == 0 ? 12 : hour),if( e == "I" ) "0" else " ",2);
  64. case "m":
  65. untyped StringTools.lpad(Std.string(d.getMonth()+1),"0",2);
  66. case "M":
  67. untyped StringTools.lpad(Std.string(d.getMinutes()),"0",2);
  68. case "n":
  69. "\n";
  70. case "p":
  71. untyped if( d.getHours() > 11 ) "PM"; else "AM";
  72. case "r":
  73. __format(d,"%I:%M:%S %p");
  74. case "R":
  75. __format(d,"%H:%M");
  76. case "s":
  77. Std.string(Std.int(d.getTime()/1000));
  78. case "S":
  79. untyped StringTools.lpad(Std.string(d.getSeconds()),"0",2);
  80. case "t":
  81. "\t";
  82. case "T":
  83. __format(d,"%H:%M:%S");
  84. case "u":
  85. untyped{
  86. var t = d.getDay();
  87. if( t == 0 ) "7"; else Std.string(t);
  88. }
  89. case "w":
  90. untyped Std.string(d.getDay());
  91. case "y":
  92. untyped StringTools.lpad(Std.string(d.getFullYear()%100),"0",2);
  93. case "Y":
  94. untyped Std.string(d.getFullYear());
  95. default:
  96. throw "Date.format %"+e+"- not implemented yet.";
  97. }
  98. }
  99. private static function __format( d : Date, f : String ) : String {
  100. var r = new StringBuf();
  101. var p = 0;
  102. while( true ){
  103. var np = f.indexOf("%", p);
  104. if( np < 0 )
  105. break;
  106. r.addSub(f,p,np-p);
  107. r.add( __format_get(d, f.substr(np+1,1) ) );
  108. p = np+2;
  109. }
  110. r.addSub(f,p,f.length-p);
  111. return r.toString();
  112. }
  113. #end
  114. /**
  115. Format the date `d` according to the format `f`. The format is
  116. compatible with the `strftime` standard format, except that there is no
  117. support in Flash and JS for day and months names (due to lack of proper
  118. internationalization API). On Haxe/Neko/Windows, some formats are not
  119. supported.
  120. ```haxe
  121. var t = DateTools.format(Date.now(), "%Y-%m-%d_%H:%M:%S");
  122. // 2016-07-08_14:44:05
  123. var t = DateTools.format(Date.now(), "%r");
  124. // 02:44:05 PM
  125. var t = DateTools.format(Date.now(), "%T");
  126. // 14:44:05
  127. var t = DateTools.format(Date.now(), "%F");
  128. // 2016-07-08
  129. ```
  130. **/
  131. public static function format( d : Date, f : String ) : String {
  132. #if (neko && !(macro || interp))
  133. return new String(untyped date_format(d.__t, f.__s));
  134. #elseif php
  135. return untyped __call__("strftime",f,d.__t);
  136. #else
  137. return __format(d,f);
  138. #end
  139. }
  140. /**
  141. Returns the result of adding timestamp `t` to Date `d`.
  142. This is a convenience function for calling
  143. `Date.fromTime(d.getTime() + t)`.
  144. **/
  145. public static inline function delta( d : Date, t : Float ) : Date {
  146. return Date.fromTime( d.getTime() + t );
  147. }
  148. static var DAYS_OF_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  149. /**
  150. Returns the number of days in the month of Date `d`.
  151. This method handles leap years.
  152. **/
  153. public static function getMonthDays( d : Date ) : Int {
  154. var month = d.getMonth();
  155. var year = d.getFullYear();
  156. if (month != 1)
  157. return DAYS_OF_MONTH[month];
  158. var isB = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
  159. return if (isB) 29 else 28;
  160. }
  161. /**
  162. Converts a number of seconds to a timestamp.
  163. **/
  164. public static inline function seconds( n : Float ) : Float {
  165. return n * 1000.0;
  166. }
  167. /**
  168. Converts a number of minutes to a timestamp.
  169. **/
  170. #if as3 @:extern #end public static inline function minutes( n : Float ) : Float {
  171. return n * 60.0 * 1000.0;
  172. }
  173. /**
  174. Converts a number of hours to a timestamp.
  175. **/
  176. public static inline function hours( n : Float ) : Float {
  177. return n * 60.0 * 60.0 * 1000.0;
  178. }
  179. /**
  180. Converts a number of days to a timestamp.
  181. **/
  182. public static inline function days( n : Float ) : Float {
  183. return n * 24.0 * 60.0 * 60.0 * 1000.0;
  184. }
  185. /**
  186. Separate a date-time into several components
  187. **/
  188. public static function parse( t : Float ) {
  189. var s = t / 1000;
  190. var m = s / 60;
  191. var h = m / 60;
  192. return {
  193. ms : t % 1000,
  194. seconds : Std.int(s % 60),
  195. minutes : Std.int(m % 60),
  196. hours : Std.int(h % 24),
  197. days : Std.int(h / 24),
  198. };
  199. }
  200. /**
  201. Build a date-time from several components
  202. **/
  203. public static function make( o : { ms : Float, seconds : Int, minutes : Int, hours : Int, days : Int } ) {
  204. return o.ms + 1000.0 * (o.seconds + 60.0 * (o.minutes + 60.0 * (o.hours + 24.0 * o.days)));
  205. }
  206. #if (js || flash || php || cpp || python)
  207. /**
  208. Retrieve Unix timestamp value from Date components. Takes same argument sequence as the Date constructor.
  209. **/
  210. public static #if (js || flash || php) inline #end function makeUtc(year : Int, month : Int, day : Int, hour : Int, min : Int, sec : Int ):Float {
  211. #if (js || flash || python)
  212. return untyped Date.UTC(year, month, day, hour, min, sec);
  213. #elseif php
  214. return untyped __call__("gmmktime", hour, min, sec, month + 1, day, year) * 1000;
  215. #elseif cpp
  216. return untyped __global__.__hxcpp_utc_date(year,month,day,hour,min,sec)*1000.0 ;
  217. #else
  218. //TODO
  219. return 0.;
  220. #end
  221. }
  222. #end
  223. }