Skip to content

Commit

Permalink
add TZID property to date-time if available
Browse files Browse the repository at this point in the history
it's not clear if that actually leads to a better user experience than
floating time that we are currently using

Google Calendar seems to work OK with floating time - it is using the
event location to properly display the time if the default timezone of
the calendar is different from that of the event

if we do set TZID Google Calendar will also use it, but it might make
incorrect assumptions about DST (for example America/New York tends to
result in Eastern Time - and not EDT or EST depending on the specific
date)
  • Loading branch information
pirxpilot committed Apr 18, 2024
1 parent ffe1c97 commit 0421405
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sub": true,
"undef": true,
"unused": true,
"esversion": 10
"esversion": 11
}
11 changes: 8 additions & 3 deletions lib/ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ function ts2date(ts) {
return new Date(ts).toISOString().replace(/[-:]/g, '').slice(0, -5);
}

function* ical({ metadata, routes }) {
function tzid(property, tz) {
return tz ? `${property};TZID=${tz}` : property;
}

function* ical({ metadata, routes, options = {} }) {
const dtstamp = ts2date(Date.now());

yield* property('BEGIN', 'VCALENDAR');
Expand All @@ -41,8 +45,9 @@ function* ical({ metadata, routes }) {
yield* property('URL', step.url);
yield* property('LOCATION', step.address);
yield* property('GEO', [step.coordinates.lat, step.coordinates.lon], formatArray);
yield* property('DTSTART', ts2date(step.arrival_time));
yield* property('DTEND', ts2date(step.departure_time));
const tz = options.tzid && step.timezone?.name;
yield* property(tzid('DTSTART', tz), ts2date(step.arrival_time));
yield* property(tzid('DTEND', tz), ts2date(step.departure_time));
yield* property('END', 'VEVENT');
}
yield* property('END', 'VCALENDAR');
Expand Down
20 changes: 16 additions & 4 deletions test/fixtures/simple-trip.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"visit_duration": 0,
"nights": 0,
"arrival_time": 1373274000000,
"departure_time": 1373274000000
"departure_time": 1373274000000,
"timezone": {
"name": "America/Los_Angeles"
}
},
{
"id": "5144f7cbd6e4d26079000009",
Expand All @@ -58,7 +61,10 @@
"driving": 11580,
"nights": 0,
"arrival_time": 1373285580000,
"departure_time": 1373286480000
"departure_time": 1373286480000,
"timezone": {
"name": "America/Los_Angeles"
}
},
{
"id": "5144f7dfd6e4d2607900000a",
Expand All @@ -84,6 +90,9 @@
"nights": 0,
"arrival_time": 1373304660000,
"departure_time": 1373305560000,
"timezone": {
"name": "America/Los_Angeles"
},
"symbol": "Gas Station"
},
{
Expand All @@ -108,10 +117,13 @@
"driving": 6595,
"nights": 0,
"arrival_time": 1373312155000,
"departure_time": 1373312155000
"departure_time": 1373312155000,
"timezone": {
"name": "America/Los_Angeles"
}
}
],
"name": ""
}
]
}
}
41 changes: 41 additions & 0 deletions test/fixtures/simple_tz.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//code42day//Furkot - road trip planner//EN
BEGIN:VEVENT
DTSTAMP:20220529T071758
UID:[email protected]
SUMMARY:San Francisco
LOCATION:San Francisco\, CA
GEO:37.7749295;-122.41941550000001
DTSTART;TZID=America/Los_Angeles:20130708T090000
DTEND;TZID=America/Los_Angeles:20130708T090000
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20220529T071758
UID:[email protected]
SUMMARY:Redding
LOCATION:1510 Gordon Lane\, Redding\, CA 96002\, USA
GEO:40.54720023441049;-122.34375
DTSTART;TZID=America/Los_Angeles:20130708T121300
DTEND;TZID=America/Los_Angeles:20130708T122800
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20220529T071758
UID:[email protected]
SUMMARY:Lane
LOCATION:51-559 Walnut Place\, Springfield\, OR 97477\, USA
GEO:44.04811573082351;-123.046875
DTSTART;TZID=America/Los_Angeles:20130708T173100
DTEND;TZID=America/Los_Angeles:20130708T174600
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20220529T071758
UID:[email protected]
SUMMARY:Portland
DESCRIPTION:Trip notes for first stop
LOCATION:Portland\, OR
GEO:45.5234515;-122.6762071
DTSTART;TZID=America/Los_Angeles:20130708T193555
DTEND;TZID=America/Los_Angeles:20130708T193555
END:VEVENT
END:VCALENDAR
8 changes: 8 additions & 0 deletions test/ical.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('ical', function () {
compareLines(generated, expected);
});

it('simple trip with TZID', function () {
const t = require('./fixtures/simple-trip.json');
const generated = ical({ ...t, options: { tzid: true } });
const expected = readFileSync('fixtures/simple_tz.ics');

compareLines(generated, expected);
});

it('multi trip', function () {
const t = require('./fixtures/multi-trip.json');
const generated = ical(t);
Expand Down

0 comments on commit 0421405

Please sign in to comment.