Initial commit
[yaffs-website] / node_modules / dateformat / lib / dateformat.js
1 /*
2  * Date Format 1.2.3
3  * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
4  * MIT license
5  *
6  * Includes enhancements by Scott Trenda <scott.trenda.net>
7  * and Kris Kowal <cixar.com/~kris.kowal/>
8  *
9  * Accepts a date, a mask, or a date and a mask.
10  * Returns a formatted version of the given date.
11  * The date defaults to the current date/time.
12  * The mask defaults to dateFormat.masks.default.
13  */
14
15 (function(global) {
16   'use strict';
17
18   var dateFormat = (function() {
19       var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZWN]|'[^']*'|'[^']*'/g;
20       var timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
21       var timezoneClip = /[^-+\dA-Z]/g;
22   
23       // Regexes and supporting functions are cached through closure
24       return function (date, mask, utc, gmt) {
25   
26         // You can't provide utc if you skip other args (use the 'UTC:' mask prefix)
27         if (arguments.length === 1 && kindOf(date) === 'string' && !/\d/.test(date)) {
28           mask = date;
29           date = undefined;
30         }
31   
32         date = date || new Date;
33   
34         if(!(date instanceof Date)) {
35           date = new Date(date);
36         }
37   
38         if (isNaN(date)) {
39           throw TypeError('Invalid date');
40         }
41   
42         mask = String(dateFormat.masks[mask] || mask || dateFormat.masks['default']);
43   
44         // Allow setting the utc/gmt argument via the mask
45         var maskSlice = mask.slice(0, 4);
46         if (maskSlice === 'UTC:' || maskSlice === 'GMT:') {
47           mask = mask.slice(4);
48           utc = true;
49           if (maskSlice === 'GMT:') {
50             gmt = true;
51           }
52         }
53   
54         var _ = utc ? 'getUTC' : 'get';
55         var d = date[_ + 'Date']();
56         var D = date[_ + 'Day']();
57         var m = date[_ + 'Month']();
58         var y = date[_ + 'FullYear']();
59         var H = date[_ + 'Hours']();
60         var M = date[_ + 'Minutes']();
61         var s = date[_ + 'Seconds']();
62         var L = date[_ + 'Milliseconds']();
63         var o = utc ? 0 : date.getTimezoneOffset();
64         var W = getWeek(date);
65         var N = getDayOfWeek(date);
66         var flags = {
67           d:    d,
68           dd:   pad(d),
69           ddd:  dateFormat.i18n.dayNames[D],
70           dddd: dateFormat.i18n.dayNames[D + 7],
71           m:    m + 1,
72           mm:   pad(m + 1),
73           mmm:  dateFormat.i18n.monthNames[m],
74           mmmm: dateFormat.i18n.monthNames[m + 12],
75           yy:   String(y).slice(2),
76           yyyy: y,
77           h:    H % 12 || 12,
78           hh:   pad(H % 12 || 12),
79           H:    H,
80           HH:   pad(H),
81           M:    M,
82           MM:   pad(M),
83           s:    s,
84           ss:   pad(s),
85           l:    pad(L, 3),
86           L:    pad(Math.round(L / 10)),
87           t:    H < 12 ? 'a'  : 'p',
88           tt:   H < 12 ? 'am' : 'pm',
89           T:    H < 12 ? 'A'  : 'P',
90           TT:   H < 12 ? 'AM' : 'PM',
91           Z:    gmt ? 'GMT' : utc ? 'UTC' : (String(date).match(timezone) || ['']).pop().replace(timezoneClip, ''),
92           o:    (o > 0 ? '-' : '+') + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
93           S:    ['th', 'st', 'nd', 'rd'][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10],
94           W:    W,
95           N:    N
96         };
97   
98         return mask.replace(token, function (match) {
99           if (match in flags) {
100             return flags[match];
101           }
102           return match.slice(1, match.length - 1);
103         });
104       };
105     })();
106
107   dateFormat.masks = {
108     'default':               'ddd mmm dd yyyy HH:MM:ss',
109     'shortDate':             'm/d/yy',
110     'mediumDate':            'mmm d, yyyy',
111     'longDate':              'mmmm d, yyyy',
112     'fullDate':              'dddd, mmmm d, yyyy',
113     'shortTime':             'h:MM TT',
114     'mediumTime':            'h:MM:ss TT',
115     'longTime':              'h:MM:ss TT Z',
116     'isoDate':               'yyyy-mm-dd',
117     'isoTime':               'HH:MM:ss',
118     'isoDateTime':           'yyyy-mm-dd\'T\'HH:MM:sso',
119     'isoUtcDateTime':        'UTC:yyyy-mm-dd\'T\'HH:MM:ss\'Z\'',
120     'expiresHeaderFormat':   'ddd, dd mmm yyyy HH:MM:ss Z'
121   };
122
123   // Internationalization strings
124   dateFormat.i18n = {
125     dayNames: [
126       'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
127       'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
128     ],
129     monthNames: [
130       'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
131       'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'
132     ]
133   };
134
135 function pad(val, len) {
136   val = String(val);
137   len = len || 2;
138   while (val.length < len) {
139     val = '0' + val;
140   }
141   return val;
142 }
143
144 /**
145  * Get the ISO 8601 week number
146  * Based on comments from
147  * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html
148  *
149  * @param  {Object} `date`
150  * @return {Number}
151  */
152 function getWeek(date) {
153   // Remove time components of date
154   var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
155
156   // Change date to Thursday same week
157   targetThursday.setDate(targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3);
158
159   // Take January 4th as it is always in week 1 (see ISO 8601)
160   var firstThursday = new Date(targetThursday.getFullYear(), 0, 4);
161
162   // Change date to Thursday same week
163   firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3);
164
165   // Check if daylight-saving-time-switch occured and correct for it
166   var ds = targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset();
167   targetThursday.setHours(targetThursday.getHours() - ds);
168
169   // Number of weeks between target Thursday and first Thursday
170   var weekDiff = (targetThursday - firstThursday) / (86400000*7);
171   return 1 + Math.floor(weekDiff);
172 }
173
174 /**
175  * Get ISO-8601 numeric representation of the day of the week
176  * 1 (for Monday) through 7 (for Sunday)
177  * 
178  * @param  {Object} `date`
179  * @return {Number}
180  */
181 function getDayOfWeek(date) {
182   var dow = date.getDay();
183   if(dow === 0) {
184     dow = 7;
185   }
186   return dow;
187 }
188
189 /**
190  * kind-of shortcut
191  * @param  {*} val
192  * @return {String}
193  */
194 function kindOf(val) {
195   if (val === null) {
196     return 'null';
197   }
198
199   if (val === undefined) {
200     return 'undefined';
201   }
202
203   if (typeof val !== 'object') {
204     return typeof val;
205   }
206
207   if (Array.isArray(val)) {
208     return 'array';
209   }
210
211   return {}.toString.call(val)
212     .slice(8, -1).toLowerCase();
213 };
214
215
216
217   if (typeof define === 'function' && define.amd) {
218     define(function () {
219       return dateFormat;
220     });
221   } else if (typeof exports === 'object') {
222     module.exports = dateFormat;
223   } else {
224     global.dateFormat = dateFormat;
225   }
226 })(this);