9c09c69325e8b8e91fd53e031c4f503cebcec2ad
[platform/framework/web/crosswalk-tizen.git] /
1 define(['../number/pad', '../string/lpad', './i18n_', './dayOfTheYear', './timezoneOffset', './timezoneAbbr', './weekOfTheYear'], function (pad, lpad, i18n, dayOfTheYear, timezoneOffset, timezoneAbbr, weekOfTheYear) {
2
3     var _combinations = {
4         'D': '%m/%d/%y',
5         'F': '%Y-%m-%d',
6         'r': '%I:%M:%S %p',
7         'R': '%H:%M',
8         'T': '%H:%M:%S',
9         'x': 'locale',
10         'X': 'locale',
11         'c': 'locale'
12     };
13
14
15     /**
16      * format date based on strftime format
17      */
18     function strftime(date, format, localeData){
19         localeData = localeData  || i18n;
20         var reToken = /%([a-z%])/gi;
21
22         function makeIterator(fn) {
23             return function(match, token){
24                 return fn(date, token, localeData);
25             };
26         }
27
28         return format
29             .replace(reToken, makeIterator(expandCombinations))
30             .replace(reToken, makeIterator(convertToken));
31     }
32
33
34     function expandCombinations(date, token, l10n){
35         if (token in _combinations) {
36             var expanded = _combinations[token];
37             return expanded === 'locale'? l10n[token] : expanded;
38         } else {
39             return '%'+ token;
40         }
41     }
42
43
44     function convertToken(date, token, l10n){
45         switch (token){
46             case 'a':
47                 return l10n.days_abbr[date.getDay()];
48             case 'A':
49                 return l10n.days[date.getDay()];
50             case 'h':
51             case 'b':
52                 return l10n.months_abbr[date.getMonth()];
53             case 'B':
54                 return l10n.months[date.getMonth()];
55             case 'C':
56                 return pad(Math.floor(date.getFullYear() / 100), 2);
57             case 'd':
58                 return pad(date.getDate(), 2);
59             case 'e':
60                 return pad(date.getDate(), 2, ' ');
61             case 'H':
62                 return pad(date.getHours(), 2);
63             case 'I':
64                 return pad(date.getHours() % 12, 2);
65             case 'j':
66                 return pad(dayOfTheYear(date), 3);
67             case 'l':
68                 return lpad(date.getHours() % 12, 2);
69             case 'L':
70                 return pad(date.getMilliseconds(), 3);
71             case 'm':
72                 return pad(date.getMonth() + 1, 2);
73             case 'M':
74                 return pad(date.getMinutes(), 2);
75             case 'n':
76                 return '\n';
77             case 'p':
78                 return date.getHours() >= 12? l10n.pm : l10n.am;
79             case 'P':
80                 return convertToken(date, 'p', l10n).toLowerCase();
81             case 's':
82                 return date.getTime() / 1000;
83             case 'S':
84                 return pad(date.getSeconds(), 2);
85             case 't':
86                 return '\t';
87             case 'u':
88                 var day = date.getDay();
89                 return day === 0? 7 : day;
90             case 'U':
91                 return pad(weekOfTheYear(date), 2);
92             case 'w':
93                 return date.getDay();
94             case 'W':
95                 return pad(weekOfTheYear(date, 1), 2);
96             case 'y':
97                 return pad(date.getFullYear() % 100, 2);
98             case 'Y':
99                 return pad(date.getFullYear(), 4);
100             case 'z':
101                 return timezoneOffset(date);
102             case 'Z':
103                 return timezoneAbbr(date);
104             case '%':
105                 return '%';
106             default:
107                 // keep unrecognized tokens
108                 return '%'+ token;
109         }
110     }
111
112
113     return strftime;
114
115 });