3

I am creating a school project for managing classes, I have a problem with time zones and DST here is my code:

javascript
dejt = new Date(thisYear, 8, day, beginTime, endTime, 0, 0);
dejt.toLocaleString("en-US", {timezone: "Europe/Belgrade"});

And I pass I through some functions until:

javascript
function makeCalendar(name, begin, end, calendar) {
    var eventSeries = CalendarApp.getCalendarById(calendar).createEventSeries(
        name,
        begin,
        end,
        CalendarApp.newRecurrence()
            .addWeeklyRule().interval(2).until(endDate)
    );
}

This works well until it hits October 29th after that everything is moved one hour earlier.

How do I manage DST?

EDIT:

After setting script to GMT (No daylight saving) things got more consistent and every date vas +00:00

Logs:
[19-02-12 11:30:15:809 CET] Sun Feb 17 00:00:00 GMT+00:00 2019
[19-02-12 11:30:15:810 CET] Mon Feb 18 00:00:00 GMT+00:00 2019
[19-02-12 11:30:24:051 CET] Mon Sep 03 14:00:00 GMT+00:00 2018
[19-02-12 11:30:24:503 CET] Mon Sep 03 14:50:00 GMT+00:00 2018

Now only problem is that recurring events just change time when daylight savings should be (Not a new bug, it was like this before).

EDIT:

New stuff I found out: If you edit events manually there is time zone box; My script makes it UTC but i can manually edit or remove it. Problem here is that I need to do it manually.

I also found .setTimeZone(string) in Google documentation: here, and here

The problem now is that i cannot make it work, no matter what i tried events would and up as UTC

Predvodnik
  • 41
  • 6
  • Could you share how exactly you pass your begin and end dates into `makeCalendar`? In my case with `var beginDate = new Date(thisYear, 8, day, beginTime, 0, 0, 0); var endDate = new Date(thisYear, 8, day, endTime, 0, 0, 0);` it was fine and there was no issue with DST – a-change Feb 11 '19 at 20:25
  • There is a lot of passing, but I can provide the whole code if you are interested – Predvodnik Feb 11 '19 at 20:42
  • Yes, that could be helpful, I think – a-change Feb 11 '19 at 20:45
  • I edited post with code – Predvodnik Feb 11 '19 at 20:52
  • thanks! actually, yeah, the thing was about my script's time zone, not about the full code of yours (probably you can remove it from the post). Looks like I came up with a solution, see the answer below – a-change Feb 12 '19 at 10:16

1 Answers1

0

Ok, after some digging into this, looks like there's something like a solution.

Google Calendar will look at script's timezone unless it is specified in the Date object you're passing into it. In your case it is not (toLocaleString will not modify the original object, and if it would, you'd run into errors with passing strings into createEventSeries). Anyway, your script's timezone seems to be Belgrade, and there's DST, time changes, etc.

Finally, possible solution. I don't know if Calendar service allows not to take DST in consideration so trying to get round the issue:

// function to convert date to UTC so that it retains original hours value
function convertDateToUTC(date, timezoneOffset) { 
    return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours() + timezoneOffset, date.getUTCMinutes(), date.getUTCSeconds()); 
}
// 1 is the timezone offset from GMT
makeCalendar('test event', convertDateToUTC(beginDate, 1), convertDateToUTC(endDate, 1), calendar.getId());

Function convertDateToUTC is based on this answer.

Using this, I got (begin time was 7 am originally):

calendar screenshot

a-change
  • 656
  • 5
  • 12
  • It still does not work Logs: [19-02-12 11:26:57:824 CET] Sun Feb 17 01:00:00 GMT+01:00 2019 [19-02-12 11:26:57:825 CET] Mon Feb 18 01:00:00 GMT+01:00 2019 [19-02-12 11:27:05:309 CET] Mon Sep 03 16:00:00 GMT+02:00 2018 [19-02-12 11:27:05:743 CET] Mon Sep 03 16:50:00 GMT+02:00 2018 – Predvodnik Feb 12 '19 at 10:28
  • Is it still the old way: before 29th of October time is correct, after — one hour earlier? The dates in the logs, how are they different from the ones you expect? – a-change Feb 12 '19 at 11:12
  • It's same problem but now every date has same offset, before this dates had different offsets no matter what I did and this is important for different part of the code that handled breaks – Predvodnik Feb 12 '19 at 11:22