Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Timezone for timed triggers

I've written a script that utilizes Google's timed triggers. However, I'm having problems getting the script to fire at the correct time. I reside in Sydney (GMT +10) and need the script to fire at noon each day.

I've gone in and set the timezone in the Spreadsheets settings, the Project Properties as well as the global settings for Google Drive but despite all this the script doesn't fire at the right time. It's off by about 6-5 hrs. What is even more frustrating is when I go into the triggers admin panel the timezone is listed as Pacific time (-8hrs) and as that wasn't enough every other time I log on it seem to change to some other random time soon like Eastern standard etc. (within the triggers panel that is).

I've even tried manually compensating for the time difference but that hasn't worked either and it's ideally not the path I want to go down as it affects the day which the script is triggered (it will be Monday in EST time and Tuesday here in Sydney) which has impacts on the DB query I'm running which is date sensitive.

Either way, I've now drawn a blank can't think of any further solutions. How can I address this?

like image 477
The Ginger Fox Avatar asked Jan 09 '14 05:01

The Ginger Fox


1 Answers

I had this problem as well. Possibly because I was traveling. Setting the spreadsheet's timezone didn't help. Setting the script's timezone didn't help either.

What did help was using a modified version of @Rodrigo's script:

function addTrigger() {
  // main() will be called weekly on Monday at 16:00 in the specified time zone
  var everyWeek = ScriptApp.newTrigger("main")
      .timeBased()
      .everyWeeks(1)
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .inTimezone("America/Los_Angeles")
      .atHour(16)
      .create();
}

You can specify the timezone using .inTimezone() -- see description here.

Once you run this script, you can edit the trigger using the GUI editor. Also, now the timezone is correct for new triggers I add using the GUI -- not sure if that's because of the script or because I'm back home now.

like image 144
ems Avatar answered Sep 25 '22 03:09

ems