-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiso8601-localizer.js
More file actions
307 lines (302 loc) · 13.6 KB
/
iso8601-localizer.js
File metadata and controls
307 lines (302 loc) · 13.6 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/// <reference path="typings/tsd.d.ts" />
var arrays = require('./lib/arrays');
var classes = require('./lib/classes');
var ISO8601Localizer = (function () {
function ISO8601Localizer(userISO8601) {
// Partially Stricted(no ^ and $): 2013-01-05T04:13:00
this.ISO8601Pattern = /(\d{4})\-([0-1][0-9])\-([0-3][0-9])T([0-2][0-9])\:([0-5][0-9])\:([0-5][0-9])/;
this.userISO8601 = userISO8601;
this.userOffset = new Date().getTimezoneOffset() / -60;
}
ISO8601Localizer.prototype.to = function (offset) {
if (!this.validOffset(offset)) {
this.errorThrower(0);
}
this.userOffset = offset;
return this;
};
// There are few variables set here for clarification and never used such as: newerDay, newerMonth and newYear.
ISO8601Localizer.prototype.localize = function () {
if (typeof this.userISO8601 !== 'string') {
this.errorThrower(6);
}
var upperCaseISO8601 = this.userISO8601.toUpperCase();
if (!this.isValid(upperCaseISO8601)) {
this.errorThrower(1);
}
var _a = this.getOffset(), offsetHours = _a.offsetHours, operator = _a.operator;
var matchStrings = upperCaseISO8601.match(this.ISO8601Pattern);
var fullMatch = matchStrings.shift();
var matchNumbers = matchStrings.map(function (val) {
return parseInt(val);
});
var year = matchNumbers[0], month = matchNumbers[1], day = matchNumbers[2], hour = matchNumbers[3], minute = matchNumbers[4], second = matchNumbers[5];
var leapYear = this.isLeapYear(year);
// -1 is because arrays.monthsDays have 0 index.
var daysInMonth = arrays.monthsDays[month - 1];
if (leapYear && month === 2) {
daysInMonth = 29; // daysInMonth = 29 instead of daysInMonth++ is used for expressivity.
}
if (!this.isLogical(daysInMonth, day)) {
this.errorThrower(2);
}
// DIM stands for days in month, its use is explained inside the operator === '-' if statement.
var previousMonthDIM = (function () {
// The -2 used because -1 due to arrays.monthsDays have 0 index and -1 because we need the previous month.
if (month - 2 < 0) {
return arrays.monthsDays[12 + (month - 2)];
}
return arrays.monthsDays[month - 2];
})();
if (leapYear && month === 3) {
previousMonthDIM = 29; // daysInMonth = 29 instead of daysInMonth++ is used for expressivity.
}
if (operator === '=') {
return fullMatch;
}
if (this.isFloat(offsetHours)) {
var offsetHoursRemainder = this.getRemainder(offsetHours);
var newMinute = 0;
var remainderMinutes = 0;
switch (offsetHoursRemainder) {
// 45 minutes
case 45:
/*
When using .to() method the user must use .3 or .45 BUT when getTimezoneOffset value is
used(when not using the to method), well lets take for example +5.45, the number of minutes returned
is 60*5+45=345, now let divide 345/60=5.75, the fraction is .75, the case 45 is as explained because
the use must use .3 or .45.
*/
case 75:
remainderMinutes = 45;
break;
// 30 minutes, users can send .30, javascript will convert it to 0.3
case 3:
// Same explanation as case 75, this case +5.3 is 60*5+30=330 and 330/60=5.5
case 5:
remainderMinutes = 30;
break;
default:
this.errorThrower(3);
}
if (operator === '+') {
newMinute = minute + remainderMinutes;
}
if (operator === '-') {
newMinute = minute - remainderMinutes;
}
/*
The Math.ceil(offsetHours) is used instead of Math.floor(offsetHours) + 1.
The floor is used to cancel the fraction and the +1 because newMinute > 59
or newMinute < 0 either way the system is using absolute offsetHours
(the system check operator variable).
*/
if (newMinute > 59) {
minute = newMinute - 60; // ---------- Final
offsetHours = Math.ceil(offsetHours);
}
else if (newMinute < 0) {
minute = newMinute + 60; // ---------- Final
offsetHours = Math.ceil(offsetHours);
}
else {
minute = newMinute; // ---------- Final
offsetHours = Math.floor(offsetHours);
}
}
// The following actions will take place only for integers offsets.
if (operator === '+') {
var newHour = hour = hour + offsetHours; // ---------- Final(maybe)
if (newHour > 23) {
var addDays = Math.floor(newHour / 24);
var remainingHours = hour = newHour % 24; // ---------- Final
var newDay = day = day + addDays; // ---------- Final(maybe)
if (newDay > daysInMonth) {
var newerDay = day = newDay - daysInMonth; // ---------- Final
var newMonth = month = month + 1; // ---------- Final(maybe)
if (newMonth > 12) {
var newerMonth = month = 1; // ---------- Final
var newYear = year = year + 1; // ---------- Final
}
}
}
}
if (operator === '-') {
var newHour = hour = hour - offsetHours; // ---------- Final(maybe)
if (newHour < 1) {
/*
I use + 24 so decreaseDays and remainingHours will calculate the currect value,
notice that this block statement is entered only when I need to decrease a day, so
the + 24 is used to artificially "show" the calculations that a day was passed(in our case
the previous day wasn't pass).
- decreaseDays divide the newHour by 24 then floor the result to get the number
of days.
- remainingHours will modulus the newHour to get the number of remaining hours to set
after it decreased a day or more.
There is a special case where newHour equals to 0, for that case the decreaseDays
calculation fails, and as result also newDay calculation fails, so before I calculate
newDay I check to see if the newHour equals 0, if so I manually set decreaseDays to 1.
*/
newHour = Math.abs(newHour) + 24;
var decreaseDays = Math.floor(newHour / 24);
var remainingHours = hour = 24 - (newHour % 24); // ---------- Final(maybe)
if (newHour === 0) {
decreaseDays = 1;
/*
When newHour % 24 equals 0, 24 - 0 is 24, so when newHour equals 0, I set the hour
to 0, for now there is no need to set remainingHours, but I want both variables to have
the same values, maybe it will help later.
*/
remainingHours = hour = 0; // ---------- Final
}
var newDay = day = day - decreaseDays; // ---------- Final(maybe)
/*
This if statement is very special, if newDay is smaller than 1, then it set the
newerDay and the day to be..... previousMonthDIM + newDay and not newDay - daysInMonth.
The + and not - is because newDay may be negative, previousMonthDIM and not daysInMonth
because we decrease days but from the maximum number of days of the previous month, remember
when operator equals '-' we going backwards.
When newDay equals 0, no days will be decreased from the previous month max days.
*/
if (newDay < 1) {
var newerDay = day = previousMonthDIM + newDay; // ---------- Final
var newMonth = month = month - 1; // ---------- Final(maybe)
if (newMonth < 1) {
var newerMonth = month = 12; // ---------- Final
var newYear = year = year - 1; // ---------- Final
}
}
}
}
var ISO8601 = [year, month, day, hour, minute, second].slice(0);
if (this.userReturnAs) {
switch (this.userReturnAs) {
case 'object':
return this.returnAsObject(ISO8601);
default:
return this.returnAsString(ISO8601);
}
}
else {
return this.returnAsString(ISO8601);
}
};
ISO8601Localizer.prototype.returnAs = function (as) {
if (typeof as !== 'string') {
this.errorThrower(7);
}
as = as.toLowerCase();
if (!this.validReturnAs(as)) {
this.errorThrower(5);
}
this.userReturnAs = as;
return this;
};
ISO8601Localizer.prototype.validReturnAs = function (returnAs) {
return (arrays.returnAsTypes.indexOf(returnAs) > -1) ? true : false;
};
ISO8601Localizer.prototype.returnAsString = function (ISO8601) {
var stringedISO8601Object = this.returnAsObject(ISO8601);
return stringedISO8601Object.year + '-' +
stringedISO8601Object.month + '-' +
stringedISO8601Object.day + 'T' +
stringedISO8601Object.hour + ':' +
stringedISO8601Object.minute + ':' +
stringedISO8601Object.second;
};
ISO8601Localizer.prototype.returnAsObject = function (ISO8601) {
var stringedISO8601Object = { year: '', month: '', day: '', hour: '', minute: '', second: '' };
var ISO8601ParameterOrder = ['year', 'month', 'day', 'hour', 'minute', 'second'];
for (var k in ISO8601) {
var toStringISO8601 = ISO8601[k].toString();
stringedISO8601Object[ISO8601ParameterOrder[k]] = (toStringISO8601.length === 1 ? "0" + toStringISO8601 : toStringISO8601);
}
return stringedISO8601Object;
};
ISO8601Localizer.prototype.getRemainder = function (n) {
return parseInt(n.toString().split('.')[1]);
};
ISO8601Localizer.prototype.isFloat = function (n) {
return n === Number(n) && n % 1 !== 0;
};
ISO8601Localizer.prototype.errorThrower = function (errorCode) {
var error = '';
var className = this.constructor.toString().match(/\w+/g)[1];
switch (errorCode) {
case 0:
error = 'Invalid offset supplied, valid offsets are between -12 to 14';
break;
case 1:
error = 'Invalid ISO8601, try something like(case insensitive, T may be t): 2005-06-03T13:04:32';
break;
case 2:
error = 'Non logical date, please check that there are X days in month Y.';
break;
case 3:
error = 'Unknown offset fraction, internal error, please contact the code author.';
break;
case 4:
error = 'to method parameter type is not a number.';
break;
case 5:
error = 'Invalid string argument supplied to returnAs method.';
break;
case 6:
error = 'Constructor parameter type is not a string.';
break;
case 7:
error = 'returnAs method parameter type is not a string.';
break;
default:
error = 'Unknow error code.';
}
throw (className + ': ' + error);
};
ISO8601Localizer.prototype.isLogical = function (maxDays, day) {
return day <= maxDays;
};
ISO8601Localizer.prototype.getOffset = function () {
var offset = this.userOffset;
return {
operator: (function () {
if (offset > 0) {
return '+';
}
else if (offset < 0) {
return '-';
}
else {
return '=';
}
})(),
offsetHours: Math.abs(offset)
};
};
ISO8601Localizer.prototype.validOffset = function (offset) {
if (typeof offset !== 'number') {
this.errorThrower(4);
}
var RangerInstance = new classes.Ranger();
var validIntegerOffsets = RangerInstance.getRange(-12, 14);
var validOffsets = validIntegerOffsets.concat(arrays.floatingPointOffsets);
return (validOffsets.indexOf(offset) > -1) ? true : false;
};
ISO8601Localizer.prototype.isValid = function (maybeValid) {
return this.ISO8601Pattern.test(maybeValid);
};
ISO8601Localizer.prototype.isLeapYear = function (year) {
/*
Mathisfun(https://www.mathsisfun.com/leap-years.html):
"
Leap Years are any year that can be evenly divided by 4 (such as 2012, 2016, etc),
except if it can can be evenly divided by 100, then it isn't (such as 2100, 2200, etc),
except if it can be evenly divided by 400, then it is (such as 2000, 2400).
"
*/
return ((year % 4 === 0 && year % 100 !== 0)
||
(year % 4 === 0 && year % 100 === 0 && year % 400 === 0));
};
return ISO8601Localizer;
})();
module.exports = ISO8601Localizer;