Class: TimezoneParser::RailsZone

Inherits:
ZoneInfo
  • Object
show all
Defined in:
lib/timezone_parser/rails_zone.rb

Overview

Rails zone

Constant Summary

Constants inherited from ZoneInfo

ZoneInfo::TIMEZONE_TYPE_DAYLIGHT, ZoneInfo::TIMEZONE_TYPE_STANDARD

Instance Attribute Summary collapse

Attributes inherited from ZoneInfo

#FromTime, #ToTime

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ZoneInfo

#getMetazones, #getOffsets, #getTimezones, #getTypes, #reset, #setTime

Constructor Details

#initialize(name) ⇒ RailsZone

Rails zone instance

Parameters:

  • name (String)

    Rails zone name



35
36
37
38
39
40
# File 'lib/timezone_parser/rails_zone.rb', line 35

def initialize(name)
    @Name = name
    @Valid = nil
    setTime
    set(@@Locales.dup, @@Regions.dup)
end

Instance Attribute Details

#LocalesObject

Returns the value of attribute Locales.



29
30
31
# File 'lib/timezone_parser/rails_zone.rb', line 29

def Locales
  @Locales
end

#RegionsObject

Returns the value of attribute Regions.



30
31
32
# File 'lib/timezone_parser/rails_zone.rb', line 30

def Regions
  @Regions
end

#TypesObject

Returns the value of attribute Types.



31
32
33
# File 'lib/timezone_parser/rails_zone.rb', line 31

def Types
  @Types
end

Class Method Details

.getMetazones(name, locales = nil) ⇒ Array<String>

Get Metazone identifiers for given Rails zone name

Parameters:

  • name (String)

    Rails zone name

  • locales (Array<String>) (defaults to: nil)

    search zone name only for these locales

Returns:

  • (Array<String>)

    list of metazone identifiers

See Also:



121
122
123
# File 'lib/timezone_parser/rails_zone.rb', line 121

def self.getMetazones(name, locales = nil)
    self.new(name).set(locales).getMetazones
end

.getOffsets(name, toTime = nil, fromTime = nil, locales = nil, types = nil) ⇒ Array<Fixnum>

Get UTC offsets in seconds for given Rails zone name

Parameters:

  • name (String)

    Rails zone name

  • toTime (DateTime) (defaults to: nil)

    look for offsets which came into effect before this date, exclusive

  • fromTime (DateTime) (defaults to: nil)

    look for offsets which came into effect at this date, inclusive

  • locales (Array<String>) (defaults to: nil)

    search zone name only for these locales

Returns:

  • (Array<Fixnum>)

    list of timezone offsets in seconds

See Also:



102
103
104
# File 'lib/timezone_parser/rails_zone.rb', line 102

def self.getOffsets(name, toTime = nil, fromTime = nil, locales = nil, types = nil)
    self.new(name).setTime(toTime, fromTime).set(locales, nil, types).getOffsets
end

.getTimezones(name, locales = nil) ⇒ Array<String>

Get Timezone identifiers for given Rails zone name

Parameters:

  • name (String)

    Rails zone name

  • locales (Array<String>) (defaults to: nil)

    search zone name only for these locales

Returns:

  • (Array<String>)

    list of timezone identifiers

See Also:



111
112
113
# File 'lib/timezone_parser/rails_zone.rb', line 111

def self.getTimezones(name, locales = nil)
    self.new(name).set(locales).getTimezones
end

.getZone(name, locales = nil) ⇒ String

Rails zone identifier

Parameters:

  • name (String)

    Rails zone name

  • locales (Array<String>) (defaults to: nil)

    search zone name only for these locales

Returns:

  • (String)

    Timezone identifier



129
130
131
# File 'lib/timezone_parser/rails_zone.rb', line 129

def self.getZone(name, locales = nil)
    self.new(name).set(locales).getZone
end

.isValid?(name, locales = nil) ⇒ Boolean

Check if given Rails zone name is a valid timezone

Parameters:

  • name (String)

    Rails zone name

  • locales (Array<String>) (defaults to: nil)

    search zone name only for these locales

Returns:

  • (Boolean)

    whether Timezone is valid

See Also:



91
92
93
# File 'lib/timezone_parser/rails_zone.rb', line 91

def self.isValid?(name, locales = nil)
    self.new(name).set(locales).isValid?
end

.LocalesArray<String>

Locales which will be used for RailsZone methods if not specified there

Each locale is language identifier based on IETF BCP 47 and ISO 639 code

Returns:

  • (Array<String>)

    list containing locale identifiers

See Also:



16
17
18
# File 'lib/timezone_parser/rails_zone.rb', line 16

def self.Locales
    @@Locales
end

.RegionsArray<String>

Regions which will be used for WindowsZone methods if not specified there

Each region is either ISO 3166-1 alpha-2 code

Returns:

  • (Array<String>)

    list containing region identifiers

See Also:



25
26
27
# File 'lib/timezone_parser/rails_zone.rb', line 25

def self.Regions
    @@Regions
end

Instance Method Details

#getZoneString

Rails zone identifier

Returns:

  • (String)

    Rails zone identifier



79
80
81
82
83
84
# File 'lib/timezone_parser/rails_zone.rb', line 79

def getZone
    unless @Zone
        @Zone = self.getFilteredData(:Zone).first
    end
    @Zone
end

#isValid?Boolean

Check if Rails zone is valid

Returns:

  • (Boolean)

    whether Rails zone is valid



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/timezone_parser/rails_zone.rb', line 57

def isValid?
    if @Valid.nil?
        params = []
        joins = ''
        where = ''

        if not @Locales.empty?
            joins += ' LEFT JOIN `Locales` AS L ON RI.Locale = L.ID'
            where = 'L.Name COLLATE NOCASE IN (' + Array.new(@Locales.count, '?').join(',') + ') AND '
            params += @Locales
        end

        sql = "SELECT 1 FROM `RailsI18N` RI #{joins} WHERE #{where}RI.`NameLowercase` = ? LIMIT 1"
        params << @Name.downcase

        @Valid = Data::Storage.getStatement(sql).execute(*params).count > 0
    end
    @Valid
end

#set(locales = nil, regions = nil, types = nil) ⇒ WindowsZone

Set locales and regions

Parameters:

  • locales (Array<String>) (defaults to: nil)

    search only in these locales

  • regions (Array<String>) (defaults to: nil)

    filter for these regions

Returns:

See Also:



48
49
50
51
52
53
# File 'lib/timezone_parser/rails_zone.rb', line 48

def set(locales = nil, regions = nil, types = nil)
    @Locales = locales unless locales.nil?
    @Regions = regions unless regions.nil?
    @Types = types unless types.nil?
    self
end