/
time.js
244 lines (202 loc) · 7.5 KB
/
time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
(function(){
var Time = function(yearOrString, month, day, hour, minute, second, millisecond){
if (typeof(yearOrString) === "string") {
this.date = new Date(Date.parse(yearOrString));
} else {
if (yearOrString) {
this.date = new Date(yearOrString, (month - 1 || 0), (day || 1), (hour || 0), (minute || 0), (second || 0), (millisecond || 0));
} else {
this.date = new Date();
}
}
};
Time.firstDayOfWeek = 0;
var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var MILLISECONDS_IN_WEEK = 604800000.0;
var MILLISECONDS_IN_DAY = 86400000;
var MILLISECONDS_IN_HOUR = 3600000;
var THIRTY_TWO_DAYS = 2764800000;
Time.prototype = {
// Month gets special treatment, to avoid zero indexing from Date.
month: function(value) {
if (value) {
this.date.setMonth(value - 1);
} else {
return this.date.getMonth() + 1;
}
},
// The week number of the year, iso8601.
week: function (value) {
if (value) {
throw new Error("Setting week number not implemented yet.");
} else {
// With thanks to http://github.com/jquery/jquery-ui/blob/37e8dd605da5d99600c0/ui/jquery.ui.datepicker.js#L919
var checker = this.clone();
checker.day(checker.day() + 4 - checker.weekday());
var epoch = checker.epoch();
checker.beginningOfYear();
return Math.floor(Math.round((epoch - checker.epoch()) / MILLISECONDS_IN_DAY) / 7) + 1;
}
},
// There is no setDay(), implementing that here.
weekday: function(value) {
if (value) {
this.advanceDays(value - this.weekday());
} else {
var a = (this.date.getDay() - (this.firstDayOfWeek || Time.firstDayOfWeek));
var b = 7;
var ringModulo = a - Math.floor(a / b) * b;
return ringModulo + 1;
}
},
/////////////////////////////////////
// Utility
clone: function(){
var newTime = new Time();
newTime.epoch(this.epoch());
newTime.firstDayOfWeek = this.firstDayOfWeek;
return newTime;
},
isLeapYear: function(){
var year = this.year();
return (year % 4 === 0) && (year % 100 !== 0) || (year % 400 === 0);
},
daysInMonth: function(){
if (this.month() === 2 && this.isLeapYear()) {
return 29;
}
return DAYS_IN_MONTH[this.month() - 1];
},
weeksInMonth: function(){
var millisecondsInThisMonth = this.clone().endOfMonth().epoch() - this.clone().firstDayInCalendarMonth().epoch();
return Math.ceil(millisecondsInThisMonth / MILLISECONDS_IN_WEEK);
},
// The current week's index in weeksInMonth
weekOfCurrentMonth: function(){
// NFI how this works. The tests pass, and brute force ftw.
return Math.floor((this.day() + this.clone().day(1).weekday() + 5) / 7);
},
firstDayInCalendarMonth: function(){
this.beginningOfMonth();
this.weekday(1);
return this;
},
/////////////////////////////////////
// Stepping
beginningOfYear: function(){
this.month(1); this.beginningOfMonth();
return this;
},
beginningOfMonth: function(){
this.day(1); this.beginningOfDay();
return this;
},
beginningOfWeek: function(){
this.weekday(1); this.beginningOfDay();
return this;
},
beginningOfDay: function(){
this.hour(0); this.beginningOfHour();
return this;
},
beginningOfHour: function(){
this.minute(0); this.beginningOfMinute();
return this;
},
beginningOfMinute: function(){
this.second(0); this.millisecond(0);
return this;
},
// ------------
endOfYear: function(){
this.month(12); this.endOfMonth();
return this;
},
endOfMonth: function(){
this.day(this.daysInMonth()); this.endOfDay();
return this;
},
endOfWeek: function(){
this.weekday(7); this.endOfDay();
return this;
},
endOfDay: function(){
this.hour(23); this.endOfHour();
return this;
},
endOfHour: function(){
this.minute(59); this.endOfMinute();
return this;
},
endOfMinute: function(){
this.second(59);
return this;
},
// ------------
nextMonth: function(){
this.epoch(this.beginningOfMonth().epoch() + THIRTY_TWO_DAYS);
this.beginningOfMonth();
return this;
},
previousMonth: function(){
this.epoch(this.beginningOfMonth().epoch() - 1);
this.beginningOfMonth();
return this;
},
// ------------
advanceDays: function(days) {
this.beginningOfDay();
// Adding 1 hour, in case this is a day where daylight savings change
this.epoch(this.epoch() + (MILLISECONDS_IN_DAY * days) + MILLISECONDS_IN_HOUR);
this.beginningOfDay();
return this;
},
advanceMonths: function(months) {
var base = this.year() * 12 + (this.month() - 1) + months;
var newYear = Math.floor(base / 12);
var newMonth = (base % 12) + 1;
// Setting the month to '2' on january 31th
// gives us march 2nd. Circumventing this.
var newTime = new Time(newYear, newMonth);
var daysInNewTimeMonth = newTime.daysInMonth();
if (this.day() > daysInNewTimeMonth) {
this.year(newYear);
this.day(1);
this.month(newMonth);
this.day(daysInNewTimeMonth);
} else {
this.year(newYear);
this.month(newMonth);
}
return this;
},
// ------------
toString: function() {
return [this.year(), this.month(), this.day()].join(".") + " " + [this.hour(), this.minute(), this.second()].join(":");
}
};
/////////////////////////////////////
// The accessors. Uses the same function for both getting and
// setting, e.g. year() to get and year(2005) to set.
Time.accessor = function(name, dateFunctionName){
Time.prototype[name] = function(value) {
if (value !== undefined) {
this.date["set" + dateFunctionName](value);
return this;
} else {
return this.date["get" + dateFunctionName]();
}
};
};
Time.accessor("year", "FullYear");
// month: see below
Time.accessor("day", "Date");
Time.accessor("hour", "Hours");
Time.accessor("minute", "Minutes");
Time.accessor("second", "Seconds");
Time.accessor("millisecond", "Milliseconds");
// weekday: See below
Time.accessor("epoch", "Time");
// Todo: Some kind of noConflict() thing, in case people have a 'Time' around already.
window.Time = Time;
})();