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