class TZ::TimeWithZone
Constants
- MAPPING
- ONE_HOUR_IN_MINS
- ONE_HOUR_IN_SECS
- TIME_FORMAT
Attributes
time_zone[R]
Public Class Methods
new(date_or_time, time_zone)
click to toggle source
Public: Class constructor.
date_or_time - The Date, Time or DateTime object to be formatted. time_zone
- The ActiveSupport String specifying the target time zone.
Examples
TZ::TimeWithZone.new(Time.now.utc, "Eastern Time (US & Canada)") # => an instance of the TZ::TimeWithZone class
Returns a fresh TZ::TimeWithZone
object.
# File lib/tz.rb, line 173 def initialize(date_or_time, time_zone) @utc_object = to_utc(date_or_time) @time_zone = begin tz = MAPPING[time_zone] TZInfo::Timezone.get(tz) end end
Public Instance Methods
in_time_zone()
click to toggle source
Public: formats the input date/time/datetime object.
Examples
TZ::TimeWithZone.new(Time.now.utc, "Eastern Time (US & Canada)") # => "Sat, 07 Jun 2014 17:23:53 EDT -04:00"
Returns the formatted String version of the input date/time/datetime object.
# File lib/tz.rb, line 189 def in_time_zone "#{formatted_local_time} #{time_zone_code} #{formatted_time}" end
Private Instance Methods
formatted_local_time()
click to toggle source
# File lib/tz.rb, line 195 def formatted_local_time local_time.rfc822.split(" ")[0..-2].join(" ") end
formatted_time()
click to toggle source
# File lib/tz.rb, line 203 def formatted_time offset = period.offset.utc_total_offset hours, minutes = offset.divmod(ONE_HOUR_IN_SECS) minutes /= ONE_HOUR_IN_MINS sign = hours < 0 ? "-" : "+" padded_hours = TIME_FORMAT % hours.abs padded_minutes = TIME_FORMAT % minutes "#{sign}#{padded_hours}:#{padded_minutes}" end
local_time()
click to toggle source
# File lib/tz.rb, line 217 def local_time @time_zone.utc_to_local(@utc_object) end
period()
click to toggle source
# File lib/tz.rb, line 213 def period @period ||= @time_zone.period_for_utc(@utc_object) end
time_zone_code()
click to toggle source
# File lib/tz.rb, line 199 def time_zone_code period.zone_identifier end
to_utc(date_or_time)
click to toggle source
# File lib/tz.rb, line 221 def to_utc(date_or_time) return date_or_time.to_datetime.to_time.utc if date_or_time.is_a?(Date) return date_or_time.utc if date_or_time.respond_to?(:utc?) && !date_or_time.utc? && date_or_time.respond_to?(:utc) date_or_time end