Module: TZTime::TimeZoneElement
- Defined in:
- lib/tztime/time_zone_element.rb
Overview
This module is intented to be included into classes that need to store and access time zones (TZInfo time zone definition) and local time builders (TZTime::LocalTime::Builder). It adds a method for assigning a time zone to the instance. A LocalTime::Builder is created when a time zone is set. The builder is used to generate LocalTime instances in the specified time zone.
Usage
Add to any class:
class SomeClass
include TZTime::TimeZoneElement
end
Create an instance of the class and assign a time zone:
sc = SomeClass.new
sc.time_zone = 'America/New_York'
or use Rails style time zone names:
sc.time_zone = 'Eastern Time (US & Canada)
or use a TZInfo::TimeZone instance:
time_zone = TZInfo::Timezone.get('America/New_York')
sc.time_zone = time_zone
Now the instance can be used to generate times:
puts sc.time_zone_builder.now # => 2008-03-31 21:27:19 EDT
puts sc.time_zone_builder.today # => 2008-03-31 00:00:00 EDT
puts sc.time_zone_builder.now.utc # => Tue Apr 01 01:27:19 UTC 2008
Instance Method Summary collapse
-
#local_time_builder ⇒ Object
retrieves the TZTime::LocalTime::Builder instance or
nil
. -
#local_time_builder=(value) ⇒ Object
Assigns a new TZTime::LocalTime:Builder instance.
-
#reset_local_time_builder! ⇒ Object
Resets the
local_time_builder
to the default which is UTC. -
#select_local_time_builder(options = {}) ⇒ Object
Selects a TZTime::LocalTime::Builder instance giving precedence to the
options
. -
#time_zone ⇒ Object
Retrieves the current time zone definition.
-
#time_zone=(value) ⇒ Object
(also: #time_zone_name=)
Sets the time zone for the parser.
-
#time_zone_name ⇒ Object
Retrieves the full name of the time zone.
Instance Method Details
#local_time_builder ⇒ Object
retrieves the TZTime::LocalTime::Builder instance or nil
.
30 31 32 33 |
# File 'lib/tztime/time_zone_element.rb', line 30 def local_time_builder return @local_time_builder if defined?(@local_time_builder) reset_local_time_builder! end |
#local_time_builder=(value) ⇒ Object
Assigns a new TZTime::LocalTime:Builder instance. This will also change the time_zone
and time_zone_name
values.
37 38 39 |
# File 'lib/tztime/time_zone_element.rb', line 37 def local_time_builder=(value) @local_time_builder = value end |
#reset_local_time_builder! ⇒ Object
Resets the local_time_builder
to the default which is UTC.
67 68 69 |
# File 'lib/tztime/time_zone_element.rb', line 67 def reset_local_time_builder! @local_time_builder = TZTime::LocalTime::Builder.utc end |
#select_local_time_builder(options = {}) ⇒ Object
Selects a TZTime::LocalTime::Builder instance giving precedence to the options
. If options
contains a :local_time_builder
value, that will be returned. If options
contains a :time_zone
value, then a builder will be instantiated for that time zone. Otherwise, the currentnly set local_time_builder
will be returned.
76 77 78 79 80 |
# File 'lib/tztime/time_zone_element.rb', line 76 def select_local_time_builder(={}) [:local_time_builder] || ([:time_zone] && TZTime::LocalTime::Builder.new([:time_zone])) || local_time_builder end |
#time_zone ⇒ Object
Retrieves the current time zone definition.
47 48 49 |
# File 'lib/tztime/time_zone_element.rb', line 47 def time_zone local_time_builder.time_zone end |
#time_zone=(value) ⇒ Object Also known as: time_zone_name=
Sets the time zone for the parser. This is used to calculate offset values for the dates and times parsed from input. The value should be either a TZInfo::Timezone
instance or a string that refers to a time zone when passed to TZInfo::Timezone#get
. If the value is nil
, then parsing will be assumed in UTC.
56 57 58 59 60 61 62 |
# File 'lib/tztime/time_zone_element.rb', line 56 def time_zone=(value) if value @local_time_builder = TZTime::LocalTime::Builder.new(value) else reset_local_time_builder! end end |
#time_zone_name ⇒ Object
Retrieves the full name of the time zone.
42 43 44 |
# File 'lib/tztime/time_zone_element.rb', line 42 def time_zone_name local_time_builder.time_zone_name end |