-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
88 lines (68 loc) · 2.08 KB
/
index.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
import dateformat from 'dateformat';
function getLastSundayOfMonth(year, month) {
const lastSunday = new Date();
lastSunday.setYear(year);
lastSunday.setUTCMonth(month);
lastSunday.setUTCDate(31);
while (lastSunday.getDay() !== 0) {
const previousDate = lastSunday.getDate();
lastSunday.setDate(previousDate - 1);
}
return lastSunday;
}
function getDateAtOneAM(date) {
date.setUTCHours(1);
date.setUTCMinutes(0);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
return date;
}
function getLastSundayOfMonthAtOneInMorning(year, month) {
const lastSundayOfMonth = getLastSundayOfMonth(year, month);
return getDateAtOneAM(lastSundayOfMonth).getTime();
}
function getBSTEndEpoch(year) {
return getLastSundayOfMonthAtOneInMorning(year, 9);
}
function getBSTStartEpoch(year) {
return getLastSundayOfMonthAtOneInMorning(year, 2);
}
function isBST(UTCEpoch, UTCYear) {
const BSTStartEpoch = getBSTStartEpoch(UTCYear);
const BSTEndEpoch = getBSTEndEpoch(UTCYear);
if (UTCEpoch < BSTEndEpoch && UTCEpoch >= BSTStartEpoch) {
return true;
}
return false;
}
function addHoursDuringBST(UTCDateTimeString, hoursToAdd) {
const UTCTime = new Date(UTCDateTimeString);
const UTCYear = UTCTime.getUTCFullYear();
const UTCEpoch = UTCTime.getTime();
if (isBST(UTCEpoch, UTCYear)) {
const BSTDate = new Date(UTCEpoch + hoursToAdd * 60 * 60 * 1000);
return BSTDate;
}
return UTCTime;
}
function getDateFormat(format) {
const formatDate = format ? `UTC:${format}` : 'isoUtcDateTime';
return formatDate;
}
function isValidDateTimeString(dateTimeString) {
return !isNaN(Date.parse(dateTimeString));
}
function toUKTime(UTCDateTimeString, format) {
if (!isValidDateTimeString(UTCDateTimeString)) {
return;
}
return dateformat(addHoursDuringBST(UTCDateTimeString, 1), getDateFormat(format));
}
function fromUKTime(UTCDateTimeString, format) {
if (!isValidDateTimeString(UTCDateTimeString)) {
return;
}
return dateformat(addHoursDuringBST(UTCDateTimeString, -1), getDateFormat(format));
}
export { fromUKTime };
export { toUKTime };