Module: RiCal::PropertyValue::DateTime::TimezoneSupport

Included in:
RiCal::PropertyValue::DateTime
Defined in:
lib/ri_cal/property_value/date_time/timezone_support.rb

Overview

Time zone related methods for DateTime

Instance Method Summary collapse

Instance Method Details

#find_timezoneObject

:nodoc:



21
22
23
24
25
26
27
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 21

def find_timezone #:nodoc:
  if @tzid == :floating
    FloatingTimezone
  else
    timezone_finder.find_timezone(@tzid)
  end
end

#floating?Boolean

Predicate indicating whether or not the instance represents a floating time

Returns:

  • (Boolean)


71
72
73
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 71

def floating?
  tzid.nil?
end

#has_local_timezone?Boolean

Determine if the receiver has a local time zone, i.e. it is not a floating time or a UTC time

Returns:

  • (Boolean)


34
35
36
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 34

def has_local_timezone?
  tzid && tzid.upcase != "UTC"
end

#has_valid_tzinfo_tzid?Boolean

:nodoc:

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 75

def has_valid_tzinfo_tzid? #:nodoc:
  if tzid && tzid != :floating
    TZInfo::Timezone.get(tzid) rescue false
  else
    false
  end
end

#in_time_zone(new_zone) ⇒ Object

Returns the simultaneous time in the specified zone.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 84

def in_time_zone(new_zone)
  new_zone = timezone_finder.find_timezone(new_zone)
  return self if tzid == new_zone.identifier
  if has_local_timezone?
    new_zone.utc_to_local(utc)
  elsif utc?
    new_zone.utc_to_local(self)
  else # Floating time
    DateTime.new(timezone_finder, :value => @date_time_value, :tzid => new_zone.identifier)
  end
end

#rational_tz_offsetObject

:nodoc:



57
58
59
60
61
62
63
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 57

def rational_tz_offset #:nodoc:
  if has_local_timezone?
    @rational_tz_offset ||= timezone.rational_utc_offset(@date_time_value.to_datetime)
  else
    @rational_tz_offset ||= RiCal.RationalOffset[0]
  end
end

#reset_cached_valuesObject

:nodoc:



17
18
19
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 17

def reset_cached_values #:nodoc:
  @timezone = @utc = @rational_tz_offset = nil
end

#timezoneObject

:nodoc:



29
30
31
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 29

def timezone #:nodoc:
  @timezone ||= find_timezone
end

#tzidObject

Return the timezone id of the receiver, or nil if it is a floating time



7
8
9
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 7

def tzid
  @tzid  == :floating ? nil : @tzid
end

#tzid=(timezone_id) ⇒ Object

:nodoc:



11
12
13
14
15
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 11

def tzid=(timezone_id) #:nodoc:
  timezone_id = default_tzid if timezone_id == :default
  @tzid = timezone_id
  reset_cached_values
end

#utcObject

Returns a instance that represents the time in UTC.



49
50
51
52
53
54
55
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 49

def utc
  if has_local_timezone?
    @utc ||= timezone.local_to_utc(self)
  else  # Already local or a floating time
    self
  end
end

#utc?Boolean

Predicate indicating whether or not the instance represents a ZULU time

Returns:

  • (Boolean)


66
67
68
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 66

def utc?
  tzid == "UTC"
end

#with_floating_timezoneObject

Return the receiver if it has a floating time zone already, otherwise return a DATETIME property with the same time as the receiver but with a floating time zone



40
41
42
43
44
45
46
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 40

def with_floating_timezone
  if @tzid == nil
    self
  else
    @date_time_value.with_floating_timezone.to_ri_cal_date_time_value
  end
end