Module: RiCal::PropertyValue::TimeMachine

Included in:
DateTime
Defined in:
lib/ri_cal/property_value/date_time/time_machine.rb

Overview

  • ©2009 Rick DeNatale

  • All rights reserved. Refer to the file README.txt for the license

Methods for DateTime which support getting values at different point in time.

Instance Method Summary collapse

Instance Method Details

#advance(options) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 31

def advance(options) # :nodoc:
  PropertyValue::DateTime.new(timezone_finder,
                              :value => compute_advance(@date_time_value, options),
                              :tzid => tzid,
                              :params =>(params ? params.dup : nil)
  )
end

#at_end_of_iso_year(wkst) ⇒ Object

Return a DATE_TIME value representing the same time on the last day of the ISO year with weeks starting on wkst containing the receiver



153
154
155
156
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 153

def at_end_of_iso_year(wkst)
  num_weeks = @date_time_value.iso_weeks_in_year(wkst)
  at_start_of_iso_year(wkst).advance(:weeks => (num_weeks - 1), :days => 6)
end

#at_start_of_iso_year(wkst) ⇒ Object

Return a DATE_TIME value representing the same time on the first day of the ISO year with weeks starting on wkst containing the receiver



146
147
148
149
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 146

def at_start_of_iso_year(wkst)
  start_of_year = @date_time_value.iso_year_start(wkst)
  change(:year => start_of_year.year, :month => start_of_year.month, :day => start_of_year.day)
end

#at_start_of_next_iso_year(wkst) ⇒ Object

Return a DATE_TIME value representing the same time on the first day of the ISO year with weeks starting on wkst after the ISO year containing the receiver



160
161
162
163
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 160

def at_start_of_next_iso_year(wkst)
  num_weeks = @date_time_value.iso_weeks_in_year(wkst)
  at_start_of_iso_year(wkst).advance(:weeks => num_weeks)
end

#at_start_of_week_with_wkst(wkst) ⇒ Object

Return a DATE-TIME property representing the receiver on a different day (if necessary) so that the result is the first day of the ISO week starting on the wkst day containing the receiver.



80
81
82
83
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 80

def at_start_of_week_with_wkst(wkst)
  date = @date_time_value.start_of_week_with_wkst(wkst)
  change(:year => date.year, :month => date.month, :day => date.day)
end

#change(options) ⇒ Object

:nodoc:



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

def change(options) # :nodoc:
  PropertyValue::DateTime.new(timezone_finder,
                              :value => compute_change(@date_time_value, options),
                              :tzid => tzid,
                              :params => (params ? params.dup : nil)
  )
end

#change_day(new_day) ⇒ Object

:nodoc:



59
60
61
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 59

def change_day(new_day) #:nodoc:
  PropertyValue::DateTime.civil(self.year, self.month, new_day, self.hour, self.min, self.sec, self.offset, self.start, params)
end

#change_hour(new_hour) ⇒ Object

:nodoc:



55
56
57
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 55

def change_hour(new_hour) #:nodoc:
  PropertyValue::DateTime.civil(self.year, self.month, self.day, new_hour, self.min, self.sec, self.offset, self.start, params)
end

#change_min(new_min) ⇒ Object

:nodoc:



51
52
53
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 51

def change_min(new_min) #:nodoc:
  PropertyValue::DateTime.civil(self.year, self.month, self.day, self.hour, new_min, self.sec, self.offset, self.start, params)
end

#change_month(new_month) ⇒ Object

:nodoc:



63
64
65
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 63

def change_month(new_month) #:nodoc:
  PropertyValue::DateTime.civil(self.year, new_month, self.day, self.hour, self.min, self.sec, self.offset, self.start, params)
end

#change_sec(new_sec) ⇒ Object

:nodoc:



47
48
49
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 47

def change_sec(new_sec) #:nodoc:
  PropertyValue::DateTime.civil(self.year, self.month, self.day, self.hour, self.min, sec, self.offset, self.start, params)
end

#change_year(new_year) ⇒ Object

:nodoc:



67
68
69
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 67

def change_year(new_year) #:nodoc:
  PropertyValue::DateTime.civil(new_year, self.month, self.day, self.hour, self.min, self.sec, self.offset, self.start, params)
end

#compute_advance(d, options) ⇒ Object

:nodoc:



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

def compute_advance(d, options) # :nodoc:
  d = d >> options[:years] * 12 if options[:years]
  d = d >> options[:months]     if options[:months]
  d = d +  options[:weeks] * 7  if options[:weeks]
  d = d +  options[:days]       if options[:days]
  datetime_advanced_by_date = compute_change(@date_time_value, :year => d.year, :month => d.month, :day => d.day)
  seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
  seconds_to_advance == 0 ? datetime_advanced_by_date : datetime_advanced_by_date + Rational(seconds_to_advance.round, 86400)
end

#compute_change(d, options) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 8

def compute_change(d, options) # :nodoc:
  ::DateTime.civil(
  options[:year]  || d.year,
  options[:month] || d.month,
  options[:day]   || d.day,
  options[:hour]  || d.hour,
  options[:min]   || (options[:hour] ? 0 : d.min),
  options[:sec]   || ((options[:hour] || options[:min]) ? 0 : d.sec),
  options[:offset]  || d.offset,
  options[:start]  || d.start
  )
end

#end_of_dayObject

Return a DATE_TIME value representing the last second of the day containing the receiver



110
111
112
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 110

def end_of_day
  change(:hour => 23, :min => 59, :sec => 59)
end

#end_of_hourObject

Return a DATE_TIME value representing the last second of the hour containing the receiver



100
101
102
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 100

def end_of_hour
  change(:min => 59, :sec => 59)
end

#end_of_iso_year(wkst) ⇒ Object

Return a DATE_TIME value representing the last second of the last day of the ISO year with weeks starting on wkst containing the receiver



167
168
169
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 167

def end_of_iso_year(wkst)
  at_end_of_iso_year(wkst).end_of_day
end

#end_of_minuteObject

Return a DATE_TIME value representing the last second of the minute containing the receiver



90
91
92
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 90

def end_of_minute
  change(:sec => 59)
end

#end_of_monthObject

Return a DATE_TIME value representing the last second of the month containing the receiver



130
131
132
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 130

def end_of_month
  change(:day => days_in_month, :hour => 23, :min => 59, :sec => 59)
end

#end_of_week_with_wkst(wkst) ⇒ Object

Return a DATE_TIME value representing the last second of the ISO week starting with wkst containing the receiver



120
121
122
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 120

def end_of_week_with_wkst(wkst)
  date = at_start_of_week_with_wkst(wkst).advance(:days => 6).end_of_day
end

#end_of_yearObject

Return a DATE_TIME value representing the last second of the month containing the receiver



140
141
142
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 140

def end_of_year
  change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59)
end

#in_month(month) ⇒ Object

Return a DATE-TIME representing the same time, on the same day of the month in month. If the month of the receiver has more days than the target month the last day of the target month will be used.



174
175
176
177
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 174

def in_month(month)
  first = change(:day => 1, :month => month)
  first.change(:day => [first.days_in_month, day].min)
end

#in_week_starting?(date) ⇒ Boolean

Return a DATE-TIME property representing the receiver on a different day (if necessary) so that the result is within the 7 days starting with date

Returns:

  • (Boolean)


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

def in_week_starting?(date)
  wkst_jd = date.jd
  @date_time_value.jd.between?(wkst_jd, wkst_jd + 6)
end

#start_of_dayObject

Return a DATE_TIME value representing the first second of the day containing the receiver



105
106
107
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 105

def start_of_day
  change(:hour => 0, :min => 0, :sec => 0)
end

#start_of_hourObject

Return a DATE_TIME value representing the first second of the hour containing the receiver



95
96
97
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 95

def start_of_hour
  change(:min => 0, :sec => 0)
end

#start_of_minuteObject

Return a DATE_TIME value representing the first second of the minute containing the receiver



85
86
87
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 85

def start_of_minute
  change(:sec => 0)
end

#start_of_monthObject

Return a DATE_TIME value representing the first second of the month containing the receiver



125
126
127
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 125

def start_of_month
  change(:day => 1, :hour => 0, :min => 0, :sec => 0)
end

#start_of_week_with_wkst(wkst) ⇒ Object

Return a Ruby Date representing the first day of the ISO week starting with wkst containing the receiver



115
116
117
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 115

def start_of_week_with_wkst(wkst)
  @date_time_value.start_of_week_with_wkst(wkst)
end

#start_of_yearObject

Return a DATE_TIME value representing the first second of the month containing the receiver



135
136
137
# File 'lib/ri_cal/property_value/date_time/time_machine.rb', line 135

def start_of_year
  change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0)
end