mechanical-turk
mechanical-turk

Reputation: 801

What's a good way to deal with timezone-independent time in Rails

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?

Upvotes: 0

Views: 856

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

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.

Upvotes: 1

Mori
Mori

Reputation: 27779

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.

Upvotes: 1

Related Questions