Module: RailsUtil::TimezoneHelper

Defined in:
lib/rails_util/timezone_helper.rb

Overview

RailsUtil::TimezoneHelper contains helper methods for converting a time between timezones. Uses ActiveSupport::TimeZone timezone.tzinfo.name names (e.g., 'America/Chicago') to identify timezones.

Constant Summary collapse

SECONDS_PER_HOUR =
3600

Class Method Summary collapse

Class Method Details

.convert_timezone(time, to_timezone, from_timezone, **options) ⇒ Object

Converts a time between a from_timezone and a to_timezone

Parameters:

  • time (Time, DateTime)

    can be a Time or DateTime object, will use the date of this argument if date not provided separately

  • to_timezone (String)

    the ActiveSupport::TimeZone timezone.tzinfo.name of the desired timezone

  • from_timezone (String)

    the ActiveSupport::TimeZone timezone.tzinfo.name of the timezone being converted

  • options (Symbol=>[Date])

    key-value option pairs, used to provide a separate Date object if a specific date is needed, otherwise the date of the time argument is used

Returns:

  • DateTime object with the time in the desired to_timezone, and a date of either the provided date or time



16
17
18
19
20
# File 'lib/rails_util/timezone_helper.rb', line 16

def convert_timezone(time, to_timezone, from_timezone, **options)
  date = options.fetch(:date, time)
  utc_time = convert_to_utc(time, from_timezone, date)
  convert_from_utc(utc_time, to_timezone, date)
end