Class: Fluent::Timezone

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/timezone.rb

Constant Summary collapse

NUMERIC_PATTERN =

[+-]HH:MM, [+-]HHMM, [+-]HH

%r{\A[+-]\d\d(:?\d\d)?\z}
NAME_PATTERN =

Region/Zone, Region/Zone/Zone

%r{\A[^/]+/[^/]+(/[^/]+)?\z}

Class Method Summary collapse

Class Method Details

.formatter(timezone = nil, format = nil) ⇒ Object

Create a formatter for a timezone and optionally a format.

An Proc object is returned. If the given timezone is invalid, nil is returned.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fluent/timezone.rb', line 102

def self.formatter(timezone = nil, format = nil)
  if timezone.nil?
    return nil
  end

  # [+-]HH:MM, [+-]HHMM, [+-]HH
  if NUMERIC_PATTERN === timezone
    offset = Time.zone_offset(timezone)

    case
    when format.is_a?(String)
      return Proc.new {|time|
        time.to_time.localtime(offset).strftime(format)
      }
    when format.is_a?(Strftime)
      return Proc.new {|time|
        format.exec(time.to_time.localtime(offset))
      }
    else
      return Proc.new {|time|
        time.to_time.localtime(offset).iso8601
      }
    end
  end

  # Region/Zone, Region/Zone/Zone
  if NAME_PATTERN === timezone
    begin
      tz = TZInfo::Timezone.get(timezone)
    rescue
      return nil
    end

    case
    when format.is_a?(String)
      return Proc.new {|time|
        time = time.to_time
        time.localtime(tz.period_for_utc(time).utc_total_offset).strftime(format)
      }
    when format.is_a?(Strftime)
      return Proc.new {|time|
        time = time.to_time
        format.exec(time.localtime(tz.period_for_utc(time).utc_total_offset))
      }
    else
      return Proc.new {|time|
        time = time.to_time
        time.localtime(tz.period_for_utc(time).utc_total_offset).iso8601
      }
    end
  end

  return nil
end

.utc_offset(timezone) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/timezone.rb', line 157

def self.utc_offset(timezone)
  return 0 if timezone.nil?

  case timezone
  when NUMERIC_PATTERN
    Time.zone_offset(timezone)
  when NAME_PATTERN
    tz = TZInfo::Timezone.get(timezone)
    ->(time) {
      tz.period_for_utc(time.to_time).utc_total_offset
    }
  end
end

.validate(timezone) ⇒ Object

Validate the format of the specified timezone.

Valid formats are as follows. Note that timezone abbreviations such as PST and JST are not supported intentionally.

1. [+-]HH:MM         (e.g. "+09:00")
2. [+-]HHMM          (e.g. "+0900")
3. [+-]HH            (e.g. "+09")
4. Region/Zone       (e.g. "Asia/Tokyo")
5. Region/Zone/Zone  (e.g. "America/Argentina/Buenos_Aires")

In the 4th and 5th cases, it is checked whether the specified timezone exists in the timezone database.

When the given timezone is valid, true is returned. Otherwise, false is returned. When nil is given, false is returned.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/timezone.rb', line 54

def self.validate(timezone)
  # If the specified timezone is nil.
  if timezone.nil?
    # Invalid.
    return false
  end

  # [+-]HH:MM, [+-]HHMM, [+-]HH
  if NUMERIC_PATTERN === timezone
    # Valid. It can be parsed by Time.zone_offset method.
    return true
  end

  # Region/Zone, Region/Zone/Zone
  if NAME_PATTERN === timezone
    begin
      # Get a Timezone instance for the specified timezone.
      TZInfo::Timezone.get(timezone)
    rescue
      # Invalid. The string does not exist in the timezone database.
      return false
    else
      # Valid. The string was found in the timezone database.
      return true
    end
  else
    # Invalid. Timezone abbreviations are not supported.
    return false
  end
end

.validate!(timezone) ⇒ Object

Validate the format of the specified timezone.

The implementation of this method calls validate(timezone) method to check whether the given timezone is valid. When invalid, this method raises a ConfigError.



90
91
92
93
94
# File 'lib/fluent/timezone.rb', line 90

def self.validate!(timezone)
  unless validate(timezone)
    raise ConfigError, "Unsupported timezone '#{timezone}'"
  end
end