Fuhrmanator
Fuhrmanator

Reputation: 12922

Specify the Olson ID in a new Date()

In Google's example for createEventSeries (shown below), the Date values are specified with "EST" at the end. However, I want to specify dates for a recurring event (see type #4 of the kinds of times in this answer), e.g., "every Monday at 9pm, even when DST changes." Let's say I can figure out which day is the first Monday of my recurring event. The problem is how do I specify that 9pm of that day should take into consideration whether DST is in force?

It would be intuitive to specify a Date using a time zone name (Olson ID), e.g., using the date from the example, "July 21, 2010 09:00:00 America/Montreal". The resulting Date has a bogus value in 1969 if I try it in Google Apps Script.

How can I specify a new Date() in Google Apps Script such that it groks the Olson ID I specify, e.g., "America/Montreal" as opposed to me knowing a priori if the date is actually "EST" or "EDT"?

// The code below will add an event to the user's default Calendar that recurs every other day 
var cal = CalendarApp.getDefaultCalendar(); cal.createEvent("Busy", new Date("July 21, 2010 08:00:00 EST"), new Date("July 21, 2010 09:00:00 EST"), CalendarApp.newRecurrence().addDailyRule().interval(2), {location:'Nap room'});`

p.s. I found a workaround/hack that relies on the information that JavaScript new Date() in GAS will inherit the timezone of the script (see the 4th bullet point). I can set my script's timezone to that of the one I want to create my recurring events, and it appears to work. But I'd like to not rely on this detail, as it seems fragile.

Edit

Here's the output of a small test using the workaround to show you how it works. Note how the GMT-0500 (EST) changes to GMT-0400 (EDT).

function shortTest() {
  // DST begins on Mar 10, 2012 in "America/Montreal" (Olson ID)
  // This script's Project Properties show a Time zone (today, Dec 18, 2012) of 
  //    "(GMT-05:00) Eastern Time - Montreal"
  Logger.log("Date = " + (new Date("March 9, 2013 21:00:00")));
  Logger.log("Date = " + (new Date("March 10, 2013 21:00:00")));
  Logger.log("Date = " + (new Date("March 11, 2013 21:00:00")));
}

Logger output

Date = Sat Mar 09 2013 21:00:00 GMT-0500 (EST)
Date = Sun Mar 10 2013 21:00:00 GMT-0400 (EDT)
Date = Mon Mar 11 2013 21:00:00 GMT-0400 (EDT)

Upvotes: 3

Views: 678

Answers (1)

Peter
Peter

Reputation: 5601

How can I specify a new Date() in Google Apps Script such that it groks the Olson ID I specify

You can use Session.getTimeZone() or Calendar#getTimeZone to get a correctly interpretable string that represents the timezone id of the script or calendar respectively.

See also the answer to Errors with Utilties.FormatDate which refers to the relevant underlying TimeZone Java doc.

You could specify your core date/time in UTC and then display it in the timezone you want via Utilities.formatDate but that doesn't answer your question.

To the heart of your question, the problem is there's no lookup table or function to convert GMT offsets to timezone strings unless you use the one deep in your operating system maintained by the OS maintainer. The way Javacript Date works is that is uses the local computer's time zone (in our case the GAS server's that you can alter via the script's Time Zone project property setting as you've seen) OR you can specify the GMT offset directly on the Date constructor.

So at the time of creating the date/time using the Date() constructor you need to a) choose what GMT offset to use or b) accept the script's project settings Time Zone.

Upvotes: 0

Related Questions