Module: Tempr::DateTimeRange

Included in:
SubRangeIterator
Defined in:
lib/tempr/date_time_range.rb

Overview

Extensions for date or time ranges (or range-like objects) To generate subranges from chainable rules

For example,

To generate a recurring hourly appointment at 2pm on the third Thursdays of each month in 2012:

range = (Date.civil(2012,1,1)...Date.civil(2013,1,1)).extend(Tempr::DateTimeRange)
subrange = range.each_month.thursday(2).at_time('2:00pm',60*60)

This gives you an enumerable you can iterate over:

pp subrange.to_a
#=>  [ 2012-01-19 14:00:00 -0500...2012-01-19 15:00:00 -0500,
       2012-02-16 14:00:00 -0500...2012-02-16 15:00:00 -0500,
       2012-03-15 14:00:00 -0400...2012-03-15 15:00:00 -0400,
       2012-04-19 14:00:00 -0400...2012-04-19 15:00:00 -0400,
       2012-05-17 14:00:00 -0400...2012-05-17 15:00:00 -0400,
       2012-06-21 14:00:00 -0400...2012-06-21 15:00:00 -0400,
       2012-07-19 14:00:00 -0400...2012-07-19 15:00:00 -0400,
       2012-08-16 14:00:00 -0400...2012-08-16 15:00:00 -0400,
       2012-09-20 14:00:00 -0400...2012-09-20 15:00:00 -0400,
       2012-10-18 14:00:00 -0400...2012-10-18 15:00:00 -0400,
       2012-11-15 14:00:00 -0500...2012-11-15 15:00:00 -0500,
       2012-12-20 14:00:00 -0500...2012-12-20 15:00:00 -0500  ]

Or check for inclusion of a date/time:

subrange.cover? Time.parse("2012-05-17 2:30pm")

which is a shortcut for

subrange.any? {|r| r.cover?(Time.parse("2012-05-17 2:30pm")) }

Note that the order of the chained rules is important, they must be defined from the widest to the narrowest date/time range.

During iteration, each rule is applied on the array of ranges defined by the previous rule.

The methods are roughly divided into methods for generating recurring subranges (e.g., “each_month”), and methods for finding a single subrange, by offset (e.g., “wednesday(1)” for the second wednesday)

In both cases, an enumerable is returned so that you can continue to chain rules together.

Defined Under Namespace

Classes: SubRangeIterator

Instance Method Summary collapse

Instance Method Details

#adjacent_to?(other) ⇒ Boolean

true iff self either succeeds or follows other range

Returns:

  • (Boolean)


530
531
532
# File 'lib/tempr/date_time_range.rb', line 530

def adjacent_to?(other)
  succeeds?(other) || precedes?(other)
end

#AprObject



90
# File 'lib/tempr/date_time_range.rb', line 90

def Apr ; self.April;     end

#AprilObject



78
# File 'lib/tempr/date_time_range.rb', line 78

def April     ; Date::MONTHNAMES.index("April");     end

#at_time(tm, dur = 0, utc_offset = nil) ⇒ Object



403
404
405
406
# File 'lib/tempr/date_time_range.rb', line 403

def at_time(tm,dur=0,utc_offset=nil) 
  warn "Note #at_time is deprecated and will be removed in 0.2.0; use #each_time_of_day instead"
  each_time_of_day(tm,dur,utc_offset)
end

#AugObject



93
# File 'lib/tempr/date_time_range.rb', line 93

def Aug ; self.August;    end

#AugustObject



82
# File 'lib/tempr/date_time_range.rb', line 82

def August    ; Date::MONTHNAMES.index("August");    end

#between_times(tm0, tm1, utc_offset = nil) ⇒ Object

time-of-day iterator specifying time range: “every day between tm0 and tm1, in utc_offset

parameters are any string that can be Time.parse’d - relative to the local time and zone. (Note the date portion is ignored, if given.)

if tm1 < tm0, ranges are interpreted to go into the next day, e.g.

`between_times "23:00", "01:00"`


416
417
418
419
420
421
422
423
424
425
# File 'lib/tempr/date_time_range.rb', line 416

def between_times(tm0,tm1,utc_offset=nil)
  tm0_p = Time.parse(tm0)
  tm1_p = Time.parse(tm1)
  dur = if tm0_p <= tm1_p
          tm1_p - tm0_p 
        else
          dur = 60*60*24 - (tm0_p - tm1_p)
        end
  each_time_of_day(tm0, dur, utc_offset)
end

#build_subrange(&builder) ⇒ Object

convenience wrapper for SubRangeIterator.new(self) { … }



537
538
539
# File 'lib/tempr/date_time_range.rb', line 537

def build_subrange(&builder)
  SubRangeIterator.new(self, &builder)
end

#day(offset = 0) ⇒ Object



159
# File 'lib/tempr/date_time_range.rb', line 159

def day(offset=0); each_day(1,offset).limit_to(1); end

#day_range(rng = self) ⇒ Object

convert to date range and make exclusive if not already For example,

`2012-02-01..2012-02-29`   becomes
`2012-02-01...2012-03-01`

while

`2012-02-01...2012-02-29`  is unmodified.


445
446
447
448
449
450
451
# File 'lib/tempr/date_time_range.rb', line 445

def day_range(rng=self)
  if rng.respond_to?(:exclude_end?) && rng.exclude_end?
    Range.new(rng.begin.to_date, rng.end.to_date, true)
  else
    Range.new(rng.begin.to_date, rng.end.to_date + 1, true)
  end
end

#DecObject



97
# File 'lib/tempr/date_time_range.rb', line 97

def Dec ; self.December;  end

#DecemberObject



86
# File 'lib/tempr/date_time_range.rb', line 86

def December  ; Date::MONTHNAMES.index("December");  end

#each_april(n = 1) ⇒ Object

“every nth April, grouped into one-month intervals”



306
# File 'lib/tempr/date_time_range.rb', line 306

def each_april(    n=1); each_monthnum(self.Apr,n); end

#each_august(n = 1) ⇒ Object

“every nth August, grouped into one-month intervals”



318
# File 'lib/tempr/date_time_range.rb', line 318

def each_august(   n=1); each_monthnum(self.Aug,n); end

#each_day_of_month(nday, dur = 1) ⇒ Object

day-of-month iterator: “every ndayth day of the month, grouped into dur day intervals”

nday is required.

if no dur parameter passed,

"every +nday+th day of the month, grouped into one-day intervals"


360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/tempr/date_time_range.rb', line 360

def each_day_of_month(nday,dur=1)
  build_subrange do |s|
    s.step   = 1
    s.adjust_range   { |r| day_range(r) }
    s.offset         do |dt| 
                          totdays = ((Date.civil(dt.year,dt.month,1) >> 1)-1).day
                          dt.to_date + (nday - dt.day)%totdays
                     end
    s.increment      { |dt,i| dt.to_date >> i }
    s.span           { |dt| dt.to_date + dur  }
  end
end

#each_days(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_day

days iterator: “every n days, starting at offset days, dur day intervals”

if no parameters passed,

"every day of range grouped into one-day intervals"


148
149
150
151
152
153
154
155
156
# File 'lib/tempr/date_time_range.rb', line 148

def each_days(n=1,offset=0,dur=1)
  build_subrange do |s|
    s.step    = n
    s.adjust_range  { |r| day_range(r) }
    s.offset        { |dt|   dt.to_date + offset }
    s.increment     { |dt,i| dt.to_date + i }
    s.span          { |dt| dt.to_date + dur }
  end
end

#each_days_of_week(*wdays) ⇒ Object Also known as: each_day_of_week

multiple day-of-week iterator: “every days of the week wdays

For example,

`range.each_days_of_week(range.Tue, range.Thu)`
# "every Tuesday and Thursday in range"

if no parameter passed, identical to each_days



233
234
235
236
237
238
239
# File 'lib/tempr/date_time_range.rb', line 233

def each_days_of_week(*wdays)
  if wdays.empty?
    each_days
  else
    each_days.except {|dt| !wdays.include?(dt.wday) }
  end
end

#each_december(n = 1) ⇒ Object

“every nth December, grouped into one-month intervals”



330
# File 'lib/tempr/date_time_range.rb', line 330

def each_december( n=1); each_monthnum(self.Dec,n); end

#each_february(n = 1) ⇒ Object

“every nth February, grouped into one-month intervals”



300
# File 'lib/tempr/date_time_range.rb', line 300

def each_february( n=1); each_monthnum(self.Feb,n); end

#each_friday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Friday, starting at offset weeks, dur day intervals”



207
# File 'lib/tempr/date_time_range.rb', line 207

def each_friday(   n=1, offset=0, dur=1); each_wdays(self.Fri,n,offset,dur); end

#each_hours(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_hour

hours iterator: “every n hours, starting at offset hours, dur hour intervals”

if no parameters passed,

"every hour of range grouped into one-hour intervals"


136
137
138
# File 'lib/tempr/date_time_range.rb', line 136

def each_hours(n=1, offset=0, dur=1)
  each_seconds(n*60*60, offset*60*60, dur*60*60)
end

#each_january(n = 1) ⇒ Object

“every nth January, grouped into one-month intervals”



297
# File 'lib/tempr/date_time_range.rb', line 297

def each_january(  n=1); each_monthnum(self.Jan,n); end

#each_july(n = 1) ⇒ Object

“every nth July, grouped into one-month intervals”



315
# File 'lib/tempr/date_time_range.rb', line 315

def each_july(     n=1); each_monthnum(self.Jul,n); end

#each_june(n = 1) ⇒ Object

“every nth June, grouped into one-month intervals”



312
# File 'lib/tempr/date_time_range.rb', line 312

def each_june(     n=1); each_monthnum(self.Jun,n); end

#each_march(n = 1) ⇒ Object

“every nth Mary, grouped into one-month intervals”



303
# File 'lib/tempr/date_time_range.rb', line 303

def each_march(    n=1); each_monthnum(self.Mar,n); end

#each_may(n = 1) ⇒ Object

“every nth May, grouped into one-month intervals”



309
# File 'lib/tempr/date_time_range.rb', line 309

def each_may(      n=1); each_monthnum(self.May,n); end

#each_minutes(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_minute

minutes iterator: “every n minutes, starting at offset minutes, dur minute intervals”

if no parameters passed,

"every minute of range grouped into one-minute intervals"


124
125
126
# File 'lib/tempr/date_time_range.rb', line 124

def each_minutes(n=1, offset=0, dur=1)
  each_seconds(n*60, offset*60, dur*60)
end

#each_monday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Monday, starting at offset weeks, dur day intervals”



195
# File 'lib/tempr/date_time_range.rb', line 195

def each_monday(   n=1, offset=0, dur=1); each_wdays(self.Mon,n,offset,dur); end

#each_monthnum(nmonth, n = 1) ⇒ Object

month-of-year iterator: “every nth month nmonth grouped into one-month intervals”

nmonth is required. Typically, each_january, etc. called instead.

if n parameter not passed,

"every month number +nmonth+ of range grouped into one-month intervals"


286
287
288
289
290
291
292
293
294
# File 'lib/tempr/date_time_range.rb', line 286

def each_monthnum(nmonth,n=1)
  build_subrange do |s|
    s.step   = n
    s.adjust_range { |r| day_range(r) }
    s.offset       { |dt| dt >> (nmonth - dt.month)%12 }
    s.increment    { |dt,i| dt.to_date >> i*12 }      
    s.span         { |dt| dt.to_date >> 1 }
  end
end

#each_months(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_month

month iterator: “every n months, starting at offset months, dur month intervals”

if no parameters passed,

"every month of range grouped into one-month intervals"


266
267
268
269
270
271
272
273
274
# File 'lib/tempr/date_time_range.rb', line 266

def each_months(n=1,offset=0,dur=1)
  build_subrange do |s|
    s.step    = n
    s.adjust_range { |r| day_range(r) }
    s.offset       { |dt|   dt.to_date >> offset }
    s.increment    { |dt,i| dt.to_date >> i }
    s.span         { |dt| dt.to_date >> dur }
  end
end

#each_november(n = 1) ⇒ Object

“every nth November, grouped into one-month intervals”



327
# File 'lib/tempr/date_time_range.rb', line 327

def each_november( n=1); each_monthnum(self.Nov,n); end

#each_october(n = 1) ⇒ Object

“every nth October, grouped into one-month intervals”



324
# File 'lib/tempr/date_time_range.rb', line 324

def each_october(  n=1); each_monthnum(self.Oct,n); end

#each_saturday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Saturday, starting at offset weeks, dur day intervals”



210
# File 'lib/tempr/date_time_range.rb', line 210

def each_saturday( n=1, offset=0, dur=1); each_wdays(self.Sat,n,offset,dur); end

#each_seconds(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_second

seconds iterator:

"every +n+ seconds, starting at +offset+, grouped into +dur+ second intervals"

if no parameters passed,

"every second of range grouped into one-second intervals"


106
107
108
109
110
111
112
113
114
# File 'lib/tempr/date_time_range.rb', line 106

def each_seconds(n=1, offset=0, dur=1)
  build_subrange do |s|
    s.step    = n
    s.adjust_range { |r| time_range(r) }
    s.offset       { |tm|   tm + offset }
    s.increment    { |tm,i| tm + i }
    s.span         { |tm| tm + dur }
  end
end

#each_september(n = 1) ⇒ Object

“every nth September, grouped into one-month intervals”



321
# File 'lib/tempr/date_time_range.rb', line 321

def each_september(n=1); each_monthnum(self.Sep,n); end

#each_sunday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Sunday, starting at offset weeks, dur day intervals”



192
# File 'lib/tempr/date_time_range.rb', line 192

def each_sunday(   n=1, offset=0, dur=1); each_wdays(self.Sun,n,offset,dur); end

#each_thursday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Thursday, starting at offset weeks, dur day intervals”



204
# File 'lib/tempr/date_time_range.rb', line 204

def each_thursday( n=1, offset=0, dur=1); each_wdays(self.Thu,n,offset,dur); end

#each_time_of_day(tm, dur = 0, utc_offset = nil) ⇒ Object

time-of-day iterator: “every day at tm, grouped into dur second intervals, in utc_offset

tm is any string that can be Time.parse’d - relative to the local time and zone. (Note the date portion is ignored, if given.)

if no dur passed, intervals are ‘instantaneous’ time ranges if no utc_offset passed, process-local timezone is used.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/tempr/date_time_range.rb', line 386

def each_time_of_day(tm,dur=0,utc_offset=nil)
  tm_p = Time.parse(tm)
  build_subrange do |s|
    s.step    = 60*60*24
    s.adjust_range    { |r| time_range(r,utc_offset) }        
    s.offset          do |t| 
                           Time.new( 
                             t.year,   t.month, t.day,
                             tm_p.hour, tm_p.min, tm_p.sec, 
                             (t + s.step).utc_offset
                           )
                      end
    s.increment       { |t,i| t + i }
    s.span            { |t|   t + dur }
  end
end

#each_tuesday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Tuesday, starting at offset weeks, dur day intervals”



198
# File 'lib/tempr/date_time_range.rb', line 198

def each_tuesday(  n=1, offset=0, dur=1); each_wdays(self.Tue,n,offset,dur); end

#each_wdays(wd, n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_wday

single day-of-week iterator: “every nth weekday wd, starting at offset weeks, dur day intervals”

wd is required. Typically, each_sunday, each_monday called instead.

if no other parameters passed,

"every weekday +wd+ of range grouped into one-day intervals"


180
181
182
183
184
185
186
187
188
# File 'lib/tempr/date_time_range.rb', line 180

def each_wdays(wd,n=1,offset=0,dur=1)
  build_subrange do |s|
    s.step    = n
    s.adjust_range  { |r| day_range(r) }
    s.offset        { |dt|   dt.to_date + (wd - dt.to_date.wday)%7 + offset*7 }
    s.increment     { |dt,i| dt.to_date + i*7 }
    s.span          { |dt| dt.to_date + dur }
  end
end

#each_wednesday(n = 1, offset = 0, dur = 1) ⇒ Object

“every nth Wednesday, starting at offset weeks, dur day intervals”



201
# File 'lib/tempr/date_time_range.rb', line 201

def each_wednesday(n=1, offset=0, dur=1); each_wdays(self.Wed,n,offset,dur); end

#each_weekdaysObject Also known as: each_weekday

every weekday



243
244
245
# File 'lib/tempr/date_time_range.rb', line 243

def each_weekdays
  each_days_of_week(*self.WEEKDAYS)
end

#each_weekendsObject Also known as: each_weekend

every weekend (Saturday and Sunday)



249
250
251
# File 'lib/tempr/date_time_range.rb', line 249

def each_weekends
  each_days_of_week(*self.WEEKENDS)
end

#each_weekends_including_fridayObject Also known as: each_weekend_including_friday

every Friday, Saturday, and Sunday



255
256
257
# File 'lib/tempr/date_time_range.rb', line 255

def each_weekends_including_friday
  each_days_of_week(*([self.Fri] + self.WEEKENDS))
end

#each_weeks(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_week

weeks iterator: “every n weeks, starting at offset weeks, dur week intervals”

if no parameters passed,

"every week of range grouped into one-week intervals"


166
167
168
# File 'lib/tempr/date_time_range.rb', line 166

def each_weeks(n=1, offset=0, dur=1)
  each_days(n*7, offset*7, dur*7)
end

#each_years(n = 1, offset = 0, dur = 1) ⇒ Object Also known as: each_year

year iterator: “every n years, starting at offset years, dur year intervals”

if no parameters passed,

"every year of range grouped into one-year intervals"


338
339
340
341
342
343
344
345
346
# File 'lib/tempr/date_time_range.rb', line 338

def each_years(n=1,offset=0,dur=1)
  build_subrange do |s|
    s.step    = n
    s.adjust_range { |r| day_range(r) }
    s.offset       { |dt|   Date.civil(dt.year + offset, dt.month, dt.day) }
    s.increment    { |dt,i| Date.civil(dt.year + i,      dt.month, dt.day) }
    s.span         { |dt|   Date.civil(dt.year + dur, dt.month, dt.day) }
  end 
end

#FebObject



88
# File 'lib/tempr/date_time_range.rb', line 88

def Feb ; self.February;  end

#FebruaryObject



76
# File 'lib/tempr/date_time_range.rb', line 76

def February  ; Date::MONTHNAMES.index("February");  end

#FriObject



67
# File 'lib/tempr/date_time_range.rb', line 67

def Fri ; self.Friday;    end

#friday(offset = 0) ⇒ Object



222
# File 'lib/tempr/date_time_range.rb', line 222

def friday(offset=0);    wday(self.Fri,offset); end

#FridayObject



60
# File 'lib/tempr/date_time_range.rb', line 60

def Friday    ; Date::DAYNAMES.index("Friday");    end

#hour(offset = 0) ⇒ Object



141
# File 'lib/tempr/date_time_range.rb', line 141

def hour(offset=0); each_hours(1,offset).limit_to(1); end

#intersection_with(other) ⇒ Object

the range intersection of self and other note: returns nil if no intersection



498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/tempr/date_time_range.rb', line 498

def intersection_with(other)
  r1,r2 = matched_range_types(self, other)
  max_begin = [r1.begin,r2.begin].max
  min_end   = [r1.end,  r2.end  ].min
  excl = ( r1.end == min_end && 
           r1.respond_to?(:exclude_end?) && r1.exclude_end?
         ) ||
         ( r2.end == min_end && 
           r2.respond_to?(:exclude_end?) && r2.exclude_end?
         )
  unless max_begin > min_end
    Range.new(max_begin, min_end, excl).extend(DateTimeRange)
  end
end

#intersects?(other) ⇒ Boolean

true iff there is a range intersection of self and other

Returns:

  • (Boolean)


514
515
516
# File 'lib/tempr/date_time_range.rb', line 514

def intersects?(other)
  !!intersection_with(other)
end

#JanObject



87
# File 'lib/tempr/date_time_range.rb', line 87

def Jan ; self.January;   end

#JanuaryObject

month shortcuts - as methods so accessible to mixin target classes



75
# File 'lib/tempr/date_time_range.rb', line 75

def January   ; Date::MONTHNAMES.index("January");   end

#JulObject



92
# File 'lib/tempr/date_time_range.rb', line 92

def Jul ; self.July;      end

#JulyObject



81
# File 'lib/tempr/date_time_range.rb', line 81

def July      ; Date::MONTHNAMES.index("July");      end

#JunObject



91
# File 'lib/tempr/date_time_range.rb', line 91

def Jun ; self.June;      end

#JuneObject



80
# File 'lib/tempr/date_time_range.rb', line 80

def June      ; Date::MONTHNAMES.index("June");      end

#MarObject



89
# File 'lib/tempr/date_time_range.rb', line 89

def Mar ; self.March;     end

#MarchObject



77
# File 'lib/tempr/date_time_range.rb', line 77

def March     ; Date::MONTHNAMES.index("March");     end

#MayObject



79
# File 'lib/tempr/date_time_range.rb', line 79

def May       ; Date::MONTHNAMES.index("May");       end

#minute(offset = 0) ⇒ Object



129
# File 'lib/tempr/date_time_range.rb', line 129

def minute(offset=0); each_minutes(1,offset).limit_to(1); end

#MonObject



63
# File 'lib/tempr/date_time_range.rb', line 63

def Mon ; self.Monday;    end

#monday(offset = 0) ⇒ Object



218
# File 'lib/tempr/date_time_range.rb', line 218

def monday(offset=0);    wday(self.Mon,offset); end

#MondayObject



56
# File 'lib/tempr/date_time_range.rb', line 56

def Monday    ; Date::DAYNAMES.index("Monday");    end

#month(offset = 0) ⇒ Object



277
# File 'lib/tempr/date_time_range.rb', line 277

def month(offset=0); each_months(1,offset).limit_to(1); end

#NovObject



96
# File 'lib/tempr/date_time_range.rb', line 96

def Nov ; self.November;  end

#NovemberObject



85
# File 'lib/tempr/date_time_range.rb', line 85

def November  ; Date::MONTHNAMES.index("November");  end

#OctObject



95
# File 'lib/tempr/date_time_range.rb', line 95

def Oct ; self.October;   end

#OctoberObject



84
# File 'lib/tempr/date_time_range.rb', line 84

def October   ; Date::MONTHNAMES.index("October");   end

#on_day(nday, dur = 1) ⇒ Object



373
374
375
376
# File 'lib/tempr/date_time_range.rb', line 373

def on_day(nday,dur=1)
  warn "Note #on_day is deprecated and will be removed in 0.2.0; use #each_day_of_month instead"
  each_day_of_month(nday,dur)
end

#precedes?(other) ⇒ Boolean

true iff self immediately precedes other range

Returns:

  • (Boolean)


519
520
521
# File 'lib/tempr/date_time_range.rb', line 519

def precedes?(other)
  range_precedes(self, other)
end

#SatObject



68
# File 'lib/tempr/date_time_range.rb', line 68

def Sat ; self.Saturday;  end

#saturday(offset = 0) ⇒ Object



223
# File 'lib/tempr/date_time_range.rb', line 223

def saturday(offset=0);  wday(self.Sat,offset); end

#SaturdayObject



61
# File 'lib/tempr/date_time_range.rb', line 61

def Saturday  ; Date::DAYNAMES.index("Saturday");  end

#second(offset = 0) ⇒ Object



117
# File 'lib/tempr/date_time_range.rb', line 117

def second(offset=0); each_seconds(1,offset).limit_to(1); end

#SepObject



94
# File 'lib/tempr/date_time_range.rb', line 94

def Sep ; self.September; end

#SeptemberObject



83
# File 'lib/tempr/date_time_range.rb', line 83

def September ; Date::MONTHNAMES.index("September"); end

#subsume?(other) ⇒ Boolean

true iff other.begin <= self.begin and other.end >= self.end (with accomodations for exclusive-end ranges)

Returns:

  • (Boolean)


492
493
494
# File 'lib/tempr/date_time_range.rb', line 492

def subsume?(other)
  range_within_other?(other,self)
end

#succeeds?(other) ⇒ Boolean Also known as: follows?

true iff self immediately succeeds (follows) other range

Returns:

  • (Boolean)


524
525
526
# File 'lib/tempr/date_time_range.rb', line 524

def succeeds?(other)
  range_precedes(other, self)
end

#SunObject



62
# File 'lib/tempr/date_time_range.rb', line 62

def Sun ; self.Sunday;    end

#sunday(offset = 0) ⇒ Object



217
# File 'lib/tempr/date_time_range.rb', line 217

def sunday(offset=0);    wday(self.Sun,offset); end

#SundayObject

day of week shortcuts - as methods so accessible to mixin target classes Note probably should change this so it copies constants over in extend_object or something



55
# File 'lib/tempr/date_time_range.rb', line 55

def Sunday    ; Date::DAYNAMES.index("Sunday");    end

#ThuObject



66
# File 'lib/tempr/date_time_range.rb', line 66

def Thu ; self.Thursday;  end

#thursday(offset = 0) ⇒ Object



221
# File 'lib/tempr/date_time_range.rb', line 221

def thursday(offset=0);  wday(self.Thu,offset); end

#ThursdayObject



59
# File 'lib/tempr/date_time_range.rb', line 59

def Thursday  ; Date::DAYNAMES.index("Thursday");  end

#time_range(rng = self, utc_offset = nil) ⇒ Object

unless already a time range, convert to exclusive date range, and then to time range

Times are local time unless utc_offset is passed

For example,

`time_range(2012-02-01..2012-02-29,'00:00')`   
#=> `2012-02-01 00:00:00 +00:00...2012-03-01 00:00:00 +00:00`


463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/tempr/date_time_range.rb', line 463

def time_range(rng=self,utc_offset=nil)
  if rng.begin.respond_to?(:sec) && rng.end.respond_to?(:sec)
    utc_offset ||= rng.begin.utc_offset
    Range.new( rng.begin.getlocal(utc_offset),
               rng.end.getlocal(utc_offset),
               rng.respond_to?(:exclude_end?) && rng.exclude_end?
             )
  else
    adj_rng = day_range(rng)
    b,e = adj_rng.begin, adj_rng.end
    Range.new( Time.new(b.year,b.month,b.day,0,0,0,utc_offset), 
               Time.new(e.year,e.month,e.day,0,0,0,utc_offset), 
               true
             )
  end
end

#TueObject



64
# File 'lib/tempr/date_time_range.rb', line 64

def Tue ; self.Tuesday;   end

#tuesday(offset = 0) ⇒ Object



219
# File 'lib/tempr/date_time_range.rb', line 219

def tuesday(offset=0);   wday(self.Tue,offset); end

#TuesdayObject



57
# File 'lib/tempr/date_time_range.rb', line 57

def Tuesday   ; Date::DAYNAMES.index("Tuesday");   end

#wday(wd, offset = 0) ⇒ Object



213
214
215
# File 'lib/tempr/date_time_range.rb', line 213

def wday(wd,offset=0)
  each_wdays(wd,1,offset).limit_to(1)
end

#WedObject



65
# File 'lib/tempr/date_time_range.rb', line 65

def Wed ; self.Wednesday; end

#wednesday(offset = 0) ⇒ Object



220
# File 'lib/tempr/date_time_range.rb', line 220

def wednesday(offset=0); wday(self.Wed,offset); end

#WednesdayObject



58
# File 'lib/tempr/date_time_range.rb', line 58

def Wednesday ; Date::DAYNAMES.index("Wednesday"); end

#week(offset = 0) ⇒ Object



171
# File 'lib/tempr/date_time_range.rb', line 171

def week(offset=0); each_weeks(1,offset).limit_to(1); end

#WEEKDAYSObject



70
# File 'lib/tempr/date_time_range.rb', line 70

def WEEKDAYS; [self.Mon, self.Tue, self.Wed, self.Thu, self.Fri]; end

#WEEKENDSObject



71
# File 'lib/tempr/date_time_range.rb', line 71

def WEEKENDS; [self.Sat, self.Sun]; end

#within?(other) ⇒ Boolean

true iff self.begin <= other.begin and self.end >= other.end (with accomodations for exclusive-end ranges)

Returns:

  • (Boolean)


486
487
488
# File 'lib/tempr/date_time_range.rb', line 486

def within?(other)
  range_within_other?(self,other)
end

#year(offset = 0) ⇒ Object



349
# File 'lib/tempr/date_time_range.rb', line 349

def year(offset=0); each_year(1,offset).limit_to(1); end