0

I am building a task scheduling application, and I need two types of events with respect to their time-zone dependency.

By timezone-independent time, I mean a time that is the same regardless of your location. I want to set time to 8:00 AM, and wherever I am, I want to see it as 8:00 AM. I want to use these for routine tasks like when I wake up. Because wanting to wake up at 8:00 AM in New York, doesn't mean wanting to wake up at 5:00 AM in Los Angeles. It's timezone-independent

And by timezone-dependent time, I mean a meeting at 8:00 AM in New York would be at 5:00 AM in Los Angeles. Its meaning depends on the timezone.

Any kind of date and time information in rails is always followed by a timezone suffix. What's a good way to represent timezone-independent time in rails?

mechanical-turk
  • 801
  • 1
  • 8
  • 9
  • 1
    One of the way I've seen it was dealt with was to use 'minutes after midnight' format - change column type to be integer and then parse it to a pure time object. – BroiSatse Nov 08 '15 at 23:52
  • Be sure to read [How to store repeating dates keeping in mind Daylight Savings Time](http://stackoverflow.com/a/19627330/634824), and [Daylight saving time and time zone best practices](http://stackoverflow.com/q/2532729/634824). Yes, you do need a timezone ignorant way to store 08:00. – Matt Johnson-Pint Nov 09 '15 at 00:20
  • @BroiSatse - That works, as long as you use it to calculate the local time *before* applying it to a date in a specific time zone. The problem is, there are local dates without a midnight due to some DST transitions. – Matt Johnson-Pint Nov 09 '15 at 00:22

2 Answers2

1

The conventional way to store time independently of timezone is to store it in UTC, also known as Coordinated Universal Time or Greenwich Mean Time. In Ruby you can convert to this by calling the utc method on a time, e.g.

irb(main):001:0> Time.now.utc
=> 2015-11-08 23:54:21 UTC

UTC time has a timezone offset of zero.

Mori
  • 27,279
  • 10
  • 68
  • 73
  • Well, UTC doesn't change for DST, and its offset is zero, but that doesn't mean it's time zone independent... – Matt Johnson-Pint Nov 08 '15 at 23:59
  • UTC is especially bad with scheduling of recurring events. Consider the alarm clock on my phone that goes of at every morning at 6:00 AM. The recurrence pattern must store 06:00. If instead it stored UTC then if I'm in PST it would store 14:00 UTC, but then during daylight saving time it would be an hour off, since 06:00 PDT is 13:00 UTC. Also if I take my phone with me when I travel, I expect it to go off at 6:00 AM in whatever time zone I'm currently in - not the time zone where I was when I set up the event. – Matt Johnson-Pint Nov 09 '15 at 00:18
1

Since many scheduling scenarios absolutely require a way to represent a time-of-day without a date or time zone, then both the DateTime and Time classes are insufficient.

Consider instead using the Tod (time-of-day) gem:

gem install tod

...

require 'tod'
Tod::TimeOfDay.parse "15:30"   # => 15:30:00

Other examples in the documentation.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575