1. /*
  2. DateFormat.js: Localizes JavaScript Dates. Copyright (C) 1999-2003 Jan Wessely
  3. <mailto:info@jawe.net>
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. the Software, and to permit persons to whom the Software is furnished to do so,
  10. subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. Created: 27 Dec 1999
  23. Last modified: 25 Sep 2000
  24. */
  25.  
  26. // constructors ***************************************************************
  27.  
  28. // DateFormat()
  29. // DateFormat(string pattern, opt Locale locale)
  30. // DateFormat(number dateStyle, opt number timeStyle, opt string locale)
  31.  
  32. function DateFormat()
  33. {
  34. var argv = DateFormat.arguments;
  35. var argc = argv.length;
  36.  
  37. if(argc == 0)
  38. {
  39. this.pattern = "";
  40. this.locale = Locale.availableLocales[Locale.DEFAULT];
  41. }
  42. else if(typeof argv[0] == "string")
  43. {
  44. this.pattern = argv[0];
  45. this.locale = (argc >= 2) ? argv[1] : Locale.availableLocales[Locale.DEFAULT];
  46. }
  47. else
  48. {
  49. this.locale = argc >= 3 && argv[2] ? argv[2] : Locale.availableLocales[Locale.DEFAULT];
  50. this.setStyle(argv[0], argc > 1 ? argv[1] : DateFormat.NONE);
  51. }
  52. }
  53.  
  54. // methods ********************************************************************
  55.  
  56. function /*string*/ _DateFormat_format(/*Date or string*/ date)
  57. {
  58. //sanity check
  59. if(!date)
  60. return "";
  61. if(typeof date == "string")
  62. date = new Date(date);
  63.  
  64. var s = "";
  65. var pos = 0;
  66. while(pos < this.pattern.length)
  67. {
  68. var c = this.pattern.charAt(pos);
  69. var length;
  70. var symbol = DateFormat.SYMBOLS[c];
  71.  
  72. if(symbol)
  73. {
  74. length = this._consume(pos, c);
  75. s += symbol.format(date, length, this.locale);
  76. }
  77. else if(c == "'")
  78. {
  79. if(pos >= this.pattern.length - 1) // last char
  80. {
  81. length = 1;
  82. s += c;
  83. }
  84. else if(this.pattern.charAt(pos + 1) != "'") // delim, not escape
  85. {
  86. var eos = this.pattern.indexOf("'", ++pos);
  87. if(eos == -1) eos = this.pattern.length; // don't be anal
  88. length = (eos - pos) + 1;
  89. s += this.pattern.substring(pos, eos);
  90. }
  91. else // escape
  92. {
  93. length = 2;
  94. s += c;
  95. }
  96. }
  97. else // treat any other char as literal
  98. {
  99. length = 1;
  100. s += c;
  101. }
  102.  
  103. pos += length;
  104. } // while
  105.  
  106. return s;
  107. }
  108.  
  109. function _DateFormat_setStyle(/*number*/ dateStyle, /*number*/ timeStyle)
  110. {
  111. dateStyle = parseInt(dateStyle);
  112. timeStyle = parseInt(timeStyle);
  113.  
  114. this.pattern = this.locale.datePattern[dateStyle];
  115. if(this.pattern && timeStyle)
  116. this.pattern += " ";
  117. this.pattern += this.locale.timePattern[timeStyle];
  118. }
  119.  
  120. // private methods ************************************************************
  121.  
  122. // returns number of same chars as c in this.pattern starting with pos
  123. function _DateFormat_consume(pos, c)
  124. {
  125. var i = pos;
  126. while(++i < this.pattern.length)
  127. if(this.pattern.charAt(i) != c)
  128. break;
  129. return i - pos;
  130. }
  131.  
  132. // pads a number with leading zeros so that it has at least l digits
  133. function _DateFormat_formatNumber(n, l, locale)
  134. {
  135. var s = "";
  136. var digits = (new String(n)).length;
  137. var zeros = Math.max(0, l - digits);
  138. for(var i = 0; i < zeros; i++)
  139. s += "0";
  140. return s + n;
  141. }
  142.  
  143. function _DateFormat_formatEra(year, l, locale)
  144. {
  145. var era = year < 0 ? DateFormat.BC : DateFormat.AD;
  146. return locale.era[era];
  147. }
  148.  
  149. function _DateFormat_formatMonth(month, l, locale)
  150. {
  151. if(l < 3)
  152. return DateFormat._formatNumber(month + 1, l, locale);
  153. else
  154. {
  155. var months = l == 3 ? locale.shortMonths : locale.longMonths;
  156. return months[month];
  157. }
  158. }
  159.  
  160. function _DateFormat_formatWeekday(weekday, l, locale)
  161. {
  162. var weekdays = l < 4 ? locale.shortWeekdays : locale.longWeekdays;
  163. return weekdays[weekday];
  164. }
  165.  
  166. function _DateFormat_formatAMPM(hour, l, locale)
  167. {
  168. var ampm = hour <= 12 && hour > 0 ? DateFormat.AM : DateFormat.PM;
  169. return locale.amPm[ampm];
  170. }
  171.  
  172. function _DateFormat_formatTimezone(offset, l, locale)
  173. {
  174. // todo localized Timezone names
  175. var diff = Math.abs(offset);
  176. var hours = DateFormat._formatNumber(parseInt(diff / 60), 2);
  177. var minutes = DateFormat._formatNumber(diff % 60, 2);
  178. return "GMT" + (offset > 0 ? "-" : "+") + hours + ":" + minutes;
  179. }
  180.  
  181. function _DateFormat_formatMillis(date, l, locale)
  182. {
  183. var date2 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  184. return DateFormat._formatNumber(date.getTime() - date2.getTime(), l, locale);
  185. }
  186.  
  187. function _DateFormat_formatYear(date, l, locale)
  188. {
  189. var year = date.getYear();
  190. var fullYear = date.getFullYear();
  191. if(year != fullYear && year == 100)
  192. year = 0;
  193. var y = l > 2 ? fullYear : year;
  194. return DateFormat._formatNumber(y, l, locale);
  195. }
  196.  
  197. DateFormat.NONE = 0;
  198. DateFormat.SHORT = 1;
  199. DateFormat.MEDIUM = 2;
  200. DateFormat.LONG = 3;
  201. DateFormat.FULL = 4;
  202. DateFormat.NSTYLES = 5;
  203.  
  204. DateFormat.BC = 0;
  205. DateFormat.AD = 1;
  206.  
  207. DateFormat.AM = 0;
  208. DateFormat.PM = 1;
  209.  
  210. DateFormat._formatNumber = _DateFormat_formatNumber;
  211. DateFormat._formatEra = _DateFormat_formatEra;
  212. DateFormat._formatMonth = _DateFormat_formatMonth;
  213. DateFormat._formatWeekday = _DateFormat_formatWeekday;
  214. DateFormat._formatAmPm = _DateFormat_formatAMPM;
  215. DateFormat._formatTimzone = _DateFormat_formatTimezone;
  216. DateFormat._formatMillis = _DateFormat_formatMillis;
  217. DateFormat._formatYear = _DateFormat_formatYear;
  218.  
  219. DateFormat.SYMBOLS = new Object();
  220. DateFormat.SYMBOLS["G"] = new _DFSymbol(DateFormat._formatEra, "date.getFullYear()");
  221. DateFormat.SYMBOLS["y"] = new _DFSymbol(DateFormat._formatYear, "date");
  222. DateFormat.SYMBOLS["M"] = new _DFSymbol(DateFormat._formatMonth, "date.getMonth()");
  223. DateFormat.SYMBOLS["d"] = new _DFSymbol(DateFormat._formatNumber, "date.getDate()");
  224. DateFormat.SYMBOLS["h"] = new _DFSymbol(DateFormat._formatNumber, "date.getHours() > 12 ? date.getHours() - 12 : (date.getHours() > 0 ? date.getHours() : 12)");
  225. DateFormat.SYMBOLS["H"] = new _DFSymbol(DateFormat._formatNumber, "date.getHours()");
  226. DateFormat.SYMBOLS["m"] = new _DFSymbol(DateFormat._formatNumber, "date.getMinutes()");
  227. DateFormat.SYMBOLS["s"] = new _DFSymbol(DateFormat._formatNumber, "date.getSeconds()");
  228. DateFormat.SYMBOLS["S"] = new _DFSymbol(DateFormat._formatMillis, "date");
  229. DateFormat.SYMBOLS["E"] = new _DFSymbol(DateFormat._formatWeekday, "date.getDay()");
  230. DateFormat.SYMBOLS["D"] = new _DFSymbol();
  231. DateFormat.SYMBOLS["F"] = new _DFSymbol();
  232. DateFormat.SYMBOLS["w"] = new _DFSymbol();
  233. DateFormat.SYMBOLS["W"] = new _DFSymbol();
  234. DateFormat.SYMBOLS["a"] = new _DFSymbol(DateFormat._formatAmPm, "date.getHours()");
  235. DateFormat.SYMBOLS["k"] = new _DFSymbol(DateFormat._formatNumber, "date.getHours() + 1");
  236. DateFormat.SYMBOLS["K"] = new _DFSymbol(DateFormat._formatNumber, "date.getHours() > 11 ? date.getHours() - 12 : date.getHours()");
  237. DateFormat.SYMBOLS["z"] = new _DFSymbol(DateFormat._formatTimezone, "date.getTimezoneOffset()");
  238.  
  239. DateFormat.prototype.format = _DateFormat_format;
  240. DateFormat.prototype.setStyle = _DateFormat_setStyle;
  241. DateFormat.prototype._consume = _DateFormat_consume;
  242.  
  243. // _DFSymbol ***********************************************************
  244.  
  245. // helper object
  246. function _DFSymbol(func, value)
  247. {
  248. this.func = func;
  249. this.value = value;
  250. }
  251.  
  252. function _DFSymbol_format(date, l, locale)
  253. {
  254. return this.func ? this.func(eval(this.value), l, locale) : "";
  255. }
  256.  
  257. _DFSymbol.prototype.format = _DFSymbol_format;
  258.  
  259. // Locale *******************************************************************
  260.  
  261. function Locale(/*string*/ name, /*opt Locale or string*/ parent)
  262. {
  263. this.name = name;
  264. Locale.availableLocales[this.name] = this;
  265. this.shortWeekdays = new Array("", "", "", "", "", "", "");
  266. this.longWeekdays = new Array("", "", "", "", "", "", "");
  267. this.shortMonths = new Array("", "", "", "", "", "", "", "", "", "", "", "");
  268. this.longMonths = new Array("", "", "", "", "", "", "", "", "", "", "", "");
  269. this.era = new Array("", "");
  270. this.amPm = new Array("", "");
  271. this.datePattern = new Array("", "", "", "", "");
  272. this.timePattern = new Array("", "", "", "", "");
  273.  
  274. if(parent)
  275. {
  276. if(typeof parent == "string")
  277. parent = Locale.availableLocales[parent];
  278. copy(parent.shortWeekdays, this.shortWeekdays);
  279. copy(parent.longWeekdays, this.longWeekdays);
  280. copy(parent.shortMonths, this.shortMonths);
  281. copy(parent.longMonths, this.longMonths);
  282. copy(parent.era, this.era);
  283. copy(parent.amPm, this.amPm);
  284. copy(parent.datePattern, this.datePattern);
  285. copy(parent.timePattern, this.timePattern);
  286. }
  287. }
  288.  
  289. function _Locale_toString()
  290. {
  291. return this.name;
  292. }
  293.  
  294. Locale.prototype.toString = _Locale_toString;
  295. Locale.availableLocales = new Object();
  296.  
  297. // helper functions ***********************************************************
  298.  
  299. // shallow copy
  300. function /*Object*/ copy(/*object*/ src, /*opt object*/ dst)
  301. {
  302. if(!dst) dst = new Object();
  303. for(var prop in src)
  304. dst[prop] = src[prop];
  305. return dst;
  306. }
  307.  
  308. // Locale definitions *********************************************************
  309.  
  310. var tmp;
  311. Locale.DEFAULT = "en";
  312.  
  313. tmp = new Locale("en");
  314. tmp.shortWeekdays = new Array("Su", "Mo", "Tu", "We", "Fr", "Sa");
  315. tmp.longWeekdays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Friday", "Saturday");
  316. tmp.shortMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  317. tmp.longMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  318. tmp.era = new Array("BC", "AD");
  319. tmp.amPm = new Array("AM", "PM");
  320. tmp.datePattern = new Array("", "M/d/yy", "MMM d, yyyy", "MMMM d, yyyy", "EEEE, MMMM d, yyyy");
  321. tmp.timePattern = new Array("", "h:mm a", "h:mm:ss a", "h:mm:ss a z", "h:mm:ss a z");
  322.  
  323. tmp = new Locale("en_GB", "en");
  324. tmp.datePattern = new Array("", "dd/MM/yy", "dd-MMM-yyyy", "dd MMMM yyyy", "dd MMMM yyyy");
  325. tmp.timePattern = new Array("", "HH:mm", "HH:mm:ss", "HH:mm:ss z", "HH:mm:ss o''cloc'k' z");
  326.  
  327. tmp = new Locale("de");
  328. tmp.shortWeekdays = new Array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");
  329. tmp.longWeekdays = new Array("Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag");
  330. tmp.shortMonths = new Array("Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez");
  331. tmp.longMonths = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
  332. tmp.era = new Array("v. Chr.", "n. Chr.");
  333. tmp.datePattern = new Array("", "dd.MM.yy", "dd.MM.yyyy", "dd. MMMM yyyy", "d. MMMM yyyy");
  334. tmp.timePattern = new Array("", "HH:mm", "HH:mm:ss", "HH:mm:ss z", "HH:mm:ss U'h'r z");
  335.  
  336. tmp = new Locale("de_AT", "de");
  337. tmp.shortMonths = new Array("J&auml;n", "Feb", "M&auml;r", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez");
  338. tmp.longMonths = new Array("J&auml;nner", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
  339.  
  340. tmp = new Locale("fr");
  341. tmp.shortWeekdays = new Array("dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam.");
  342. tmp.longWeekdays = new Array("dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi");
  343. tmp.shortMonths = new Array("janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc.");
  344. tmp.longMonths = new Array("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre");
  345. tmp.era = new Array("BC", "ap. J.-C.");
  346. tmp.datePattern = new Array("", "dd/MM/yy", "d MMM yy", "d MMMM yyyy", "EEEE d MMMM yyyy");
  347. tmp.timePattern = new Array("", "HH:mm", "HH:mm:ss", "HH:mm:ss z", "HH 'h' mm z");
  348.  
  349. tmp = new Locale("it");
  350. tmp.shortWeekdays = new Array("dom", "lun", "mar", "mer", "gio", "ven", "sab");
  351. tmp.longWeekdays = new Array("domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato");
  352. tmp.shortMonths = new Array("gen", "feb", "mar", "apr", "mag", "giu", "lug", "ago", "set", "ott", "nov", "dic");
  353. tmp.longMonths = new Array("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre");
  354. tmp.era = new Array("BC", "dopo Cristo");
  355. tmp.datePattern = new Array("", "dd/MM/yy", "d-MMM-yy", "d MMMM yyyy", "EEEE d MMMM yyyy");
  356. tmp.timePattern = new Array("", "H.mm", "H.mm.ss", "HH.mm.ss z", "HH.mm.ss z");
  357.  
  358. tmp = new Locale("es");
  359. tmp.shortWeekdays = new Array("dom", "lun", "mar", "mié", "jue", "vie", "sáb");
  360. tmp.longWeekdays = new Array("domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado");
  361. tmp.shortMonths = new Array("ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic");
  362. tmp.longMonths = new Array("enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre");
  363. tmp.era = new Array("BC", "AD");
  364. tmp.datePattern = new Array("", "d/MM/yy", "d-MMM-yy", "d 'de' MMMM 'de' yyyy", "EEEE d 'de' MMMM 'de' yyyy");
  365. tmp.timePattern = new Array("", "H:mm", "H:mm:ss", "HH:mm:ss z", "HH'H'mm'' z");
  366.  
  367. tmp = new Locale("pt");
  368. tmp.shortWeekdays = new Array("Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb");
  369. tmp.longWeekdays = new Array("Domingo", "Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira", "Sábado");
  370. tmp.shortMonths = new Array("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez");
  371. tmp.longMonths = new Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro");
  372. tmp.era = new Array("BC", "AD");
  373. tmp.datePattern = new Array("", "dd-MM-yy", "d/MMM/yy", "d 'de' MMMM 'de' yyyy", "EEEE, d 'de' MMMM 'de' yyyy");
  374. tmp.timePattern = new Array("", "H:mm", "H:mm:ss", "HH:mm:ss z", "HH'H'mm'm' z");