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

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

Overview

  • ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license

Time zone related methods for DateTime

Instance Method Summary collapse

Instance Method Details

#find_timezoneObject

:nodoc:



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

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)


73
74
75
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 73

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)


36
37
38
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 36

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

#has_valid_tzinfo_tzid?Boolean

:nodoc:

Returns:

  • (Boolean)


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

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.



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

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:



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

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:



19
20
21
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 19

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

#timezoneObject

:nodoc:



31
32
33
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 31

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

#tzidObject

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



9
10
11
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 9

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

#tzid=(timezone_id) ⇒ Object

:nodoc:



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

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.



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

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)


68
69
70
# File 'lib/ri_cal/property_value/date_time/timezone_support.rb', line 68

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



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

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