ebeigarts-ri_cal 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/History.txt +136 -0
  2. data/Manifest.txt +140 -0
  3. data/README.txt +410 -0
  4. data/Rakefile +53 -0
  5. data/bin/ri_cal +8 -0
  6. data/component_attributes/alarm.yml +10 -0
  7. data/component_attributes/calendar.yml +4 -0
  8. data/component_attributes/component_property_defs.yml +180 -0
  9. data/component_attributes/event.yml +45 -0
  10. data/component_attributes/freebusy.yml +16 -0
  11. data/component_attributes/journal.yml +35 -0
  12. data/component_attributes/timezone.yml +3 -0
  13. data/component_attributes/timezone_period.yml +11 -0
  14. data/component_attributes/todo.yml +46 -0
  15. data/copyrights.txt +1 -0
  16. data/docs/draft-ietf-calsify-2446bis-08.txt +7280 -0
  17. data/docs/draft-ietf-calsify-rfc2445bis-09.txt +10416 -0
  18. data/docs/incrementers.txt +7 -0
  19. data/docs/rfc2445.pdf +0 -0
  20. data/lib/ri_cal.rb +175 -0
  21. data/lib/ri_cal/component.rb +249 -0
  22. data/lib/ri_cal/component/alarm.rb +21 -0
  23. data/lib/ri_cal/component/calendar.rb +259 -0
  24. data/lib/ri_cal/component/event.rb +60 -0
  25. data/lib/ri_cal/component/freebusy.rb +18 -0
  26. data/lib/ri_cal/component/journal.rb +30 -0
  27. data/lib/ri_cal/component/non_standard.rb +35 -0
  28. data/lib/ri_cal/component/t_z_info_timezone.rb +139 -0
  29. data/lib/ri_cal/component/timezone.rb +197 -0
  30. data/lib/ri_cal/component/timezone/daylight_period.rb +25 -0
  31. data/lib/ri_cal/component/timezone/standard_period.rb +23 -0
  32. data/lib/ri_cal/component/timezone/timezone_period.rb +78 -0
  33. data/lib/ri_cal/component/todo.rb +44 -0
  34. data/lib/ri_cal/core_extensions.rb +6 -0
  35. data/lib/ri_cal/core_extensions/array.rb +7 -0
  36. data/lib/ri_cal/core_extensions/array/conversions.rb +15 -0
  37. data/lib/ri_cal/core_extensions/date.rb +13 -0
  38. data/lib/ri_cal/core_extensions/date/conversions.rb +69 -0
  39. data/lib/ri_cal/core_extensions/date_time.rb +15 -0
  40. data/lib/ri_cal/core_extensions/date_time/conversions.rb +55 -0
  41. data/lib/ri_cal/core_extensions/object.rb +8 -0
  42. data/lib/ri_cal/core_extensions/object/conversions.rb +20 -0
  43. data/lib/ri_cal/core_extensions/string.rb +8 -0
  44. data/lib/ri_cal/core_extensions/string/conversions.rb +53 -0
  45. data/lib/ri_cal/core_extensions/time.rb +13 -0
  46. data/lib/ri_cal/core_extensions/time/calculations.rb +153 -0
  47. data/lib/ri_cal/core_extensions/time/conversions.rb +42 -0
  48. data/lib/ri_cal/core_extensions/time/tzid_access.rb +50 -0
  49. data/lib/ri_cal/core_extensions/time/week_day_predicates.rb +55 -0
  50. data/lib/ri_cal/fast_date_time.rb +226 -0
  51. data/lib/ri_cal/floating_timezone.rb +32 -0
  52. data/lib/ri_cal/invalid_property_value.rb +8 -0
  53. data/lib/ri_cal/invalid_timezone_identifer.rb +20 -0
  54. data/lib/ri_cal/occurrence_enumerator.rb +265 -0
  55. data/lib/ri_cal/occurrence_period.rb +17 -0
  56. data/lib/ri_cal/parser.rb +145 -0
  57. data/lib/ri_cal/properties/alarm.rb +390 -0
  58. data/lib/ri_cal/properties/calendar.rb +164 -0
  59. data/lib/ri_cal/properties/event.rb +1526 -0
  60. data/lib/ri_cal/properties/freebusy.rb +594 -0
  61. data/lib/ri_cal/properties/journal.rb +1240 -0
  62. data/lib/ri_cal/properties/timezone.rb +151 -0
  63. data/lib/ri_cal/properties/timezone_period.rb +416 -0
  64. data/lib/ri_cal/properties/todo.rb +1562 -0
  65. data/lib/ri_cal/property_value.rb +149 -0
  66. data/lib/ri_cal/property_value/array.rb +27 -0
  67. data/lib/ri_cal/property_value/cal_address.rb +11 -0
  68. data/lib/ri_cal/property_value/date.rb +169 -0
  69. data/lib/ri_cal/property_value/date_time.rb +351 -0
  70. data/lib/ri_cal/property_value/date_time/additive_methods.rb +44 -0
  71. data/lib/ri_cal/property_value/date_time/time_machine.rb +159 -0
  72. data/lib/ri_cal/property_value/date_time/timezone_support.rb +100 -0
  73. data/lib/ri_cal/property_value/duration.rb +110 -0
  74. data/lib/ri_cal/property_value/geo.rb +11 -0
  75. data/lib/ri_cal/property_value/integer.rb +12 -0
  76. data/lib/ri_cal/property_value/occurrence_list.rb +144 -0
  77. data/lib/ri_cal/property_value/period.rb +86 -0
  78. data/lib/ri_cal/property_value/recurrence_rule.rb +145 -0
  79. data/lib/ri_cal/property_value/recurrence_rule/enumeration_support_methods.rb +97 -0
  80. data/lib/ri_cal/property_value/recurrence_rule/enumerator.rb +79 -0
  81. data/lib/ri_cal/property_value/recurrence_rule/initialization_methods.rb +148 -0
  82. data/lib/ri_cal/property_value/recurrence_rule/negative_setpos_enumerator.rb +53 -0
  83. data/lib/ri_cal/property_value/recurrence_rule/numbered_span.rb +31 -0
  84. data/lib/ri_cal/property_value/recurrence_rule/occurence_incrementer.rb +793 -0
  85. data/lib/ri_cal/property_value/recurrence_rule/recurring_day.rb +131 -0
  86. data/lib/ri_cal/property_value/recurrence_rule/recurring_month_day.rb +60 -0
  87. data/lib/ri_cal/property_value/recurrence_rule/recurring_numbered_week.rb +33 -0
  88. data/lib/ri_cal/property_value/recurrence_rule/recurring_year_day.rb +49 -0
  89. data/lib/ri_cal/property_value/recurrence_rule/validations.rb +125 -0
  90. data/lib/ri_cal/property_value/text.rb +40 -0
  91. data/lib/ri_cal/property_value/uri.rb +11 -0
  92. data/lib/ri_cal/property_value/utc_offset.rb +33 -0
  93. data/lib/ri_cal/required_timezones.rb +55 -0
  94. data/performance/paris_eastern/subject.rb +90 -0
  95. data/performance/penultimate_weekday/subject.rb +15 -0
  96. data/performance/psm_big_enum/ical.ics +3171 -0
  97. data/performance/psm_big_enum/subject.rb +16 -0
  98. data/performance/utah_cycling/subject.rb +55 -0
  99. data/ri_cal.gemspec +48 -0
  100. data/sample_ical_files/from_ical_dot_app/test1.ics +38 -0
  101. data/script/benchmark_subject +23 -0
  102. data/script/console +10 -0
  103. data/script/destroy +14 -0
  104. data/script/generate +14 -0
  105. data/script/profile_subject +27 -0
  106. data/script/txt2html +71 -0
  107. data/spec/ri_cal/bugreports_spec.rb +230 -0
  108. data/spec/ri_cal/component/alarm_spec.rb +12 -0
  109. data/spec/ri_cal/component/calendar_spec.rb +88 -0
  110. data/spec/ri_cal/component/event_spec.rb +718 -0
  111. data/spec/ri_cal/component/freebusy_spec.rb +12 -0
  112. data/spec/ri_cal/component/journal_spec.rb +37 -0
  113. data/spec/ri_cal/component/t_z_info_timezone_spec.rb +59 -0
  114. data/spec/ri_cal/component/timezone_spec.rb +236 -0
  115. data/spec/ri_cal/component/todo_spec.rb +112 -0
  116. data/spec/ri_cal/component_spec.rb +224 -0
  117. data/spec/ri_cal/core_extensions/string/conversions_spec.rb +78 -0
  118. data/spec/ri_cal/core_extensions/time/calculations_spec.rb +188 -0
  119. data/spec/ri_cal/core_extensions/time/week_day_predicates_spec.rb +45 -0
  120. data/spec/ri_cal/fast_date_time_spec.rb +67 -0
  121. data/spec/ri_cal/occurrence_enumerator_spec.rb +611 -0
  122. data/spec/ri_cal/parser_spec.rb +337 -0
  123. data/spec/ri_cal/property_value/date_spec.rb +53 -0
  124. data/spec/ri_cal/property_value/date_time_spec.rb +383 -0
  125. data/spec/ri_cal/property_value/duration_spec.rb +126 -0
  126. data/spec/ri_cal/property_value/occurrence_list_spec.rb +72 -0
  127. data/spec/ri_cal/property_value/period_spec.rb +63 -0
  128. data/spec/ri_cal/property_value/recurrence_rule/recurring_year_day_spec.rb +21 -0
  129. data/spec/ri_cal/property_value/recurrence_rule_spec.rb +1814 -0
  130. data/spec/ri_cal/property_value/text_spec.rb +25 -0
  131. data/spec/ri_cal/property_value/utc_offset_spec.rb +48 -0
  132. data/spec/ri_cal/property_value_spec.rb +125 -0
  133. data/spec/ri_cal/required_timezones_spec.rb +67 -0
  134. data/spec/ri_cal_spec.rb +53 -0
  135. data/spec/spec.opts +4 -0
  136. data/spec/spec_helper.rb +46 -0
  137. data/tasks/gem_loader/load_active_support.rb +3 -0
  138. data/tasks/gem_loader/load_tzinfo_gem.rb +2 -0
  139. data/tasks/ri_cal.rake +412 -0
  140. data/tasks/spec.rake +102 -0
  141. metadata +230 -0
@@ -0,0 +1,149 @@
1
+ module RiCal
2
+ #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
3
+ #
4
+ # PropertyValue provides common implementation of various RFC 2445 property value types
5
+ class PropertyValue
6
+
7
+ attr_writer :params, :value #:nodoc:
8
+ attr_reader :timezone_finder #:nodoc:
9
+ def initialize(timezone_finder, options={}) # :nodoc:
10
+ @timezone_finder = timezone_finder
11
+ validate_value(options)
12
+ ({:params => {}}).merge(options).each do |attribute, val|
13
+ unless attribute == :name
14
+ setter = :"#{attribute.to_s.downcase}="
15
+ send(setter, val)
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.if_valid_string(timezone_finder, string) #:nodoc:
21
+ if valid_string?(string)
22
+ new(timezone_finder, :value => string)
23
+ else
24
+ nil
25
+ end
26
+ end
27
+
28
+ def validate_value(options) #:nodoc:
29
+ val = options[:value]
30
+ raise "Invalid property value #{val.inspect}" if val.kind_of?(String) && /^;/.match(val)
31
+ end
32
+
33
+ # return a hash containing the parameters and values, if any
34
+ def params
35
+ @params ||= {}
36
+ end
37
+
38
+ def to_options_hash #:nodoc:
39
+ options_hash = {:value => value}
40
+ options_hash[:params] = params unless params.empty?
41
+ end
42
+
43
+ def self.date_or_date_time(timezone_finder, separated_line) # :nodoc:
44
+ match = separated_line[:value].match(/(\d\d\d\d)(\d\d)(\d\d)((T?)((\d\d)(\d\d)(\d\d))(Z?))?/)
45
+ raise Exception.new("Invalid date") unless match
46
+ if match[5] == "T" # date-time
47
+ time = Time.utc(match[1].to_i, match[2].to_i, match[3].to_i, match[7].to_i, match[8].to_i, match[9].to_i)
48
+ parms = (separated_line[:params] ||{}).dup
49
+ if match[10] == "Z"
50
+ raise Exception.new("Invalid time, cannot combine Zulu with timezone reference") if parms[:tzid]
51
+ parms['TZID'] = "UTC"
52
+ end
53
+ PropertyValue::DateTime.new(timezone_finder, separated_line.merge(:params => parms))
54
+ else
55
+ PropertyValue::Date.new(timezone_finder, separated_line)
56
+ end
57
+ end
58
+
59
+ def self.date_or_date_time_or_period(timezone_finder, separated_line) #:nodoc:
60
+ if separated_line[:value].include?("/")
61
+ PropertyValue::Period.new(timezone_finder, separated_line)
62
+ else
63
+ date_or_date_time(timezone_finder, separated_line)
64
+ end
65
+ end
66
+
67
+ # def self.from_string(string) # :nodoc:
68
+ # new(nil, :value => string)
69
+ # end
70
+
71
+ def self.convert(timezone_finder, value) #:nodoc:
72
+ new(timezone_finder, :value => value)
73
+ end
74
+
75
+ # Determine if another object is equivalent to the receiver.
76
+ def ==(o)
77
+ if o.class == self.class
78
+ equality_value == o.equality_value
79
+ else
80
+ super
81
+ end
82
+ end
83
+
84
+ # Return the string value
85
+ def value
86
+ @value
87
+ end
88
+
89
+ def equality_value #:nodoc:
90
+ value
91
+ end
92
+
93
+ def visible_params # :nodoc:
94
+ params
95
+ end
96
+
97
+ def parms_string #:nodoc:
98
+ if (vp = visible_params) && !vp.empty?
99
+ # We only sort for testability reasons
100
+ vp.keys.sort.map {|key| ";#{key}=#{vp[key]}"}.join
101
+ else
102
+ ""
103
+ end
104
+ end
105
+
106
+ # Return a string representing the receiver in RFC 2445 format
107
+ def to_s #:nodoc:
108
+ "#{parms_string}:#{value}"
109
+ end
110
+
111
+ # return the ruby value
112
+ def ruby_value
113
+ self.value
114
+ end
115
+
116
+ def to_ri_cal_property_value #:nodoc:
117
+ self
118
+ end
119
+
120
+ def find_timezone(timezone_identifier) #:nodoc:
121
+ if timezone_finder
122
+ timezone_finder.find_timezone(timezone_identifier)
123
+ else
124
+ raise "Unable to find timezone with tzid #{timezone_identifier}"
125
+ end
126
+ end
127
+
128
+ def default_tzid #:nodoc:
129
+ if timezone_finder
130
+ timezone_finder.default_tzid
131
+ else
132
+ PropertyValue::DateTime.default_tzid
133
+ end
134
+ end
135
+
136
+ def tz_info_source? #:nodoc:
137
+ if timezone_finder
138
+ timezone_finder.tz_info_source?
139
+ else
140
+ true
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ Dir[File.dirname(__FILE__) + "/property_value/*.rb"].sort.each do |path|
147
+ filename = File.basename(path)
148
+ require path
149
+ end
@@ -0,0 +1,27 @@
1
+ module RiCal
2
+ class PropertyValue
3
+ #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
4
+ #
5
+ class Array < PropertyValue # :nodoc:
6
+
7
+ def value=(val)
8
+ case val
9
+ when String
10
+ @value = val.split(",")
11
+ else
12
+ @value = val
13
+ end
14
+ end
15
+
16
+ def value
17
+ @value.join(",")
18
+ end
19
+
20
+ def self.convert(timezone_finder, ruby_object) # :nodoc:
21
+ self.new(timezone_finder, :value => ruby_object)
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,11 @@
1
+ module RiCal
2
+ class PropertyValue
3
+ #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
4
+ #
5
+ # RiCal::PropertyValue::CalAddress represents an icalendar CalAddress property value
6
+ # which is defined in
7
+ # RFC 2445 section 4.3.3 p 34
8
+ class CalAddress < PropertyValue
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,169 @@
1
+ require 'date'
2
+ module RiCal
3
+ class PropertyValue
4
+ #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
5
+ #
6
+ # RiCal::PropertyValue::CalAddress represents an icalendar Date property value
7
+ # which is defined in
8
+ # RFC 2445 section 4.3.4 p 34
9
+ class Date < PropertyValue
10
+
11
+ def self.valid_string?(string) #:nodoc:
12
+ string =~ /^\d{8}$/
13
+ end
14
+
15
+ # Returns the value of the reciever as an RFC 2445 iCalendar string
16
+ def value
17
+ if @date_time_value
18
+ @date_time_value.ical_date_str
19
+ else
20
+ nil
21
+ end
22
+ end
23
+
24
+ # Set the value of the property to val
25
+ #
26
+ # val may be either:
27
+ #
28
+ # * A string which can be parsed as a DateTime
29
+ # * A Time instance
30
+ # * A Date instance
31
+ # * A DateTime instance
32
+ def value=(val)
33
+ case val
34
+ when nil
35
+ @date_time_value = nil
36
+ when String
37
+ @date_time_value = FastDateTime.from_date_time(::DateTime.parse(::DateTime.parse(val).strftime("%Y%m%d")))
38
+ when ::Time, ::Date, ::DateTime
39
+ @date_time_value = FastDateTime.from_date_time(::DateTime.parse(val.strftime("%Y%m%d")))
40
+ when FastDateTime
41
+ @date_time_value = val
42
+ end
43
+ end
44
+
45
+ # Nop to allow occurrence list to try to set it
46
+ def tzid=(val)#:nodoc:
47
+ end
48
+
49
+ def tzid #:nodoc:
50
+ nil
51
+ end
52
+
53
+ def visible_params #:nodoc:
54
+ {"VALUE" => "DATE"}.merge(params)
55
+ end
56
+
57
+ # Returns the year (including the century)
58
+ def year
59
+ @date_time_value.year
60
+ end
61
+
62
+ # Returns the month of the year (1..12)
63
+ def month
64
+ @date_time_value.month
65
+ end
66
+
67
+ # Returns the day of the month
68
+ def day
69
+ @date_time_value.day
70
+ end
71
+
72
+ # Returns the ruby representation a ::Date
73
+ def ruby_value
74
+ @date_time_value.date
75
+ end
76
+
77
+ alias_method :to_ri_cal_ruby_value, :ruby_value
78
+
79
+ # Return an instance of RiCal::PropertyValue::DateTime representing the start of this date
80
+ def to_ri_cal_date_time_value
81
+ PropertyValue::DateTime.new(timezone_finder, :value => @date_time_value)
82
+ end
83
+
84
+ # Return this date property
85
+ def to_ri_cal_date_value(timezone_finder = nil)
86
+ self
87
+ end
88
+
89
+ # Return the "Natural' property value for the date_property, in this case the date property itself."
90
+ def to_ri_cal_date_or_date_time_value
91
+ self
92
+ end
93
+
94
+ def advance(options) #:nodoc:
95
+ PropertyValue::Date.new(timezone_finder, :value => @date_time_value.advance(options), :params =>(params ? params.dup : nil) )
96
+ end
97
+
98
+ def change(options) #:nodoc:
99
+ PropertyValue::Date.new(timezone_finder,:value => @date_time_value.change(options), :params => (params ? params.dup : nil) )
100
+ end
101
+
102
+ def add_date_times_to(required_timezones) #:nodoc:
103
+ # Do nothing since dates don't have a timezone
104
+ end
105
+
106
+ # Return the difference between the receiver and other
107
+ #
108
+ # The parameter other should be either a RiCal::PropertyValue::Duration or a RiCal::PropertyValue::DateTime
109
+ #
110
+ # If other is a Duration, the result will be a DateTime, if it is a DateTime the result will be a Duration
111
+ def -(other)
112
+ other.subtract_from_date_time_value(to_ri_cal_date_time_value)
113
+ end
114
+
115
+ def subtract_from_date_time_value(date_time)
116
+ to_ri_cal_date_time_value.subtract_from_date_time_value(date_time)
117
+ end
118
+
119
+ # Return the sum of the receiver and duration
120
+ #
121
+ # The parameter other duration should be a RiCal::PropertyValue::Duration
122
+ #
123
+ # The result will be an RiCal::PropertyValue::DateTime
124
+ def +(duration)
125
+ duration.add_to_date_time_value(to_ri_cal_date_time_value)
126
+ end
127
+
128
+ # Delegate unknown messages to the wrappered Date instance.
129
+ # TODO: Is this really necessary?
130
+ def method_missing(selector, *args) #:nodoc:
131
+ @date_time_value.send(selector, *args)
132
+ end
133
+
134
+ # TODO: consider if this should be a period rather than a hash
135
+ def occurrence_period(default_duration) #:nodoc:
136
+ date_time = self.to_ri_cal_date_time_value
137
+ RiCal::OccurrencePeriod.new(date_time, date_time.advance(:hours => 24, :seconds => -1))
138
+ end
139
+
140
+ def start_of_day?
141
+ true
142
+ end
143
+
144
+ def to_floating_date_time_property
145
+ PropertyValue::DateTime.new(timezone_finder, :value => @date_time_value.ical_str)
146
+ end
147
+
148
+ def to_zulu_occurrence_range_start_time
149
+ to_floating_date_time_property.to_zulu_occurrence_range_start_time
150
+ end
151
+
152
+ def to_zulu_occurrence_range_finish_time
153
+ to_ri_cal_date_time_value.end_of_day.to_zulu_occurrence_range_finish_time
154
+ end
155
+
156
+ def to_finish_time
157
+ to_ri_cal_date_time_value.end_of_day.to_datetime
158
+ end
159
+
160
+ def for_occurrence(occurrence)
161
+ if occurrence.start_of_day?
162
+ occurrence.to_ri_cal_date_value(timezone_finder)
163
+ else
164
+ occurrence.for_parent(timezone_finder)
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,351 @@
1
+ require 'date'
2
+ module RiCal
3
+ class PropertyValue
4
+ #- ©2009 Rick DeNatale, All rights reserved. Refer to the file README.txt for the license
5
+ #
6
+ # RiCal::PropertyValue::CalAddress represents an icalendar CalAddress property value
7
+ # which is defined in RFC 2445 section 4.3.5 pp 35-37
8
+ class DateTime < PropertyValue
9
+
10
+ Dir[File.dirname(__FILE__) + "/date_time/*.rb"].sort.each do |path|
11
+ require path
12
+ end
13
+
14
+ include Comparable
15
+ include AdditiveMethods
16
+ include TimezoneSupport
17
+ include TimeMachine
18
+
19
+ def self.or_date(parent, line) # :nodoc:
20
+ if /T/.match(line[:value] || "")
21
+ new(parent, line)
22
+ else
23
+ PropertyValue::Date.new(parent, line)
24
+ end
25
+ end
26
+
27
+ def self.valid_string?(string) #:nodoc:
28
+ string =~ /^\d{8}T\d{6}Z?$/
29
+ end
30
+
31
+ def self.default_tzid # :nodoc:
32
+ @default_tzid ||= "UTC"
33
+ end
34
+
35
+ def self.params_for_tzid(tzid) #:nodoc:
36
+ if tzid == :floating
37
+ {}
38
+ else
39
+ {'TZID' => tzid}
40
+ end
41
+ end
42
+
43
+ # Set the default tzid to be used when instantiating an instance from a ruby object
44
+ # see RiCal::PropertyValue::DateTime.from_time
45
+ #
46
+ # The parameter tzid is a string value to be used for the default tzid, a value of :floating will cause
47
+ # values with NO timezone to be produced, which will be interpreted by iCalendar as floating times
48
+ # i.e. they are interpreted in the timezone of each client. Floating times are typically used
49
+ # to represent events which are 'repeated' in the various time zones, like the first hour of the year.
50
+ def self.default_tzid=(tzid)
51
+ @default_tzid = tzid
52
+ end
53
+
54
+ def self.default_tzid_hash # :nodoc:
55
+ if default_tzid.to_s == 'none'
56
+ {}
57
+ else
58
+ {'TZID' => default_tzid}
59
+ end
60
+ end
61
+
62
+ def inspect # :nodoc:
63
+ "#{@date_time_value}:#{tzid}"
64
+ end
65
+
66
+ # Returns the value of the receiver as an RFC 2445 iCalendar string
67
+ def value
68
+ if @date_time_value
69
+ "#{@date_time_value.ical_str}#{tzid == "UTC" ? "Z" : ""}"
70
+ else
71
+ nil
72
+ end
73
+ end
74
+
75
+ # Set the value of the property to val
76
+ #
77
+ # val may be either:
78
+ #
79
+ # * A string which can be parsed as a DateTime
80
+ # * A Time instance
81
+ # * A Date instance
82
+ # * A DateTime instance
83
+ def value=(val) # :nodoc:
84
+ case val
85
+ when nil
86
+ @date_time_value = nil
87
+ when String
88
+ @date_time_value = FastDateTime.from_date_time(::DateTime.parse(val))
89
+ if val =~/Z/
90
+ self.tzid = 'UTC'
91
+ else
92
+ @tzid ||= :floating
93
+ end
94
+ when FastDateTime
95
+ @date_time_value = val
96
+ when ::DateTime
97
+ @date_time_value = FastDateTime.from_date_time(val)
98
+ when ::Date, ::Time
99
+ @date_time_value = FastDateTime.from_date_time(::DateTime.parse(val.to_s))
100
+ end
101
+ reset_cached_values
102
+ end
103
+
104
+ # Extract the time and timezone identifier from an object used to set the value of a DATETIME property.
105
+ #
106
+ # If the object is a string it should be of the form [TZID=identifier:]
107
+ #
108
+ # Otherwise determine if the object acts like an activesupport enhanced time, and extract its timezone
109
+ # idenfifier if it has one.
110
+ #
111
+ def self.time_and_parameters(object)
112
+ parameters = {}
113
+ if ::String === object
114
+ object, parameters = self.time_and_parameters_from_string(object)
115
+ else
116
+ identifier = object.tzid rescue nil
117
+ parameters["TZID"] = identifier if identifier
118
+ end
119
+ [object, parameters]
120
+ end
121
+
122
+
123
+ def self.convert(timezone_finder, ruby_object) # :nodoc:
124
+ ruby_object.to_ri_cal_date_or_date_time_value(timezone_finder)
125
+ end
126
+
127
+ def self.from_string(string) # :nodoc:
128
+ if string.match(/Z$/)
129
+ new(nil, :value => string, :tzid => 'UTC')
130
+ else
131
+ new(nil, :value => string)
132
+ end
133
+ end
134
+
135
+ def for_parent(parent) #:nodoc:
136
+ if timezone_finder.nil?
137
+ @timezone_finder = parent
138
+ self
139
+ elsif parent == timezone_finder
140
+ self
141
+ else
142
+ DateTime.new(parent, :value => @date_time_value, :params => params, :tzid => tzid)
143
+ end
144
+ end
145
+
146
+ def visible_params # :nodoc:
147
+ result = {"VALUE" => "DATE-TIME"}.merge(params)
148
+ if has_local_timezone?
149
+ result['TZID'] = tzid
150
+ else
151
+ result.delete('TZID')
152
+ end
153
+ result
154
+ end
155
+
156
+ def params=(value) #:nodoc:
157
+ @params = value.dup
158
+ if params_timezone = @params['TZID']
159
+ self.tzid = @params['TZID']
160
+ end
161
+ end
162
+
163
+ # Return a Hash representing this properties parameters
164
+ def params
165
+ result = @params.dup
166
+ case tzid
167
+ when :floating, nil, "UTC"
168
+ result.delete('TZID')
169
+ else
170
+ result['TZID'] = tzid
171
+ end
172
+ result
173
+ end
174
+
175
+ # Compare the receiver with another object which must respond to the to_datetime message
176
+ # The comparison is done using the Ruby DateTime representations of the two objects
177
+ def <=>(other)
178
+ other.cmp_fast_date_time_value(@date_time_value)
179
+ end
180
+
181
+ def cmp_fast_date_time_value(other)
182
+ other <=> @date_time_value
183
+ end
184
+
185
+ # Determine if the receiver and other are in the same month
186
+ def in_same_month_as?(other)
187
+ [other.year, other.month] == [year, month]
188
+ end
189
+
190
+ def with_date_time_value(date_time_value)
191
+ PropertyValue::DateTime.new(
192
+ timezone_finder,
193
+ :value => date_time_value,
194
+ :params => (params),
195
+ :tzid => tzid
196
+ )
197
+ end
198
+
199
+ def nth_wday_in_month(n, which_wday) #:nodoc:
200
+ with_date_time_value(@date_time_value.nth_wday_in_month(n, which_wday))
201
+ end
202
+
203
+ def nth_wday_in_year(n, which_wday) #:nodoc:
204
+ with_date_time_value(@date_time_value.nth_wday_in_year(n, which_wday))
205
+ end
206
+
207
+ def self.civil(year, month, day, hour, min, sec, offset, start, params) #:nodoc:
208
+ PropertyValue::DateTime.new(
209
+ :value => ::DateTime.civil(year, month, day, hour, min, sec, offset, start),
210
+ :params =>(params ? params.dup : nil)
211
+ )
212
+ end
213
+
214
+ # Return the number of days in the month containing the receiver
215
+ def days_in_month
216
+ @date_time_value.days_in_month
217
+ end
218
+
219
+ # Determine if the receiver and another object are equivalent RiCal::PropertyValue::DateTime instances
220
+ def ==(other)
221
+ if self.class === other
222
+ self.value == other.value && self.visible_params == other.visible_params && self.tzid == other.tzid
223
+ else
224
+ super
225
+ end
226
+ end
227
+
228
+ # TODO: consider if this should be a period rather than a hash
229
+ def occurrence_period(default_duration) # :nodoc:
230
+ RiCal::OccurrencePeriod.new(self, (default_duration ? self + default_duration : nil))
231
+ end
232
+
233
+ # Return the year (including the century)
234
+ def year
235
+ @date_time_value.year
236
+ end
237
+
238
+ # Return the month of the year (1..12)
239
+ def month
240
+ @date_time_value.month
241
+ end
242
+
243
+ # Return the day of the month
244
+ def day
245
+ @date_time_value.day
246
+ end
247
+
248
+ alias_method :mday, :day
249
+
250
+ # Return the day of the week
251
+ def wday
252
+ @date_time_value.wday
253
+ end
254
+
255
+ # Return the hour
256
+ def hour
257
+ @date_time_value.hour
258
+ end
259
+
260
+ # Return the minute
261
+ def min
262
+ @date_time_value.min
263
+ end
264
+
265
+ # Return the second
266
+ def sec
267
+ @date_time_value.sec
268
+ end
269
+
270
+
271
+ # Return an RiCal::PropertyValue::DateTime representing the receiver.
272
+ def to_ri_cal_date_time_value(timezone=nil)
273
+ for_parent(timezone)
274
+ end
275
+
276
+ def iso_year_and_week_one_start(wkst) #:nodoc:
277
+ @date_time_value.iso_year_and_week_one_start(wkst)
278
+ end
279
+
280
+ def iso_weeks_in_year(wkst) #:nodoc:
281
+ @date_time_value.iso_weeks_in_year(wkst) #:nodoc:
282
+ end
283
+
284
+ # Return the "Natural' property value for the receover, in this case the receiver itself."
285
+ def to_ri_cal_date_or_date_time_value(timezone_finder = nil) #:nodoc:
286
+ self.for_parent(timezone_finder)
287
+ end
288
+
289
+ # Return a Date property for this DateTime
290
+ def to_ri_cal_date_value(timezone_finder=nil)
291
+ PropertyValue::Date.new(timezone_finder, :value => @date_time_value.ical_date_str)
292
+ end
293
+
294
+ # Return the Ruby DateTime representation of the receiver
295
+ def to_datetime #:nodoc:
296
+ @date_time_value.to_datetime
297
+ end
298
+
299
+ # Returns a ruby DateTime object representing the receiver.
300
+ def ruby_value
301
+ if has_valid_tzinfo_tzid? && RiCal::TimeWithZone && tz_info_source?
302
+ RiCal::TimeWithZone.new(utc.to_datetime, ::Time.__send__(:get_zone, @tzid))
303
+ else
304
+ ::DateTime.civil(year, month, day, hour, min, sec, rational_tz_offset).set_tzid(@tzid)
305
+ end
306
+ end
307
+
308
+ alias_method :to_ri_cal_ruby_value, :to_datetime
309
+ alias_method :to_finish_time, :ruby_value
310
+
311
+ def to_zulu_time
312
+ utc.to_datetime
313
+ end
314
+
315
+ # If a time is floating, then the utc of it's start time may actually be as early
316
+ # as 12 hours earlier if the occurrence is being viewed in a time zone just west
317
+ # of the International Date Line
318
+ def to_zulu_occurrence_range_start_time
319
+ if floating?
320
+ @date_time_value.advance(:hours => -12, :offset => 0).to_datetime
321
+ else
322
+ to_zulu_time
323
+ end
324
+ end
325
+
326
+
327
+ # If a time is floating, then the utc of it's start time may actually be as early
328
+ # as 12 hours later if the occurrence is being viewed in a time zone just east
329
+ # of the International Date Line
330
+ def to_zulu_occurrence_range_finish_time
331
+ if floating?
332
+ utc.advance(:hours => 12).to_datetime
333
+ else
334
+ to_zulu_time
335
+ end
336
+ end
337
+
338
+ def add_date_times_to(required_timezones) #:nodoc:
339
+ required_timezones.add_datetime(self, tzid) if has_local_timezone?
340
+ end
341
+
342
+ def start_of_day?
343
+ [hour, min, sec] == [0,0,0]
344
+ end
345
+
346
+ def for_occurrence(occurrence)
347
+ occurrence.to_ri_cal_date_time_value(timezone_finder)
348
+ end
349
+ end
350
+ end
351
+ end