UNPKG

6.79 kBJavaScriptView Raw
1import { __extends } from "tslib";
2import { Frequency } from './types';
3import { pymod, divmod, empty, includes } from './helpers';
4import { dateutil } from './dateutil';
5var Time = /** @class */ (function () {
6 function Time(hour, minute, second, millisecond) {
7 this.hour = hour;
8 this.minute = minute;
9 this.second = second;
10 this.millisecond = millisecond || 0;
11 }
12 Time.prototype.getHours = function () {
13 return this.hour;
14 };
15 Time.prototype.getMinutes = function () {
16 return this.minute;
17 };
18 Time.prototype.getSeconds = function () {
19 return this.second;
20 };
21 Time.prototype.getMilliseconds = function () {
22 return this.millisecond;
23 };
24 Time.prototype.getTime = function () {
25 return ((this.hour * 60 * 60 + this.minute * 60 + this.second) * 1000 +
26 this.millisecond);
27 };
28 return Time;
29}());
30export { Time };
31var DateTime = /** @class */ (function (_super) {
32 __extends(DateTime, _super);
33 function DateTime(year, month, day, hour, minute, second, millisecond) {
34 var _this = _super.call(this, hour, minute, second, millisecond) || this;
35 _this.year = year;
36 _this.month = month;
37 _this.day = day;
38 return _this;
39 }
40 DateTime.fromDate = function (date) {
41 return new this(date.getUTCFullYear(), date.getUTCMonth() + 1, date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.valueOf() % 1000);
42 };
43 DateTime.prototype.getWeekday = function () {
44 return dateutil.getWeekday(new Date(this.getTime()));
45 };
46 DateTime.prototype.getTime = function () {
47 return new Date(Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second, this.millisecond)).getTime();
48 };
49 DateTime.prototype.getDay = function () {
50 return this.day;
51 };
52 DateTime.prototype.getMonth = function () {
53 return this.month;
54 };
55 DateTime.prototype.getYear = function () {
56 return this.year;
57 };
58 DateTime.prototype.addYears = function (years) {
59 this.year += years;
60 };
61 DateTime.prototype.addMonths = function (months) {
62 this.month += months;
63 if (this.month > 12) {
64 var yearDiv = Math.floor(this.month / 12);
65 var monthMod = pymod(this.month, 12);
66 this.month = monthMod;
67 this.year += yearDiv;
68 if (this.month === 0) {
69 this.month = 12;
70 --this.year;
71 }
72 }
73 };
74 DateTime.prototype.addWeekly = function (days, wkst) {
75 if (wkst > this.getWeekday()) {
76 this.day += -(this.getWeekday() + 1 + (6 - wkst)) + days * 7;
77 }
78 else {
79 this.day += -(this.getWeekday() - wkst) + days * 7;
80 }
81 this.fixDay();
82 };
83 DateTime.prototype.addDaily = function (days) {
84 this.day += days;
85 this.fixDay();
86 };
87 DateTime.prototype.addHours = function (hours, filtered, byhour) {
88 if (filtered) {
89 // Jump to one iteration before next day
90 this.hour += Math.floor((23 - this.hour) / hours) * hours;
91 }
92 while (true) {
93 this.hour += hours;
94 var _a = divmod(this.hour, 24), dayDiv = _a.div, hourMod = _a.mod;
95 if (dayDiv) {
96 this.hour = hourMod;
97 this.addDaily(dayDiv);
98 }
99 if (empty(byhour) || includes(byhour, this.hour))
100 break;
101 }
102 };
103 DateTime.prototype.addMinutes = function (minutes, filtered, byhour, byminute) {
104 if (filtered) {
105 // Jump to one iteration before next day
106 this.minute +=
107 Math.floor((1439 - (this.hour * 60 + this.minute)) / minutes) * minutes;
108 }
109 while (true) {
110 this.minute += minutes;
111 var _a = divmod(this.minute, 60), hourDiv = _a.div, minuteMod = _a.mod;
112 if (hourDiv) {
113 this.minute = minuteMod;
114 this.addHours(hourDiv, false, byhour);
115 }
116 if ((empty(byhour) || includes(byhour, this.hour)) &&
117 (empty(byminute) || includes(byminute, this.minute))) {
118 break;
119 }
120 }
121 };
122 DateTime.prototype.addSeconds = function (seconds, filtered, byhour, byminute, bysecond) {
123 if (filtered) {
124 // Jump to one iteration before next day
125 this.second +=
126 Math.floor((86399 - (this.hour * 3600 + this.minute * 60 + this.second)) / seconds) * seconds;
127 }
128 while (true) {
129 this.second += seconds;
130 var _a = divmod(this.second, 60), minuteDiv = _a.div, secondMod = _a.mod;
131 if (minuteDiv) {
132 this.second = secondMod;
133 this.addMinutes(minuteDiv, false, byhour, byminute);
134 }
135 if ((empty(byhour) || includes(byhour, this.hour)) &&
136 (empty(byminute) || includes(byminute, this.minute)) &&
137 (empty(bysecond) || includes(bysecond, this.second))) {
138 break;
139 }
140 }
141 };
142 DateTime.prototype.fixDay = function () {
143 if (this.day <= 28) {
144 return;
145 }
146 var daysinmonth = dateutil.monthRange(this.year, this.month - 1)[1];
147 if (this.day <= daysinmonth) {
148 return;
149 }
150 while (this.day > daysinmonth) {
151 this.day -= daysinmonth;
152 ++this.month;
153 if (this.month === 13) {
154 this.month = 1;
155 ++this.year;
156 if (this.year > dateutil.MAXYEAR) {
157 return;
158 }
159 }
160 daysinmonth = dateutil.monthRange(this.year, this.month - 1)[1];
161 }
162 };
163 DateTime.prototype.add = function (options, filtered) {
164 var freq = options.freq, interval = options.interval, wkst = options.wkst, byhour = options.byhour, byminute = options.byminute, bysecond = options.bysecond;
165 switch (freq) {
166 case Frequency.YEARLY: return this.addYears(interval);
167 case Frequency.MONTHLY: return this.addMonths(interval);
168 case Frequency.WEEKLY: return this.addWeekly(interval, wkst);
169 case Frequency.DAILY: return this.addDaily(interval);
170 case Frequency.HOURLY: return this.addHours(interval, filtered, byhour);
171 case Frequency.MINUTELY: return this.addMinutes(interval, filtered, byhour, byminute);
172 case Frequency.SECONDLY: return this.addSeconds(interval, filtered, byhour, byminute, bysecond);
173 }
174 };
175 return DateTime;
176}(Time));
177export { DateTime };
178//# sourceMappingURL=datetime.js.map
\No newline at end of file