master
1
2dateEqual = function(a, b, message) {
3 var format = '{yyyy}-{MM}-{dd} {HH}:{mm}:{ss}{tz}'
4 var buffer = 50; // Number of milliseconds of "play" to make sure these tests pass.
5 if(typeof b == 'number') {
6 var d = new Date();
7 d.setTime(d.getTime() + b);
8 b = d;
9 }
10 var offset = Math.abs(a.getTime() - b.getTime());
11 equal(offset < buffer, true, message + ' | expected: ' + b.format(format) + ' got: ' + a.format(format), null, 1);
12}
13
14dateRangeEqual = function(a, b, message) {
15 dateEqual(a.start, b.start, message);
16 dateEqual(a.end, b.end, message);
17}
18
19getRelativeDate = function(year, month, day, hours, minutes, seconds, milliseconds) {
20 var d = this.getFullYear ? new Date(this.getTime()) : new Date();
21 var setYear = d.getFullYear() + (year || 0)
22 var setMonth = d.getMonth() + (month || 0)
23 var setDate = d.getDate() + (day || 0);
24 // Relative dates that have no more specificity than months only walk
25 // the bounds of months, they can't traverse into a new month if the
26 // target month doesn't have the same number of days.
27 if(day === undefined && month !== undefined) {
28 setDate = Math.min(setDate, new Date(setYear, setMonth).daysInMonth());
29 d.setDate(setDate);
30 }
31 d.setFullYear(setYear);
32 d.setMonth(setMonth);
33 d.setDate(setDate);
34 d.setHours(d.getHours() + (hours || 0));
35 d.setMinutes(d.getMinutes() + (minutes || 0));
36 d.setSeconds(d.getSeconds() + (seconds || 0));
37 d.setMilliseconds(d.getMilliseconds() + (milliseconds || 0));
38 return d;
39}
40
41getUTCDate = function(year, month, day, hours, minutes, seconds, milliseconds) {
42 var d = new Date();
43 if(year) d.setFullYear(year);
44 d.setUTCDate(15); // Pre-emptively preventing a month overflow situation
45 d.setUTCMonth(month === undefined ? 0 : month - 1);
46 d.setUTCDate(day === undefined ? 1 : day);
47 d.setUTCHours(hours === undefined ? 0 : hours);
48 d.setUTCMinutes(minutes === undefined ? 0 : minutes);
49 d.setUTCSeconds(seconds === undefined ? 0 : seconds);
50 d.setUTCMilliseconds(milliseconds === undefined ? 0 : milliseconds);
51 return d;
52}
53
54getDateWithWeekdayAndOffset = function(weekday, offset, hours, minutes, seconds, milliseconds) {
55 var d = new Date();
56 if(offset) d.setDate(d.getDate() + offset);
57 d.setDate(d.getDate() + (weekday - d.getDay()));
58 d.setHours(hours || 0);
59 d.setMinutes(minutes || 0);
60 d.setSeconds(seconds || 0);
61 d.setMilliseconds(milliseconds || 0);
62 return d;
63}
64
65getDaysInMonth = function(year, month) {
66 return 32 - new Date(year, month, 32).getDate();
67}
68
69getWeekdayFromDate = function(d, utc) {
70 var day = utc ? d.getUTCDay() : d.getDay();
71 return ['sunday','monday','tuesday','wednesday','thursday','friday','saturday','sunday'][day];
72}
73
74getMonthFromDate = function(d, utc) {
75 var month = utc ? d.getUTCMonth() : d.getMonth();
76 return ['january','february','march','april','may','june','july','august','september','october','november','december'][month];
77}
78
79getHours = function(num) {
80 return Math.floor(num < 0 ? 24 + num : num);
81}
82
83
84toUTC = function(d) {
85 return d.addMinutes(-d.getTimezoneOffset());
86}