Search code examples
javascriptnode.jsrecurring-eventsrrule

Generating rrules based on certain timezones


Let's say I have a user of my application set a recurring event. His timezone is America/Denver, so I store that along with an rrule that determines when the recurrences happen.

This works assuming that both my server and all of my users are in the same time zone. However, let's say I have other users, in America/Pheonix and maybe America/New_York who wants to get the occurances of this event the user has defined. I need to be able to create the events using the America/Denver time, but then return them to the user in UTC. Conversely, I also need to calculate recurring events that users in America/New_York defined and return those as UTC to the user.

Is there a library that exists that I can give it a timezone and an rrule and have it generate the recurring events based on that timezone's rules (like respecting DST)? Or maybe a third party API?

EDIT: Here is some clarity to my problem.

Let's say I have a recurring event that occurs every Friday at 9am. If I set this event in Colorado, the times that this event occurs are going to be slightly different than times in Arizona, which doesn't have DST for the 5 months of the year that Colorado does. So in my database, I need to store the time zone, and I need some way to generate the events based on that time zone's rules. This is the part I am stuck on, finding a way to generate the events based on the time zone's rules.


Solution

  • I need some way to generate the events based on that time zone's rules

    No, you don't. The "time zone's rules" are irrelevant to the generation of occurrences. An event at "every Friday at 9am" is going to be at 9am all year round, whether DST is active or not. In other words, you can (and should) completely ignore DST for the rrule part.

    The only thing you need to do is keep the original time zone along with each occurrence - a date and a time is not enough - then simply convert the date/time to another time zone on the fly, when you need to display it to a different user.

    I have used Moment.js and Moment Timezone to work with dates before, but you probably have other options out there.