source: Dev/branches/rest-dojo-ui/client/dojox/date/posix.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 10.3 KB
Line 
1define(["dojo/_base/kernel", "dojo/date", "dojo/date/locale", "dojo/string", "dojo/cldr/supplemental"],
2       function(dojo, dojoDate, dojoDateLocale, dojoString, dojoCldrSupplemental){
3
4dojo.getObject("date.posix", true, dojox);
5
6dojox.date.posix.strftime = function(/*Date*/dateObject, /*String*/format, /*String?*/locale){
7//
8// summary:
9//              Formats the date object using the specifications of the POSIX strftime function
10//
11// description:
12//              see http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
13
14        // zero pad
15        var padChar = null;
16        var _ = function(s, n){
17                return dojoString.pad(s, n || 2, padChar || "0");
18        };
19
20        var bundle = dojoDateLocale._getGregorianBundle(locale);
21
22        var $ = function(property){
23                switch(property){
24                        case "a": // abbreviated weekday name according to the current locale
25                                return dojoDateLocale.getNames('days', 'abbr', 'format', locale)[dateObject.getDay()];
26
27                        case "A": // full weekday name according to the current locale
28                                return dojoDateLocale.getNames('days', 'wide', 'format', locale)[dateObject.getDay()];
29
30                        case "b":
31                        case "h": // abbreviated month name according to the current locale
32                                return dojoDateLocale.getNames('months', 'abbr', 'format', locale)[dateObject.getMonth()];
33                               
34                        case "B": // full month name according to the current locale
35                                return dojoDateLocale.getNames('months', 'wide', 'format', locale)[dateObject.getMonth()];
36                               
37                        case "c": // preferred date and time representation for the current
38                                      // locale
39                                return dojoDateLocale.format(dateObject, {formatLength: 'full', locale: locale});
40
41                        case "C": // century number (the year divided by 100 and truncated
42                                      // to an integer, range 00 to 99)
43                                return _(Math.floor(dateObject.getFullYear()/100));
44                               
45                        case "d": // day of the month as a decimal number (range 01 to 31)
46                                return _(dateObject.getDate());
47                               
48                        case "D": // same as %m/%d/%y
49                                return $("m") + "/" + $("d") + "/" + $("y");
50                                       
51                        case "e": // day of the month as a decimal number, a single digit is
52                                      // preceded by a space (range ' 1' to '31')
53                                if(padChar == null){ padChar = " "; }
54                                return _(dateObject.getDate());
55                       
56                        case "f": // month as a decimal number, a single digit is
57                                                        // preceded by a space (range ' 1' to '12')
58                                if(padChar == null){ padChar = " "; }
59                                return _(dateObject.getMonth()+1);
60                       
61                        case "g": // like %G, but without the century.
62                                break;
63                       
64                        case "G": // The 4-digit year corresponding to the ISO week number
65                                      // (see %V).  This has the same format and value as %Y,
66                                      // except that if the ISO week number belongs to the
67                                      // previous or next year, that year is used instead.
68                                console.warn("unimplemented modifier 'G'");
69                                break;
70                       
71                        case "F": // same as %Y-%m-%d
72                                return $("Y") + "-" + $("m") + "-" + $("d");
73                               
74                        case "H": // hour as a decimal number using a 24-hour clock (range
75                                      // 00 to 23)
76                                return _(dateObject.getHours());
77                               
78                        case "I": // hour as a decimal number using a 12-hour clock (range
79                                      // 01 to 12)
80                                return _(dateObject.getHours() % 12 || 12);
81
82                        case "j": // day of the year as a decimal number (range 001 to 366)
83                                return _(dojoDateLocale._getDayOfYear(dateObject), 3);
84
85                        case "k": // Hour as a decimal number using a 24-hour clock (range
86                                          // 0 to 23 (space-padded))
87                                if(padChar == null){ padChar = " "; }
88                                return _(dateObject.getHours());
89
90                        case "l": // Hour as a decimal number using a 12-hour clock (range
91                                          // 1 to 12 (space-padded))
92                                if(padChar == null){ padChar = " "; }
93                                return _(dateObject.getHours() % 12 || 12);
94
95                        case "m": // month as a decimal number (range 01 to 12)
96                                return _(dateObject.getMonth() + 1);
97
98                        case "M": // minute as a decimal number
99                                return _(dateObject.getMinutes());
100
101                        case "n":
102                                return "\n";
103
104                        case "p": // either `am' or `pm' according to the given time value,
105                                      // or the corresponding strings for the current locale
106                                return bundle['dayPeriods-format-wide-' + (dateObject.getHours() < 12 ? "am" : "pm")];
107                               
108                        case "r": // time in a.m. and p.m. notation
109                                return $("I") + ":" + $("M") + ":" + $("S") + " " + $("p");
110                               
111                        case "R": // time in 24 hour notation
112                                return $("H") + ":" + $("M");
113                               
114                        case "S": // second as a decimal number
115                                return _(dateObject.getSeconds());
116
117                        case "t":
118                                return "\t";
119
120                        case "T": // current time, equal to %H:%M:%S
121                                return $("H") + ":" + $("M") + ":" + $("S");
122                               
123                        case "u": // weekday as a decimal number [1,7], with 1 representing
124                                      // Monday
125                                return String(dateObject.getDay() || 7);
126                               
127                        case "U": // week number of the current year as a decimal number,
128                                      // starting with the first Sunday as the first day of the
129                                      // first week
130                                return _(dojoDateLocale._getWeekOfYear(dateObject));
131
132                        case "V": // week number of the year (Monday as the first day of the
133                                      // week) as a decimal number [01,53]. If the week containing
134                                      // 1 January has four or more days in the new year, then it
135                                      // is considered week 1. Otherwise, it is the last week of
136                                      // the previous year, and the next week is week 1.
137                                return _(dojox.date.posix.getIsoWeekOfYear(dateObject));
138                               
139                        case "W": // week number of the current year as a decimal number,
140                                      // starting with the first Monday as the first day of the
141                                      // first week
142                                return _(dojoDateLocale._getWeekOfYear(dateObject, 1));
143                               
144                        case "w": // day of the week as a decimal, Sunday being 0
145                                return String(dateObject.getDay());
146
147                        case "x": // preferred date representation for the current locale
148                                      // without the time
149                                return dojoDateLocale.format(dateObject, {selector:'date', formatLength: 'full', locale:locale});
150
151                        case "X": // preferred time representation for the current locale
152                                      // without the date
153                                return dojoDateLocale.format(dateObject, {selector:'time', formatLength: 'full', locale:locale});
154
155                        case "y": // year as a decimal number without a century (range 00 to
156                                      // 99)
157                                return _(dateObject.getFullYear()%100);
158                               
159                        case "Y": // year as a decimal number including the century
160                                return String(dateObject.getFullYear());
161                       
162                        case "z": // time zone or name or abbreviation
163                                var timezoneOffset = dateObject.getTimezoneOffset();
164                                return (timezoneOffset > 0 ? "-" : "+") +
165                                        _(Math.floor(Math.abs(timezoneOffset)/60)) + ":" +
166                                        _(Math.abs(timezoneOffset)%60);
167
168                        case "Z": // time zone or name or abbreviation
169                                return dojoDate.getTimezoneName(dateObject);
170                       
171                        case "%":
172                                return "%";
173                }
174        };
175
176        // parse the formatting string and construct the resulting string
177        var string = "",
178                i = 0,
179                index = 0,
180                switchCase = null;
181        while ((index = format.indexOf("%", i)) != -1){
182                string += format.substring(i, index++);
183               
184                // inspect modifier flag
185                switch (format.charAt(index++)) {
186                        case "_": // Pad a numeric result string with spaces.
187                                padChar = " "; break;
188                        case "-": // Do not pad a numeric result string.
189                                padChar = ""; break;
190                        case "0": // Pad a numeric result string with zeros.
191                                padChar = "0"; break;
192                        case "^": // Convert characters in result string to uppercase.
193                                switchCase = "upper"; break;
194                        case "*": // Convert characters in result string to lowercase
195                                switchCase = "lower"; break;
196                        case "#": // Swap the case of the result string.
197                                switchCase = "swap"; break;
198                        default: // no modifier flag so decrement the index
199                                padChar = null; index--; break;
200                }
201
202                // toggle case if a flag is set
203                var property = $(format.charAt(index++));
204                switch (switchCase){
205                        case "upper":
206                                property = property.toUpperCase();
207                                break;
208                        case "lower":
209                                property = property.toLowerCase();
210                                break;
211                        case "swap": // Upper to lower, and versey-vicea
212                                var compareString = property.toLowerCase();
213                                var swapString = '';
214                                var ch = '';
215                                for (var j = 0; j < property.length; j++){
216                                        ch = property.charAt(j);
217                                        swapString += (ch == compareString.charAt(j)) ?
218                                                ch.toUpperCase() : ch.toLowerCase();
219                                }
220                                property = swapString;
221                                break;
222                        default:
223                                break;
224                }
225                switchCase = null;
226               
227                string += property;
228                i = index;
229        }
230        string += format.substring(i);
231       
232        return string; // String
233};
234
235dojox.date.posix.getStartOfWeek = function(/*Date*/dateObject, /*Number*/firstDay){
236        // summary: Return a date object representing the first day of the given
237        //   date's week.
238        if(isNaN(firstDay)){
239                firstDay = dojoCldrSupplemental.getFirstDayOfWeek ? dojoCldrSupplemental.getFirstDayOfWeek() : 0;
240        }
241        var offset = firstDay;
242        if(dateObject.getDay() >= firstDay){
243                offset -= dateObject.getDay();
244        }else{
245                offset -= (7 - dateObject.getDay());
246        }
247        var date = new Date(dateObject);
248        date.setHours(0, 0, 0, 0);
249        return dojoDate.add(date, "day", offset); // Date
250}
251
252dojox.date.posix.setIsoWeekOfYear = function(/*Date*/dateObject, /*Number*/week){
253        // summary: Set the ISO8601 week number of the given date.
254        //   The week containing January 4th is the first week of the year.
255        // week:
256        //   can be positive or negative: -1 is the year's last week.
257        if(!week){ return dateObject; }
258        var currentWeek = dojox.date.posix.getIsoWeekOfYear(dateObject);
259        var offset = week - currentWeek;
260        if(week < 0){
261                var weeks = dojox.date.posix.getIsoWeeksInYear(dateObject);
262                offset = (weeks + week + 1) - currentWeek;
263        }
264        return dojoDate.add(dateObject, "week", offset); // Date
265}
266
267dojox.date.posix.getIsoWeekOfYear = function(/*Date*/dateObject){
268        // summary: Get the ISO8601 week number of the given date.
269        //   The week containing January 4th is the first week of the year.
270        //   See http://en.wikipedia.org/wiki/ISO_week_date
271        var weekStart = dojox.date.posix.getStartOfWeek(dateObject, 1);
272        var yearStart = new Date(dateObject.getFullYear(), 0, 4); // January 4th
273        yearStart = dojox.date.posix.getStartOfWeek(yearStart, 1);
274        var diff = weekStart.getTime() - yearStart.getTime();
275        if(diff < 0){ return dojox.date.posix.getIsoWeeksInYear(weekStart); } // Integer
276        return Math.ceil(diff / 604800000) + 1; // Integer
277}
278
279dojox.date.posix.getIsoWeeksInYear = function(/*Date*/dateObject) {
280        // summary: Determine the number of ISO8601 weeks in the year of the given
281        //   date. Most years have 52 but some have 53.
282        //   See http://www.phys.uu.nl/~vgent/calendar/isocalendar_text3.htm
283        function p(y) {
284                return y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
285        }
286        var y = dateObject.getFullYear();
287        return ( p(y) % 7 == 4 || p(y-1) % 7 == 3 ) ? 53 : 52;  //      Integer
288}
289        return dojox.date.posix;
290});
Note: See TracBrowser for help on using the repository browser.