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");
5 dojo.require("dojo.date");
6 dojo.require("dojo.date.locale");
7 dojo.require("dojo.string");
9 dojox.date.posix.strftime = function(/*Date*/dateObject, /*String*/format, /*String?*/locale){
12 // Formats the date object using the specifications of the POSIX strftime function
15 // see http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
19 var _ = function(s, n){
20 return dojo.string.pad(s, n || 2, padChar || "0");
23 var bundle = dojo.date.locale._getGregorianBundle(locale);
25 var $ = function(property){
27 case "a": // abbreviated weekday name according to the current locale
28 return dojo.date.locale.getNames('days', 'abbr', 'format', locale)[dateObject.getDay()];
30 case "A": // full weekday name according to the current locale
31 return dojo.date.locale.getNames('days', 'wide', 'format', locale)[dateObject.getDay()];
34 case "h": // abbreviated month name according to the current locale
35 return dojo.date.locale.getNames('months', 'abbr', 'format', locale)[dateObject.getMonth()];
37 case "B": // full month name according to the current locale
38 return dojo.date.locale.getNames('months', 'wide', 'format', locale)[dateObject.getMonth()];
40 case "c": // preferred date and time representation for the current
42 return dojo.date.locale.format(dateObject, {formatLength: 'full', locale: locale});
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));
48 case "d": // day of the month as a decimal number (range 01 to 31)
49 return _(dateObject.getDate());
51 case "D": // same as %m/%d/%y
52 return $("m") + "/" + $("d") + "/" + $("y");
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());
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);
64 case "g": // like %G, but without the century.
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'");
74 case "F": // same as %Y-%m-%d
75 return $("Y") + "-" + $("m") + "-" + $("d");
77 case "H": // hour as a decimal number using a 24-hour clock (range
79 return _(dateObject.getHours());
81 case "I": // hour as a decimal number using a 12-hour clock (range
83 return _(dateObject.getHours() % 12 || 12);
85 case "j": // day of the year as a decimal number (range 001 to 366)
86 return _(dojo.date.locale._getDayOfYear(dateObject), 3);
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());
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);
98 case "m": // month as a decimal number (range 01 to 12)
99 return _(dateObject.getMonth() + 1);
101 case "M": // minute as a decimal number
102 return _(dateObject.getMinutes());
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"];
111 case "r": // time in a.m. and p.m. notation
112 return $("I") + ":" + $("M") + ":" + $("S") + " " + $("p");
114 case "R": // time in 24 hour notation
115 return $("H") + ":" + $("M");
117 case "S": // second as a decimal number
118 return _(dateObject.getSeconds());
123 case "T": // current time, equal to %H:%M:%S
124 return $("H") + ":" + $("M") + ":" + $("S");
126 case "u": // weekday as a decimal number [1,7], with 1 representing
128 return String(dateObject.getDay() || 7);
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
133 return _(dojo.date.locale._getWeekOfYear(dateObject));
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));
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
145 return _(dojo.date.locale._getWeekOfYear(dateObject, 1));
147 case "w": // day of the week as a decimal, Sunday being 0
148 return String(dateObject.getDay());
150 case "x": // preferred date representation for the current locale
152 return dojo.date.locale.format(dateObject, {selector:'date', formatLength: 'full', locale:locale});
154 case "X": // preferred time representation for the current locale
156 return dojo.date.locale.format(dateObject, {selector:'time', formatLength: 'full', locale:locale});
158 case "y": // year as a decimal number without a century (range 00 to
160 return _(dateObject.getFullYear()%100);
162 case "Y": // year as a decimal number including the century
163 return String(dateObject.getFullYear());
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);
171 case "Z": // time zone or name or abbreviation
172 return dojo.date.getTimezoneName(dateObject);
179 // parse the formatting string and construct the resulting string
183 var switchCase = null;
184 while ((index = format.indexOf("%", i)) != -1){
185 string += format.substring(i, index++);
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.
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;
205 // toggle case if a flag is set
206 var property = $(format.charAt(index++));
209 property = property.toUpperCase();
212 property = property.toLowerCase();
214 case "swap": // Upper to lower, and versey-vicea
215 var compareString = property.toLowerCase();
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();
223 property = swapString;
233 string += format.substring(i);
235 return string; // String
238 dojox.date.posix.getStartOfWeek = function(/*Date*/dateObject, /*Number*/firstDay){
239 // summary: Return a date object representing the first day of the given
242 firstDay = dojo.cldr.supplemental.getFirstDayOfWeek ? dojo.cldr.supplemental.getFirstDayOfWeek() : 0;
244 var offset = firstDay;
245 if(dateObject.getDay() >= firstDay){
246 offset -= dateObject.getDay();
248 offset -= (7 - dateObject.getDay());
250 var date = new Date(dateObject);
251 date.setHours(0, 0, 0, 0);
252 return dojo.date.add(date, "day", offset); // Date
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.
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;
264 var weeks = dojox.date.posix.getIsoWeeksInYear(dateObject);
265 offset = (weeks + week + 1) - currentWeek;
267 return dojo.date.add(dateObject, "week", offset); // Date
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
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
287 return y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400);
289 var y = dateObject.getFullYear();
290 return ( p(y) % 7 == 4 || p(y-1) % 7 == 3 ) ? 53 : 52; // Integer