[英]DST problems with Google Apps Script and Google calendar
I am creating a school project for managing classes, I have a problem with time zones and DST here is my code: 我正在创建一个学校课程来管理班级,时区和DST存在问题,这是我的代码:
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. 在一切都在一小时前移至10月29日之前,此方法一直有效。
How do I manage DST? 如何管理DST?
EDIT: 编辑:
After setting script to GMT (No daylight saving) things got more consistent and every date vas +00:00 将脚本设置为GMT(无夏令时)后,情况变得更加一致,每个日期均+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. 我的脚本使它成为UTC,但我可以手动编辑或删除它。 Problem here is that I need to do it manually. 这里的问题是我需要手动执行。
I also found .setTimeZone(string) in Google documentation: here , and here 我还在Google文档中找到了.setTimeZone(string): here和here
The problem now is that i cannot make it work, no matter what i tried events would and up as UTC 现在的问题是,无论我尝试了什么,事件都将无法发挥作用,而且就像UTC一样
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. 除非在传递给脚本的Date
对象中指定了脚本,否则Google日历会查看脚本的时区。 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
). 在您的情况下,它不是( toLocaleString
不会修改原始对象,如果这样做,则将字符串传递到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: 我不知道日历服务是否允许不考虑DST,因此尝试解决此问题:
// 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 . 函数convertDateToUTC
基于此答案 。
Using this, I got (begin time was 7 am originally): 使用这个,我得到了(最初时间是早上7点):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.