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.



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

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.



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

def utc_total_offset
  @utc_total_offset
end

#zone_identifierObject (readonly)

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)


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

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)


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

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)


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

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.



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

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.



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

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.



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

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.



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

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)


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

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)


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

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.



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

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.



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

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.



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

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)


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

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)


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

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