php.js 8.2 KB

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