Switching the old RPC methods to the websocketrpc
[KiwiIRC.git] / client / src / helpers / formatdate.js
1 var formatDate = (function() {
2 /*
3 Modified version of date.format.js
4 https://github.com/jacwright/date.format
5 */
6 var locale_init = false, // Once the loales have been loaded, this is set to true
7 shortMonths, longMonths, shortDays, longDays;
8
9 // defining patterns
10 var replaceChars = {
11 // Day
12 d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
13 D: function() { return Date.shortDays[this.getDay()]; },
14 j: function() { return this.getDate(); },
15 l: function() { return Date.longDays[this.getDay()]; },
16 N: function() { return this.getDay() + 1; },
17 S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
18 w: function() { return this.getDay(); },
19 z: function() { var d = new Date(this.getFullYear(),0,1); return Math.ceil((this - d) / 86400000); }, // Fixed now
20 // Week
21 W: function() { var d = new Date(this.getFullYear(), 0, 1); return Math.ceil((((this - d) / 86400000) + d.getDay() + 1) / 7); }, // Fixed now
22 // Month
23 F: function() { return Date.longMonths[this.getMonth()]; },
24 m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
25 M: function() { return Date.shortMonths[this.getMonth()]; },
26 n: function() { return this.getMonth() + 1; },
27 t: function() { var d = new Date(); return new Date(d.getFullYear(), d.getMonth(), 0).getDate(); }, // Fixed now, gets #days of date
28 // Year
29 L: function() { var year = this.getFullYear(); return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)); }, // Fixed now
30 o: function() { var d = new Date(this.valueOf()); d.setDate(d.getDate() - ((this.getDay() + 6) % 7) + 3); return d.getFullYear();}, //Fixed now
31 Y: function() { return this.getFullYear(); },
32 y: function() { return ('' + this.getFullYear()).substr(2); },
33 // Time
34 a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
35 A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
36 B: function() { return Math.floor((((this.getUTCHours() + 1) % 24) + this.getUTCMinutes() / 60 + this.getUTCSeconds() / 3600) * 1000 / 24); }, // Fixed now
37 g: function() { return this.getHours() % 12 || 12; },
38 G: function() { return this.getHours(); },
39 h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
40 H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
41 i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
42 s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
43 u: function() { var m = this.getMilliseconds(); return (m < 10 ? '00' : (m < 100 ? '0' : '')) + m; },
44 // Timezone
45 e: function() { return "Not Yet Supported"; },
46 I: function() {
47 var DST = null;
48 for (var i = 0; i < 12; ++i) {
49 var d = new Date(this.getFullYear(), i, 1);
50 var offset = d.getTimezoneOffset();
51
52 if (DST === null) DST = offset;
53 else if (offset < DST) { DST = offset; break; }
54 else if (offset > DST) break;
55 }
56 return (this.getTimezoneOffset() == DST) | 0;
57 },
58 O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
59 P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':00'; }, // Fixed now
60 T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
61 Z: function() { return -this.getTimezoneOffset() * 60; },
62 // Full Date/Time
63 c: function() { return this.format("Y-m-d\\TH:i:sP"); }, // Fixed now
64 r: function() { return this.toString(); },
65 U: function() { return this.getTime() / 1000; }
66 };
67
68
69 var initLocaleFormats = function() {
70 shortMonths = [
71 _kiwi.global.i18n.translate('client.libs.date_format.short_months.january').fetch(),
72 _kiwi.global.i18n.translate('client.libs.date_format.short_months.february').fetch(),
73 _kiwi.global.i18n.translate('client.libs.date_format.short_months.march').fetch(),
74 _kiwi.global.i18n.translate('client.libs.date_format.short_months.april').fetch(),
75 _kiwi.global.i18n.translate('client.libs.date_format.short_months.may').fetch(),
76 _kiwi.global.i18n.translate('client.libs.date_format.short_months.june').fetch(),
77 _kiwi.global.i18n.translate('client.libs.date_format.short_months.july').fetch(),
78 _kiwi.global.i18n.translate('client.libs.date_format.short_months.august').fetch(),
79 _kiwi.global.i18n.translate('client.libs.date_format.short_months.september').fetch(),
80 _kiwi.global.i18n.translate('client.libs.date_format.short_months.october').fetch(),
81 _kiwi.global.i18n.translate('client.libs.date_format.short_months.november').fetch(),
82 _kiwi.global.i18n.translate('client.libs.date_format.short_months.december').fetch()
83 ];
84 longMonths = [
85 _kiwi.global.i18n.translate('client.libs.date_format.long_months.january').fetch(),
86 _kiwi.global.i18n.translate('client.libs.date_format.long_months.february').fetch(),
87 _kiwi.global.i18n.translate('client.libs.date_format.long_months.march').fetch(),
88 _kiwi.global.i18n.translate('client.libs.date_format.long_months.april').fetch(),
89 _kiwi.global.i18n.translate('client.libs.date_format.long_months.may').fetch(),
90 _kiwi.global.i18n.translate('client.libs.date_format.long_months.june').fetch(),
91 _kiwi.global.i18n.translate('client.libs.date_format.long_months.july').fetch(),
92 _kiwi.global.i18n.translate('client.libs.date_format.long_months.august').fetch(),
93 _kiwi.global.i18n.translate('client.libs.date_format.long_months.september').fetch(),
94 _kiwi.global.i18n.translate('client.libs.date_format.long_months.october').fetch(),
95 _kiwi.global.i18n.translate('client.libs.date_format.long_months.november').fetch(),
96 _kiwi.global.i18n.translate('client.libs.date_format.long_months.december').fetch()
97 ];
98 shortDays = [
99 _kiwi.global.i18n.translate('client.libs.date_format.short_days.monday').fetch(),
100 _kiwi.global.i18n.translate('client.libs.date_format.short_days.tuesday').fetch(),
101 _kiwi.global.i18n.translate('client.libs.date_format.short_days.wednesday').fetch(),
102 _kiwi.global.i18n.translate('client.libs.date_format.short_days.thursday').fetch(),
103 _kiwi.global.i18n.translate('client.libs.date_format.short_days.friday').fetch(),
104 _kiwi.global.i18n.translate('client.libs.date_format.short_days.saturday').fetch(),
105 _kiwi.global.i18n.translate('client.libs.date_format.short_days.sunday').fetch()
106 ];
107 longDays = [
108 _kiwi.global.i18n.translate('client.libs.date_format.long_days.monday').fetch(),
109 _kiwi.global.i18n.translate('client.libs.date_format.long_days.tuesday').fetch(),
110 _kiwi.global.i18n.translate('client.libs.date_format.long_days.wednesday').fetch(),
111 _kiwi.global.i18n.translate('client.libs.date_format.long_days.thursday').fetch(),
112 _kiwi.global.i18n.translate('client.libs.date_format.long_days.friday').fetch(),
113 _kiwi.global.i18n.translate('client.libs.date_format.long_days.saturday').fetch(),
114 _kiwi.global.i18n.translate('client.libs.date_format.long_days.sunday').fetch()
115 ];
116
117 locale_init = true;
118 };
119 /* End of date.format */
120
121
122 // Finally.. the actuall formatDate function
123 return function(working_date, format) {
124 if (!locale_init)
125 initLocaleFormats();
126
127 working_date = working_date || new Date();
128 format = format || _kiwi.global.i18n.translate('client_date_format').fetch();
129
130 return format.replace(/(\\?)(.)/g, function(_, esc, chr) {
131 return (esc === '' && replaceChars[chr]) ? replaceChars[chr].call(working_date) : chr;
132 });
133 };
134 })();