Class: TZInfo::TimezonePeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/timezone_period.rb

Overview

A period of time in a timezone where the same offset from UTC applies.

All the methods that take times accept instances of Time, DateTime or integer timestamps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier) ⇒ TimezonePeriod

Initializes a new TimezonePeriod.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tzinfo/timezone_period.rb', line 51

def initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier)
  @utc_start = utc_start.nil? ? nil : TimeOrDateTime.wrap(utc_start)
  @utc_end = utc_end.nil? ? nil : TimeOrDateTime.wrap(utc_end)
  @utc_offset = utc_offset
  @std_offset = std_offset
  @zone_identifier = zone_identifier
  @utc_total_offset = utc_offset + std_offset
  @utc_total_offset_rational = nil
          
  # Use add_with_convert to use DateTime based TimeOrDateTimes if we run
  # off the range of Time.
  @local_start = @utc_start.nil? ? nil : @utc_start.add_with_convert(@utc_total_offset)
  @local_end = @utc_end.nil? ? nil : @utc_end.add_with_convert(@utc_total_offset)             
end

Instance Attribute Details

#std_offsetObject (readonly)

Offset from the local time where daylight savings is in effect (seconds). E.g.: utc_offset could be -5 hours. Normally, std_offset would be 0. During daylight savings, std_offset would become +1 hours.



39
40
41
# File 'lib/tzinfo/timezone_period.rb', line 39

def std_offset
  @std_offset
end

#utc_offsetObject (readonly)

Base offset of the timezone from UTC (seconds).



34
35
36
# File 'lib/tzinfo/timezone_period.rb', line 34

def utc_offset
  @utc_offset
end

#utc_total_offsetObject (readonly)

Total offset from UTC (seconds). Equal to utc_offset + std_offset.



48
49
50
# File 'lib/tzinfo/timezone_period.rb', line 48

def utc_total_offset
  @utc_total_offset
end

#zone_identifierObject (readonly) Also known as: abbreviation

The identifier of this period, e.g. “GMT” (Greenwich Mean Time) or “BST” (British Summer Time) for “Europe/London”. The returned identifier is a symbol.



44
45
46
# File 'lib/tzinfo/timezone_period.rb', line 44

def zone_identifier
  @zone_identifier
end

Instance Method Details

#dst?Boolean

true if daylight savings is in effect for this period; otherwise false.

Returns:

  • (Boolean)


95
96
97
# File 'lib/tzinfo/timezone_period.rb', line 95

def dst?
  std_offset != 0
end

#local_after_start?(local) ⇒ Boolean

true if the given local DateTime is after the start of the period (inclusive); otherwise false.

Returns:

  • (Boolean)


120
121
122
# File 'lib/tzinfo/timezone_period.rb', line 120

def local_after_start?(local)                
  @local_start.nil? || @local_start <= local
end

#local_before_end?(local) ⇒ Boolean

true if the given local DateTime is before the end of the period (exclusive); otherwise false.

Returns:

  • (Boolean)


125
126
127
# File 'lib/tzinfo/timezone_period.rb', line 125

def local_before_end?(local)               
  @local_end.nil? || @local_end > local
end

#local_endObject

End time of the period (local time). May be nil if unbounded.



90
91
92
# File 'lib/tzinfo/timezone_period.rb', line 90

def local_end
  @local_end.nil? ? nil : @local_end.to_datetime
end

#local_startObject

Start time of the period (local time). May be nil if unbounded.



85
86
87
# File 'lib/tzinfo/timezone_period.rb', line 85

def local_start
  @local_start.nil? ? nil : @local_start.to_datetime
end

#to_local(utc) ⇒ Object

Converts a UTC DateTime to local time based on the offset of this period.



130
131
132
133
134
# File 'lib/tzinfo/timezone_period.rb', line 130

def to_local(utc)
  TimeOrDateTime.wrap(utc) {|utc|
    utc + utc_total_offset
  }
end

#to_utc(local) ⇒ Object

Converts a local DateTime to UTC based on the offset of this period.



137
138
139
140
141
# File 'lib/tzinfo/timezone_period.rb', line 137

def to_utc(local)
  TimeOrDateTime.wrap(local) {|local|
    local - utc_total_offset
  }
end

#utc_after_start?(utc) ⇒ Boolean

true if the given utc DateTime is after the start of the period (inclusive); otherwise false.

Returns:

  • (Boolean)


105
106
107
# File 'lib/tzinfo/timezone_period.rb', line 105

def utc_after_start?(utc)                
  @utc_start.nil? || @utc_start <= utc
end

#utc_before_end?(utc) ⇒ Boolean

true if the given utc DateTime is before the end of the period (exclusive); otherwise false.

Returns:

  • (Boolean)


110
111
112
# File 'lib/tzinfo/timezone_period.rb', line 110

def utc_before_end?(utc)           
  @utc_end.nil? || @utc_end > utc
end

#utc_endObject

End time of the period (UTC). May be nil if unbounded.



80
81
82
# File 'lib/tzinfo/timezone_period.rb', line 80

def utc_end
  @utc_end.nil? ? nil : @utc_end.to_datetime
end

#utc_startObject

Start time of the period (UTC). May be nil if unbounded.



75
76
77
# File 'lib/tzinfo/timezone_period.rb', line 75

def utc_start
  @utc_start.nil? ? nil : @utc_start.to_datetime
end

#utc_total_offset_rationalObject

Total offset from UTC (days). Result is a Rational.



67
68
69
70
71
72
# File 'lib/tzinfo/timezone_period.rb', line 67

def utc_total_offset_rational
  if @utc_total_offset_rational.nil?
    @utc_total_offset_rational = OffsetRationals.rational_for_offset(@utc_total_offset) 
  end
  @utc_total_offset_rational
end

#valid_for_local?(local) ⇒ Boolean

true if this period is valid for the given local DateTime; otherwise false.

Returns:

  • (Boolean)


115
116
117
# File 'lib/tzinfo/timezone_period.rb', line 115

def valid_for_local?(local)      
  local_after_start?(local) && local_before_end?(local) 
end

#valid_for_utc?(utc) ⇒ Boolean

true if this period is valid for the given utc DateTime; otherwise false.

Returns:

  • (Boolean)


100
101
102
# File 'lib/tzinfo/timezone_period.rb', line 100

def valid_for_utc?(utc)
  utc_after_start?(utc) && utc_before_end?(utc) 
end