-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExecution Time.js
47 lines (41 loc) · 2.02 KB
/
Execution 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
function updateLastExecutionTime(overview) {
const timezones = [
{ timeZone: "America/New_York", abbreviation: "EST" },
{ timeZone: "Europe/London", abbreviation: "GMT" },
{ timeZone: "Europe/Berlin", abbreviation: "CET" },
];
const timezoneCells = ["N5", "N6", "N7"];
const executionTimeCells = ["O5", "O6", "O7"];
for (let i = 0; i < timezones.length; i++) {
const timezoneData = timezones[i];
const date = new Date();
const timeZone = timezoneData.timeZone;
const abbreviation = timezoneData.abbreviation;
const isDaylightSavingTime = isDaylightSavingTimeInTimeZone(date, timeZone);
const timezoneOffset = isDaylightSavingTime ? 2 : 1;
const todayUpdateTime = Utilities.formatDate(date, timeZone, "MMM d, hh:mm aa");
const timezoneCell = overview.getRange(timezoneCells[i]);
const executionTimeCell = overview.getRange(executionTimeCells[i]);
timezoneCell.setValue(abbreviation);
executionTimeCell.mergeAcross().setValue(todayUpdateTime);
}
}
function isDaylightSavingTimeInTimeZone(date, timeZone) {
const dateInTimeZone = Utilities.formatDate(date, timeZone, "yyyy-MM-dd HH:mm:ss");
const formattedDate = new Date(dateInTimeZone);
const januaryDate = new Date(formattedDate.getFullYear(), 0, 1);
const julyDate = new Date(formattedDate.getFullYear(), 6, 1);
return (
formattedDate.getTimezoneOffset() !==
Math.max(januaryDate.getTimezoneOffset(), julyDate.getTimezoneOffset())
);
}
function calculateTimeDifferenceInTimeZone(currentTime, timeZoneCell) {
const timeZone = timeZoneCell.getValue();
const timeZoneOffsetInMinutes = new Date(timeZone).getTimezoneOffset();
const currentTimeInTimeZone = new Date(currentTime - timeZoneOffsetInMinutes * 60 * 1000);
const timeDifference = currentTimeInTimeZone - new Date(timeZoneCell.offset(0, 1).getValue());
const hours = Math.floor(timeDifference / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
return hours + " hours and " + minutes + " minutes ago";
}