inTimeZone not working when setting up new time based triggers in google scripts

139 views
Skip to first unread message

Dan Gorlitsky

unread,
Apr 3, 2019, 10:51:04 AM4/3/19
to Google Apps Script Community
Hi Everyone.

I am using inTimeZone when creating a new time-based trigger but google scripts are completely disregarding this, and using a different time zone (not local, and not project time zone)

ScriptApp.newTrigger("Timer_NZ_T1").timeBased().atHour(T1).nearMinute(16).everyDays(1).inTimezone("Pacific/Auckland").create();

For example, this was supposed to use New Zealand's time zone but it didnt, any idea how this can be fixed

Alan Wells

unread,
Apr 3, 2019, 12:00:46 PM4/3/19
to Google Apps Script Community
What time zone did it use?  What is the offset?

Reno Blair

unread,
Apr 3, 2019, 12:47:11 PM4/3/19
to google-apps-sc...@googlegroups.com
I remember this coming up in a post a while back. I decided see what we could do with the timezone string you are using here and came up with this:


function createTrigger() {
var t = getAdjustedTriggerTime("Pacific/Auckland", 4, 0);
ScriptApp.newTrigger("Timer_NZ_T1")
.timeBased()
.atHour(t.hours)
.nearMinute(t.minutes)
.everyDays(1)
.create();
}

function getTimezoneAdjustment(targetTimezoneString) {
var n =
Number(Utilities.formatDate(new Date(), targetTimezoneString, "Z")) -
Number(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "Z"));
return {
hours: Math.floor(n / 100) + (n < 0 ? 1 : 0),
minutes: n % 100,
};
}

function getAdjustedTriggerTime(timezoneString, hours, minutes) {
var hoursPerDay = 24;
var minutesPerHour = 60;
var adjustment = getTimezoneAdjustment(timezoneString);
hours -= adjustment.hours;
minutes -= adjustment.minutes;
if (Math.abs(minutes) > minutesPerHour) {
hours += minutes < 0 ? -1 : 1;
}
return {
hours: (hours + hoursPerDay) % hoursPerDay,
minutes: (minutes + minutesPerHour) % minutesPerHour,
};
}


I used format date to easily consume the timezone string originally used. It could probably be optimized, but it seems to work for various target timezone strings and times based on my quick testing.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/a1c85808-0be7-4cfb-91b2-7baecce93c83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Reno Blair
Educational Technology Services

Reno Blair

unread,
Apr 3, 2019, 2:45:05 PM4/3/19
to google-apps-sc...@googlegroups.com
After a little more testing, I found that my timezone adjustment calculation sometimes returns incorrect values in some cases involving timezones that are not on the hour. So here's a function to convert a timezone string to a timezone offset consistent with the value returned from getTimezoneOffset Date method. And an updated getTimezoneAdjustment function.

function getTimezoneOffsetForTimezoneString(timezoneString) {
var minutesPerHour = 60;
var offsetString = Utilities.formatDate(new Date(), timezoneString, "Z");
return (
(offsetString.slice(0, 1) === "-" ? 1 : -1) *
(Number(offsetString.slice(1, 3)) * minutesPerHour + Number(offsetString.slice(-2)))
);
}

function getTimezoneAdjustment(targetTimezoneString) {
var minutesPerHour = 60;
var n = new Date().getTimezoneOffset() - getTimezoneOffsetForTimezoneString(targetTimezoneString);
return {
hours: Math.floor(n / minutesPerHour) + (n < 0 ? 1 : 0),
minutes: n % minutesPerHour,
};
}

Dan Gorlitsky

unread,
Apr 23, 2019, 12:05:09 PM4/23/19
to Google Apps Script Community
Thank you for this solution.
Any idea why Google built in function is not working.
The other issue is that the solution you offered will not automatically adjust for winter and summer clock changes.

Any recommendation?

Dan

unread,
Aug 3, 2020, 7:34:39 AM8/3/20
to Google Apps Script Community
Any update on this bug, why is the builtin google fucntion of intimezone not working?
Reply all
Reply to author
Forward
0 new messages