0

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.

Turner Houghton
  • 496
  • 6
  • 16
  • I am having a hard time understanding a circumstance where this might be necessary. Can you provide us with an example? Also, is it not sufficient to convert everything to UTC and perform the logic with that time? – Michael Sorensen Jun 03 '18 at 21:42
  • I updated the OP. I can't just store everything as UTC because if I schedule an event for 9am local time, I have to guarantee that all future events are at 9am local time (which things like DST mess up if I store it as UTC). – Turner Houghton Jun 03 '18 at 23:03
  • I think I understand the conundrum now. Are you aware of the getTimezoneOffset() function in javascript? It should help you account for the DST issue. – Michael Sorensen Jun 04 '18 at 00:32

1 Answers1

1

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.

rlanvin
  • 6,057
  • 2
  • 18
  • 24