Function: strftime version 124

Function:
Description: Format a local time/date according to locale settings.
Version:
Status: latest
Date: 2011-12-20 05:35
Summary: Import from phpjs.
Programmer: jj5
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function strftime (fmt, timestamp) {
    // http://jsphp.co/jsphp/fn/view/strftime
    // +      original by: Blues (http://tech.bluesmoon.info/)
    // + reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Alex
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // -       depends on: setlocale
    // %        note 1: Uses global: php_js to store locale info
    // *        example 1: strftime("%A", 1062462400); // Return value will depend on date and locale
    // *        returns 1: 'Tuesday'
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.setlocale('LC_ALL', 0); // ensure setup of localization variables takes place
    // END REDUNDANT
    var phpjs = this.php_js;

    // BEGIN STATIC
    var _xPad = function (x, pad, r) {
        if (typeof r === 'undefined') {
            r = 10;
        }
        for (; parseInt(x, 10) < r && r > 1; r /= 10) {
            x = pad.toString() + x;
        }
        return x.toString();
    };

    var locale = phpjs.localeCategories.LC_TIME;
    var locales = phpjs.locales;
    var lc_time = locales[locale].LC_TIME;

    var _formats = {
        a: function (d) {
            return lc_time.a[d.getDay()];
        },
        A: function (d) {
            return lc_time.A[d.getDay()];
        },
        b: function (d) {
            return lc_time.b[d.getMonth()];
        },
        B: function (d) {
            return lc_time.B[d.getMonth()];
        },
        C: function (d) {
            return _xPad(parseInt(d.getFullYear() / 100, 10), 0);
        },
        d: ['getDate', '0'],
        e: ['getDate', ' '],
        g: function (d) {
            return _xPad(parseInt(this.G(d) / 100, 10), 0);
        },
        G: function (d) {
            var y = d.getFullYear();
            var V = parseInt(_formats.V(d), 10);
            var W = parseInt(_formats.W(d), 10);

            if (W > V) {
                y++;
            } else if (W === 0 && V >= 52) {
                y--;
            }

            return y;
        },
        H: ['getHours', '0'],
        I: function (d) {
            var I = d.getHours() % 12;
            return _xPad(I === 0 ? 12 : I, 0);
        },
        j: function (d) {
            var ms = d - new Date('' + d.getFullYear() + '/1/1 GMT');
            ms += d.getTimezoneOffset() * 60000; // Line differs from Yahoo implementation which would be equivalent to replacing it here with:
            // ms = new Date('' + d.getFullYear() + '/' + (d.getMonth()+1) + '/' + d.getDate() + ' GMT') - ms;
            var doy = parseInt(ms / 60000 / 60 / 24, 10) + 1;
            return _xPad(doy, 0, 100);
        },
        k: ['getHours', '0'],
        // not in PHP, but implemented here (as in Yahoo)
        l: function (d) {
            var l = d.getHours() % 12;
            return _xPad(l === 0 ? 12 : l, ' ');
        },
        m: function (d) {
            return _xPad(d.getMonth() + 1, 0);
        },
        M: ['getMinutes', '0'],
        p: function (d) {
            return lc_time.p[d.getHours() >= 12 ? 1 : 0];
        },
        P: function (d) {
            return lc_time.P[d.getHours() >= 12 ? 1 : 0];
        },
        s: function (d) { // Yahoo uses return parseInt(d.getTime()/1000, 10);
            return Date.parse(d) / 1000;
        },
        S: ['getSeconds', '0'],
        u: function (d) {
            var dow = d.getDay();
            return ((dow === 0) ? 7 : dow);
        },
        U: function (d) {
            var doy = parseInt(_formats.j(d), 10);
            var rdow = 6 - d.getDay();
            var woy = parseInt((doy + rdow) / 7, 10);
            return _xPad(woy, 0);
        },
        V: function (d) {
            var woy = parseInt(_formats.W(d), 10);
            var dow1_1 = (new Date('' + d.getFullYear() + '/1/1')).getDay();
            // First week is 01 and not 00 as in the case of %U and %W,
            // so we add 1 to the final result except if day 1 of the year
            // is a Monday (then %W returns 01).
            // We also need to subtract 1 if the day 1 of the year is
            // Friday-Sunday, so the resulting equation becomes:
            var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
            if (idow === 53 && (new Date('' + d.getFullYear() + '/12/31')).getDay() < 4) {
                idow = 1;
            } else if (idow === 0) {
                idow = _formats.V(new Date('' + (d.getFullYear() - 1) + '/12/31'));
            }
            return _xPad(idow, 0);
        },
        w: 'getDay',
        W: function (d) {
            var doy = parseInt(_formats.j(d), 10);
            var rdow = 7 - _formats.u(d);
            var woy = parseInt((doy + rdow) / 7, 10);
            return _xPad(woy, 0, 10);
        },
        y: function (d) {
            return _xPad(d.getFullYear() % 100, 0);
        },
        Y: 'getFullYear',
        z: function (d) {
            var o = d.getTimezoneOffset();
            var H = _xPad(parseInt(Math.abs(o / 60), 10), 0);
            var M = _xPad(o % 60, 0);
            return (o > 0 ? '-' : '+') + H + M;
        },
        Z: function (d) {
            return d.toString().replace(/^.*\(([^)]+)\)$/, '$1');
/*
            // Yahoo's: Better?
            var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, '$2').replace(/[a-z ]/g, '');
            if(tz.length > 4) {
                tz = Dt.formats.z(d);
            }
            return tz;
            */
        },
        '%': function (d) {
            return '%';
        }
    };
    // END STATIC
/* Fix: Locale alternatives are supported though not documented in PHP; see http://linux.die.net/man/3/strptime
Ec
EC
Ex
EX
Ey
EY
Od or Oe
OH
OI
Om
OM
OS
OU
Ow
OW
Oy
*/

    var _date = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
    (typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
    new Date(timestamp * 1000) // PHP API expects UNIX timestamp (auto-convert to int)
    );

    var _aggregates = {
        c: 'locale',
        D: '%m/%d/%y',
        F: '%y-%m-%d',
        h: '%b',
        n: '\n',
        r: 'locale',
        R: '%H:%M',
        t: '\t',
        T: '%H:%M:%S',
        x: 'locale',
        X: 'locale'
    };


    // First replace aggregates (run in a loop because an agg may be made up of other aggs)
    while (fmt.match(/%[cDFhnrRtTxX]/)) {
        fmt = fmt.replace(/%([cDFhnrRtTxX])/g, function (m0, m1) {
            var f = _aggregates[m1];
            return (f === 'locale' ? lc_time[m1] : f);
        });
    }

    // Now replace formats - we need a closure so that the date object gets passed through
    var str = fmt.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, function (m0, m1) {
        var f = _formats[m1];
        if (typeof f === 'string') {
            return _date[f]();
        } else if (typeof f === 'function') {
            return f(_date);
        } else if (typeof f === 'object' && typeof(f[0]) === 'string') {
            return _xPad(_date[f[0]](), f[1]);
        } else { // Shouldn't reach here
            return m1;
        }
    });
    return str;
}
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.