Class: Parse::TimeZone

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/model/time_zone.rb

Overview

This class a wrapper around ActiveSupport::TimeZone when using Parse columns that store IANA time zone identifiers (ex. Installation collection). Parse does not have a native time zone data type, but this class is provided to manage and perform timezone-like operation on those properties which you have marked as type :timezone.

When declaring a property of type :timezone, you may also define a default just like any other property. In addition, the framework will automatically add a validation to make sure that your property is either nil or one of the valid IANA time zone identifiers.

Each instance of TimeZone has a #zone attribute that provides access to the underlying ActiveSupport::TimeZone instance, which you can use to perform time zone operations.

Examples:

class Event < Parse::Object
  # an event occurs in a time zone.
  property :time_zone, :timezone, default: 'America/Los_Angeles'
end

event = Event.new
event.time_zone.name # => 'America/Los_Angeles'
event.time_zone.valid? # => true

event.time_zone.zone # => ActiveSupport::TimeZone
event.time_zone.formatted_offset # => "-08:00"

event.time_zone = 'Europe/Paris'
event.time_zone.formatted_offset # => +01:00"

event.time_zone = 'Galaxy/Andromeda'
event.time_zone.valid? # => false

Version:

  • 1.7.1

Constant Summary collapse

MAPPING =

The mapping of TimeZones

ActiveSupport::TimeZone::MAPPING

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(iana) ⇒ Parse::TimeZone #new(timezone) ⇒ Parse::TimeZone

Creates a new instance given the IANA identifier (ex. America/Los_Angeles)

Overloads:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/parse/model/time_zone.rb', line 63

def initialize(iana)
  if iana.is_a?(String)
    @name = iana
    @zone = nil
  elsif iana.is_a?(::Parse::TimeZone)
    @zone = iana.zone
    @name = nil
  elsif iana.is_a?(::ActiveSupport::TimeZone)
    @zone = iana
    @name = nil
  end
end

Instance Attribute Details

#nameString

Returns the IANA identifier for this time zone.

Returns:

  • (String)

    the IANA identifier for this time zone.

Raises:

  • ArgumentError if value is not a string type.



79
80
81
# File 'lib/parse/model/time_zone.rb', line 79

def name
  @zone.present? ? zone.name : @name
end

#zoneActiveSupport::TimeZone

Returns an instance of ActiveSupport::TimeZone based on the IANA identifier. The setter may allow usign an IANA string identifier, a Parse::TimeZone or an ActiveSupport::TimeZone object.

Returns:

  • (ActiveSupport::TimeZone)

Raises:

  • ArgumentError

See Also:



100
101
102
103
104
105
106
107
108
# File 'lib/parse/model/time_zone.rb', line 100

def zone
  # lazy load the TimeZone object only when the user requests it, otherwise
  # just keep the name of the string around. Makes encoding/decoding faster.
  if @zone.nil? && @name.present?
    @zone = ::ActiveSupport::TimeZone.new(@name)
    @name = nil # clear out the cache
  end
  @zone
end

Instance Method Details

#as_json(*args) ⇒ String

Returns the IANA identifier for this timezone or nil.

Returns:

  • (String)

    the IANA identifier for this timezone or nil.



126
127
128
# File 'lib/parse/model/time_zone.rb', line 126

def as_json(*args)
  name
end

#to_sString

Returns the IANA identifier for this timezone or nil.

Returns:

  • (String)

    the IANA identifier for this timezone or nil.



131
132
133
# File 'lib/parse/model/time_zone.rb', line 131

def to_s
  name
end

#valid?Bool

Returns true or false whether the time zone exists in the ActiveSupport::TimeZone mapping.

Returns:

  • (Bool)

    true if it contains a valid time zone



137
138
139
# File 'lib/parse/model/time_zone.rb', line 137

def valid?
  ActiveSupport::TimeZone[to_s].present?
end