1 | var utils = require('./utils');
|
2 |
|
3 | var _months = {
|
4 | full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
|
5 | abbr: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
6 | },
|
7 | _days = {
|
8 | full: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
|
9 | abbr: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
|
10 | alt: {'-1': 'Yesterday', 0: 'Today', 1: 'Tomorrow'}
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | exports.tzOffset = 0;
|
21 | exports.DateZ = function () {
|
22 | var members = {
|
23 | 'default': ['getUTCDate', 'getUTCDay', 'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth', 'getUTCSeconds', 'toISOString', 'toGMTString', 'toUTCString', 'valueOf', 'getTime'],
|
24 | z: ['getDate', 'getDay', 'getFullYear', 'getHours', 'getMilliseconds', 'getMinutes', 'getMonth', 'getSeconds', 'getYear', 'toDateString', 'toLocaleDateString', 'toLocaleTimeString']
|
25 | },
|
26 | d = this;
|
27 |
|
28 | d.date = d.dateZ = (arguments.length > 1) ? new Date(Date.UTC.apply(Date, arguments) + ((new Date()).getTimezoneOffset() * 60000)) : (arguments.length === 1) ? new Date(new Date(arguments['0'])) : new Date();
|
29 |
|
30 | d.timezoneOffset = d.dateZ.getTimezoneOffset();
|
31 |
|
32 | utils.each(members.z, function (name) {
|
33 | d[name] = function () {
|
34 | return d.dateZ[name]();
|
35 | };
|
36 | });
|
37 | utils.each(members['default'], function (name) {
|
38 | d[name] = function () {
|
39 | return d.date[name]();
|
40 | };
|
41 | });
|
42 |
|
43 | this.setTimezoneOffset(exports.tzOffset);
|
44 | };
|
45 | exports.DateZ.prototype = {
|
46 | getTimezoneOffset: function () {
|
47 | return this.timezoneOffset;
|
48 | },
|
49 | setTimezoneOffset: function (offset) {
|
50 | this.timezoneOffset = offset;
|
51 | this.dateZ = new Date(this.date.getTime() + this.date.getTimezoneOffset() * 60000 - this.timezoneOffset * 60000);
|
52 | return this;
|
53 | }
|
54 | };
|
55 |
|
56 |
|
57 | exports.d = function (input) {
|
58 | return (input.getDate() < 10 ? '0' : '') + input.getDate();
|
59 | };
|
60 | exports.D = function (input) {
|
61 | return _days.abbr[input.getDay()];
|
62 | };
|
63 | exports.j = function (input) {
|
64 | return input.getDate();
|
65 | };
|
66 | exports.l = function (input) {
|
67 | return _days.full[input.getDay()];
|
68 | };
|
69 | exports.N = function (input) {
|
70 | var d = input.getDay();
|
71 | return (d >= 1) ? d : 7;
|
72 | };
|
73 | exports.S = function (input) {
|
74 | var d = input.getDate();
|
75 | return (d % 10 === 1 && d !== 11 ? 'st' : (d % 10 === 2 && d !== 12 ? 'nd' : (d % 10 === 3 && d !== 13 ? 'rd' : 'th')));
|
76 | };
|
77 | exports.w = function (input) {
|
78 | return input.getDay();
|
79 | };
|
80 | exports.z = function (input, offset, abbr) {
|
81 | var year = input.getFullYear(),
|
82 | e = new exports.DateZ(year, input.getMonth(), input.getDate(), 12, 0, 0),
|
83 | d = new exports.DateZ(year, 0, 1, 12, 0, 0);
|
84 |
|
85 | e.setTimezoneOffset(offset, abbr);
|
86 | d.setTimezoneOffset(offset, abbr);
|
87 | return Math.round((e - d) / 86400000);
|
88 | };
|
89 |
|
90 |
|
91 | exports.W = function (input) {
|
92 | var target = new Date(input.valueOf()),
|
93 | dayNr = (input.getDay() + 6) % 7,
|
94 | fThurs;
|
95 |
|
96 | target.setDate(target.getDate() - dayNr + 3);
|
97 | fThurs = target.valueOf();
|
98 | target.setMonth(0, 1);
|
99 | if (target.getDay() !== 4) {
|
100 | target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
|
101 | }
|
102 |
|
103 | return 1 + Math.ceil((fThurs - target) / 604800000);
|
104 | };
|
105 |
|
106 |
|
107 | exports.F = function (input) {
|
108 | return _months.full[input.getMonth()];
|
109 | };
|
110 | exports.m = function (input) {
|
111 | return (input.getMonth() < 9 ? '0' : '') + (input.getMonth() + 1);
|
112 | };
|
113 | exports.M = function (input) {
|
114 | return _months.abbr[input.getMonth()];
|
115 | };
|
116 | exports.n = function (input) {
|
117 | return input.getMonth() + 1;
|
118 | };
|
119 | exports.t = function (input) {
|
120 | return 32 - (new Date(input.getFullYear(), input.getMonth(), 32).getDate());
|
121 | };
|
122 |
|
123 |
|
124 | exports.L = function (input) {
|
125 | return new Date(input.getFullYear(), 1, 29).getDate() === 29;
|
126 | };
|
127 | exports.o = function (input) {
|
128 | var target = new Date(input.valueOf());
|
129 | target.setDate(target.getDate() - ((input.getDay() + 6) % 7) + 3);
|
130 | return target.getFullYear();
|
131 | };
|
132 | exports.Y = function (input) {
|
133 | return input.getFullYear();
|
134 | };
|
135 | exports.y = function (input) {
|
136 | return (input.getFullYear().toString()).substr(2);
|
137 | };
|
138 |
|
139 |
|
140 | exports.a = function (input) {
|
141 | return input.getHours() < 12 ? 'am' : 'pm';
|
142 | };
|
143 | exports.A = function (input) {
|
144 | return input.getHours() < 12 ? 'AM' : 'PM';
|
145 | };
|
146 | exports.B = function (input) {
|
147 | var hours = input.getUTCHours(), beats;
|
148 | hours = (hours === 23) ? 0 : hours + 1;
|
149 | beats = Math.abs(((((hours * 60) + input.getUTCMinutes()) * 60) + input.getUTCSeconds()) / 86.4).toFixed(0);
|
150 | return ('000'.concat(beats).slice(beats.length));
|
151 | };
|
152 | exports.g = function (input) {
|
153 | var h = input.getHours();
|
154 | return h === 0 ? 12 : (h > 12 ? h - 12 : h);
|
155 | };
|
156 | exports.G = function (input) {
|
157 | return input.getHours();
|
158 | };
|
159 | exports.h = function (input) {
|
160 | var h = input.getHours();
|
161 | return ((h < 10 || (12 < h && 22 > h)) ? '0' : '') + ((h < 12) ? h : h - 12);
|
162 | };
|
163 | exports.H = function (input) {
|
164 | var h = input.getHours();
|
165 | return (h < 10 ? '0' : '') + h;
|
166 | };
|
167 | exports.i = function (input) {
|
168 | var m = input.getMinutes();
|
169 | return (m < 10 ? '0' : '') + m;
|
170 | };
|
171 | exports.s = function (input) {
|
172 | var s = input.getSeconds();
|
173 | return (s < 10 ? '0' : '') + s;
|
174 | };
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | exports.O = function (input) {
|
181 | var tz = input.getTimezoneOffset();
|
182 | return (tz < 0 ? '-' : '+') + (tz / 60 < 10 ? '0' : '') + Math.abs((tz / 60)) + '00';
|
183 | };
|
184 |
|
185 | exports.Z = function (input) {
|
186 | return input.getTimezoneOffset() * 60;
|
187 | };
|
188 |
|
189 |
|
190 | exports.c = function (input) {
|
191 | return input.toISOString();
|
192 | };
|
193 | exports.r = function (input) {
|
194 | return input.toUTCString();
|
195 | };
|
196 | exports.U = function (input) {
|
197 | return input.getTime() / 1000;
|
198 | };
|