Module: Spree::TimeZones

Defined in:
lib/spree/time_zones.rb

Overview

Time zones are stored and exchanged as IANA identifiers (Europe/Berlin), the names browsers and every non-Ruby client understand. Rails' display names ("Eastern Time (US & Canada)") are converted to them on the way in.

Constant Summary collapse

SUGGESTED =

The zones clients are offered: the IANA zones Rails gives a display name to, with plain UTC standing in for Etc/UTC. Any other valid IANA identifier is accepted too. The list comes with the Rails version rather than the machine's time zone database, so generated types match wherever the same Rails version runs.

(ActiveSupport::TimeZone::MAPPING.values - ['Etc/UTC'] + ['UTC']).uniq.sort.freeze

Class Method Summary collapse

Class Method Details

.normalize(value) ⇒ String?

Returns the IANA identifier, or the value unchanged when it names no zone the installed time zone database knows.

Parameters:

  • value (String, nil) —

    an IANA identifier or a Rails time zone name

Returns:

  • (String, nil) —

    the IANA identifier, or the value unchanged when it names no zone the installed time zone database knows



27
28
29
30
31
32
# File 'lib/spree/time_zones.rb', line 27

def self.normalize(value)
  return value if value.blank? || valid?(value)

  identifier = ActiveSupport::TimeZone::MAPPING[value.to_s]
  valid?(identifier) ? identifier : value
end

.valid?(identifier) ⇒ Boolean

Returns whether the value is an IANA time zone identifier.

Parameters:

  • identifier (String, nil)

Returns:

  • (Boolean) —

    whether the value is an IANA time zone identifier



15
16
17
18
19
20
21
22
# File 'lib/spree/time_zones.rb', line 15

def self.valid?(identifier)
  return false if identifier.blank?

  TZInfo::Timezone.get(identifier.to_s)
  true
rescue TZInfo::InvalidTimezoneIdentifier
  false
end