php.js 9.2 KB

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