-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allDay option doesn't reflect timezone #38
Comments
Thank you for your exemplary bug report, i think I get your point here. Unfortunately, the solution is not a real one: Case 1 with local timeical({
domain: 'sebbo.net',
prodId: '//superman-industries.com//ical-generator//EN',
timezone: 'Europe/Berlin',
events: [{
start: new Date('Sun May 01 2016 00:00:00 GMT+0200 (CEST)'),
summary: 'Example Event',
allDay: true
}]
}) The start time would be Case 2 with local timeical({
domain: 'sebbo.net',
prodId: '//superman-industries.com//ical-generator//EN',
timezone: 'Brazil Time',
events: [{
start: new Date('Sun May 01 2016 00:00:00 UTC'),
summary: 'Example Event',
allDay: true
}]
}) In this case, the start time would be To be onest, I have no idea how to handle this properly. But I think using local time is not a solution we should rely on here (UTC-X etc.) |
Here's a sketch of one approach to take. Use moment-timezone, which is a widely-used date library that also understands timezones. It provides an object, 'moment', which wraps the JavaScript native Date object but also provides new functionality, which means it's backwards compatible while still providing extra, better, functionality. For more info the front page of http://momentjs.com/ and the docs are pretty straightforward. With this library (and the patch below) we can use timezones in three ways:
A replacement formatDate function would look like: var moment = require('moment-timezone');
function formatDate(opener, inputDate, allday, timezone) {
// Wrap the date in a moment object if it's not already one, so we can use its formatting & timezone functions - ideally this should be done when the start or end date is set on the event object
var date = moment(inputDate);
var line = opener;
if (allday) {
// All day events can't take timezones, see 3.2.19 of ical spec
line += ';VALUE=DATE:' + date.format('YYYYMMDD');
} else {
// Use the moment timezone, falling back on the timezone property of event
var timezone = date.tz() || timezone;
if (timezone) {
line += ';TZID=' + timezone;
} else {
// Set Moment's UTC mode to output with no TZ offset
date.utc();
}
line += ':' + date.format('YYYYMMDDTHHmmss');
if (!timezone)
line += 'Z';
}
return line;
} And then the relevant part of the event output function (event.js:652-665]) would be replaced with: g += formatDate('DTSTART', data.start, data.allDay, data.timezone) + '\r\n';
if (data.end)
g += formatDate('DTEND', data.end, data.allDay, data.timezone) + '\r\n';
}
if (data.allDay) {
g += 'X-MICROSOFT-CDO-ALLDAYEVENT:TRUE\r\n';
g += 'X-MICROSOFT-MSNCALENDAR-ALLDAYEVENT:TRUE\r\n';
} Testing in the Node REPL with a few different parameters seems to do the right things: > formatDate('DTSTART', moment.tz('2016-05-01 00:00', 'Europe/Berlin'), true)
'DTSTART;VALUE=DATE:20160501'
> formatDate('DTSTART', moment.tz('2016-05-01 00:00', 'Brazil/West'), true)
'DTSTART;VALUE=DATE:20160501'
> formatDate('DTSTART', new Date('Sun May 01 2016 00:00:00 UTC'), true, 'Brazil/West')
'DTSTART;VALUE=DATE:20160501'
> formatDate('DTSTART', moment.tz('2016-05-01 07:00', 'Brazil/West'))
'DTSTART;TZID=Brazil/West:20160501T070000'
> formatDate('DTSTART', new Date('Sun May 01 2016 00:00:00 UTC'))
'DTSTART:20160501T000000Z'
> formatDate('DTSTART', new Date('Sun May 01 2016 00:00:00 GMT+0200'))
'DTSTART:20160430T220000Z' |
Thanks for your quick and detailed response.
|
Using the iCal generator on http://apps.marudot.com/ical/ you're asked to set your timezone before generating the file which lead me to believe that it makes use of that factor for "all day" events. I think the only factors in play in the file generated below are
|
ical-generator 3.0 now uses moment-timezone, so this issue should be fixed. I also added the above example in the unit tests. You can test the current develop with |
Great news @sebbo2002 ! Is the 3.0 version still available ? Because running |
@billyshena 3.x is now our main version, so you can run |
Just got my version updated, thanks @sebbo2002 ! |
@sebbo2002 We are facing a small issue about timezone parsing. Here is an example: The Event is well formatted in the Europe/Paris timezone
The Event is still formatted in Europe/Paris timezone
I think that the Event timezone should "always take over" the timezone set on the Calendar one. Thanks, |
build: bump node from `f07ead7` to `cc1a31b`
First of all thanks for this awesome lib.
I noticed that the
allday
option doesn't work properly.ical-generator uses the UTC Time to generate the full day.
In a time Zone (e.g.
Europe/Berlin
) this might result in a different day.Example:
Code to reproduce:
Solution:
Normally using UTC Dates on the server is of course the right way to go.
But since the value type
Date
is not able to reflect the timezone properly, I suggest to use local time on this specific use case instead.Affected Code
The text was updated successfully, but these errors were encountered: