php.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.php"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.php"] = true;
  8. dojo.provide("dojox.date.php");
  9. dojo.require("dojo.date");
  10. dojo.require("dojox.string.tokenize");
  11. dojox.date.php.format = function(/*Date*/ date, /*String*/ format){
  12. // summary: Get a formatted string for a given date object
  13. var df = new dojox.date.php.DateFormat(format);
  14. return df.format(date);
  15. }
  16. dojox.date.php.DateFormat = function(/*String*/ format){
  17. // summary: Format the internal date object
  18. if(!this.regex){
  19. var keys = [];
  20. for(var key in this.constructor.prototype){
  21. if(dojo.isString(key) && key.length == 1 && dojo.isFunction(this[key])){
  22. keys.push(key);
  23. }
  24. }
  25. this.constructor.prototype.regex = new RegExp("(?:(\\\\.)|([" + keys.join("") + "]))", "g");
  26. }
  27. var replacements = [];
  28. this.tokens = dojox.string.tokenize(format, this.regex, function(escape, token, i){
  29. if(token){
  30. replacements.push([i, token]);
  31. return token;
  32. }
  33. if(escape){
  34. return escape.charAt(1);
  35. }
  36. });
  37. this.replacements = replacements;
  38. }
  39. dojo.extend(dojox.date.php.DateFormat, {
  40. weekdays: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  41. weekdays_3: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  42. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  43. months_3: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  44. monthdays: [31,28,31,30,31,30,31,31,30,31,30,31],
  45. format: function(/*Date*/ date){
  46. this.date = date;
  47. for(var i = 0, replacement; replacement = this.replacements[i]; i++){
  48. this.tokens[replacement[0]] = this[replacement[1]]();
  49. }
  50. return this.tokens.join("");
  51. },
  52. // Day
  53. d: function(){
  54. // summary: Day of the month, 2 digits with leading zeros
  55. var j = this.j();
  56. return (j.length == 1) ? "0" + j : j;
  57. },
  58. D: function(){
  59. // summary: A textual representation of a day, three letters
  60. return this.weekdays_3[this.date.getDay()];
  61. },
  62. j: function(){
  63. // summary: Day of the month without leading zeros
  64. return this.date.getDate() + "";
  65. },
  66. l: function(){
  67. // summary: A full textual representation of the day of the week
  68. return this.weekdays[this.date.getDay()];
  69. },
  70. N: function(){
  71. // summary: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
  72. var w = this.w();
  73. return (!w) ? 7 : w;
  74. },
  75. S: function(){
  76. // summary: English ordinal suffix for the day of the month, 2 characters
  77. switch(this.date.getDate()){
  78. case 11: case 12: case 13: return "th";
  79. case 1: case 21: case 31: return "st";
  80. case 2: case 22: return "nd";
  81. case 3: case 23: return "rd";
  82. default: return "th";
  83. }
  84. },
  85. w: function(){
  86. // summary: Numeric representation of the day of the week
  87. return this.date.getDay() + "";
  88. },
  89. z: function(){
  90. // summary: The day of the year (starting from 0)
  91. var millis = this.date.getTime() - new Date(this.date.getFullYear(), 0, 1).getTime();
  92. return Math.floor(millis/86400000) + "";
  93. },
  94. // Week
  95. W: function(){
  96. // summary: ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
  97. var week;
  98. var jan1_w = new Date(this.date.getFullYear(), 0, 1).getDay() + 1;
  99. var w = this.date.getDay() + 1;
  100. var z = parseInt(this.z());
  101. if(z <= (8 - jan1_w) && jan1_w > 4){
  102. var last_year = new Date(this.date.getFullYear() - 1, this.date.getMonth(), this.date.getDate());
  103. if(jan1_w == 5 || (jan1_w == 6 && dojo.date.isLeapYear(last_year))){
  104. week = 53;
  105. }else{
  106. week = 52;
  107. }
  108. }else{
  109. var i;
  110. if(Boolean(this.L())){
  111. i = 366;
  112. }else{
  113. i = 365;
  114. }
  115. if((i - z) < (4 - w)){
  116. week = 1;
  117. }else{
  118. var j = z + (7 - w) + (jan1_w - 1);
  119. week = Math.ceil(j / 7);
  120. if(jan1_w > 4){
  121. --week;
  122. }
  123. }
  124. }
  125. return week;
  126. },
  127. // Month
  128. F: function(){
  129. // summary: A full textual representation of a month, such as January or March
  130. return this.months[this.date.getMonth()];
  131. },
  132. m: function(){
  133. // summary: Numeric representation of a month, with leading zeros
  134. var n = this.n();
  135. return (n.length == 1) ? "0" + n : n;
  136. },
  137. M: function(){
  138. // summary: A short textual representation of a month, three letters
  139. return this.months_3[this.date.getMonth()];
  140. },
  141. n: function(){
  142. // summary: Numeric representation of a month, without leading zeros
  143. return this.date.getMonth() + 1 + "";
  144. },
  145. t: function(){
  146. // summary: Number of days in the given month
  147. return (Boolean(this.L()) && this.date.getMonth() == 1) ? 29 : this.monthdays[this.getMonth()];
  148. },
  149. // Year
  150. L: function(){
  151. // summary: Whether it's a leap year
  152. return (dojo.date.isLeapYear(this.date)) ? "1" : "0";
  153. },
  154. o: function(){
  155. // summary:
  156. // ISO-8601 year number. This has the same value as Y, except that if
  157. // the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
  158. // TODO: Figure out what this means
  159. },
  160. Y: function(){
  161. // summary: A full numeric representation of a year, 4 digits
  162. return this.date.getFullYear() + "";
  163. },
  164. y: function(){
  165. // summary: A two digit representation of a year
  166. return this.Y().slice(-2);
  167. },
  168. // Time
  169. a: function(){
  170. // summary: Lowercase Ante meridiem and Post meridiem
  171. return this.date.getHours() >= 12 ? "pm" : "am";
  172. },
  173. b: function(){
  174. // summary: Uppercase Ante meridiem and Post meridiem
  175. return this.a().toUpperCase();
  176. },
  177. B: function(){
  178. // summary:
  179. // Swatch Internet time
  180. // A day is 1,000 beats. All time is measured from GMT + 1
  181. var off = this.date.getTimezoneOffset() + 60;
  182. var secs = (this.date.getHours() * 3600) + (this.date.getMinutes() * 60) + this.getSeconds() + (off * 60);
  183. var beat = Math.abs(Math.floor(secs / 86.4) % 1000) + "";
  184. while(beat.length < 2) beat = "0" + beat;
  185. return beat;
  186. },
  187. g: function(){
  188. // summary: 12-hour format of an hour without leading zeros
  189. return (this.date.getHours() > 12) ? this.date.getHours() - 12 + "" : this.date.getHours() + "";
  190. },
  191. G: function(){
  192. // summary: 24-hour format of an hour without leading zeros
  193. return this.date.getHours() + "";
  194. },
  195. h: function(){
  196. // summary: 12-hour format of an hour with leading zeros
  197. var g = this.g();
  198. return (g.length == 1) ? "0" + g : g;
  199. },
  200. H: function(){
  201. // summary: 24-hour format of an hour with leading zeros
  202. var G = this.G();
  203. return (G.length == 1) ? "0" + G : G;
  204. },
  205. i: function(){
  206. // summary: Minutes with leading zeros
  207. var mins = this.date.getMinutes() + "";
  208. return (mins.length == 1) ? "0" + mins : mins;
  209. },
  210. s: function(){
  211. // summary: Seconds, with leading zeros
  212. var secs = this.date.getSeconds() + "";
  213. return (secs.length == 1) ? "0" + secs : secs;
  214. },
  215. // Timezone
  216. e: function(){
  217. // summary: Timezone identifier (added in PHP 5.1.0)
  218. return dojo.date.getTimezoneName(this.date);
  219. },
  220. I: function(){
  221. // summary: Whether or not the date is in daylight saving time
  222. // TODO: Can dojo.date do this?
  223. },
  224. O: function(){
  225. // summary: Difference to Greenwich time (GMT) in hours
  226. var off = Math.abs(this.date.getTimezoneOffset());
  227. var hours = Math.floor(off / 60) + "";
  228. var mins = (off % 60) + "";
  229. if(hours.length == 1) hours = "0" + hours;
  230. if(mins.length == 1) hours = "0" + mins;
  231. return ((this.date.getTimezoneOffset() < 0) ? "+" : "-") + hours + mins;
  232. },
  233. P: function(){
  234. // summary: Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
  235. var O = this.O();
  236. return O.substring(0, 2) + ":" + O.substring(2, 4);
  237. },
  238. T: function(){
  239. // summary: Timezone abbreviation
  240. // Guess...
  241. return this.e().substring(0, 3);
  242. },
  243. Z: function(){
  244. // summary:
  245. // Timezone offset in seconds. The offset for timezones west of UTC is always negative,
  246. // and for those east of UTC is always positive.
  247. return this.date.getTimezoneOffset() * -60;
  248. },
  249. // Full Date/Time
  250. c: function(){
  251. // summary: ISO 8601 date (added in PHP 5)
  252. return this.Y() + "-" + this.m() + "-" + this.d() + "T" + this.h() + ":" + this.i() + ":" + this.s() + this.P();
  253. },
  254. r: function(){
  255. // summary: RFC 2822 formatted date
  256. return this.D() + ", " + this.d() + " " + this.M() + " " + this.Y() + " " + this.H() + ":" + this.i() + ":" + this.s() + " " + this.O();
  257. },
  258. U: function(){
  259. // summary: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  260. return Math.floor(this.date.getTime() / 1000);
  261. }
  262. });
  263. }