DateTools.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. The DateTools class contains some extra functionalities for [Date]
  27. manipulation. It's stored in a different class in order to prevent
  28. the standard [Date] of being bloated and thus increasing the size of
  29. each application using it.
  30. **/
  31. class DateTools {
  32. #if php
  33. #elseif neko
  34. static var date_format = neko.Lib.load("std","date_format",2);
  35. #else
  36. private static function __format_get( d : Date, e : String ) : String {
  37. return switch( e ){
  38. case "%":
  39. "%";
  40. case "C":
  41. untyped StringTools.lpad(Std.string(Std.int(d.getFullYear()/100)),"0",2);
  42. case "d":
  43. untyped StringTools.lpad(Std.string(d.getDate()),"0",2);
  44. case "D":
  45. __format(d,"%m/%d/%y");
  46. case "e":
  47. untyped Std.string(d.getDate());
  48. case "H","k":
  49. untyped StringTools.lpad(Std.string(d.getHours()),if( e == "H" ) "0" else " ",2);
  50. case "I","l":
  51. var hour = d.getHours()%12;
  52. untyped StringTools.lpad(Std.string(hour == 0 ? 12 : hour),if( e == "I" ) "0" else " ",2);
  53. case "m":
  54. untyped StringTools.lpad(Std.string(d.getMonth()+1),"0",2);
  55. case "M":
  56. untyped StringTools.lpad(Std.string(d.getMinutes()),"0",2);
  57. case "n":
  58. "\n";
  59. case "p":
  60. untyped if( d.getHours() > 11 ) "PM"; else "AM";
  61. case "r":
  62. __format(d,"%I:%M:%S %p");
  63. case "R":
  64. __format(d,"%H:%M");
  65. case "s":
  66. Std.string(Std.int(d.getTime()/1000));
  67. case "S":
  68. untyped StringTools.lpad(Std.string(d.getSeconds()),"0",2);
  69. case "t":
  70. "\t";
  71. case "T":
  72. __format(d,"%H:%M:%S");
  73. case "u":
  74. untyped{
  75. var t = d.getDay();
  76. if( t == 0 ) "7"; else Std.string(t);
  77. }
  78. case "w":
  79. untyped Std.string(d.getDay());
  80. case "y":
  81. untyped StringTools.lpad(Std.string(d.getFullYear()%100),"0",2);
  82. case "Y":
  83. untyped Std.string(d.getFullYear());
  84. default:
  85. throw "Date.format %"+e+"- not implemented yet.";
  86. }
  87. }
  88. private static function __format( d : Date, f : String ) : String {
  89. var r = new StringBuf();
  90. var p = 0;
  91. while( true ){
  92. var np = f.indexOf("%", p);
  93. if( np < 0 )
  94. break;
  95. r.addSub(f,p,np-p);
  96. r.add( __format_get(d, f.substr(np+1,1) ) );
  97. p = np+2;
  98. }
  99. r.addSub(f,p,f.length-p);
  100. return r.toString();
  101. }
  102. #end
  103. /**
  104. Format the date [d] according to the format [f]. The format
  105. is compatible with the [strftime] standard format, except that there
  106. is no support in Flash and JS for day and months names (due to lack
  107. of proper internationalization API). On haXe/Neko/Windows, some
  108. formats are not supported.
  109. **/
  110. public static function format( d : Date, f : String ) : String {
  111. #if neko
  112. return new String(untyped date_format(d.__t, f.__s));
  113. #elseif php
  114. return untyped __php__("strftime($f,$d->__t)");
  115. #else
  116. return __format(d,f);
  117. #end
  118. }
  119. /**
  120. Returns a Date which time has been changed by [t] milliseconds.
  121. **/
  122. public static function delta( d : Date, t : Float ) : Date {
  123. return Date.fromTime( d.getTime() + t );
  124. }
  125. static var DAYS_OF_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  126. /**
  127. Returns the number of days in a month
  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. Convert a number of seconds to a date-time
  139. **/
  140. public static function seconds( n : Float ) : Float {
  141. return n * 1000.0;
  142. }
  143. /**
  144. Convert a number of minutes to a date-time
  145. **/
  146. public static function minutes( n : Float ) : Float {
  147. return n * 60.0 * 1000.0;
  148. }
  149. /**
  150. Convert a number of hours to a date-time
  151. **/
  152. public static function hours( n : Float ) : Float {
  153. return n * 60.0 * 60.0 * 1000.0;
  154. }
  155. /**
  156. Convert a number of days to a date-time
  157. **/
  158. public static 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. }