class RiCal::FastDateTime

FastDateTime mimics the Ruby Standard library DateTime class but avoids the use of Rational
Instead of using a Rational for the utc offset, FastDateTime uses an integer seconds value

Constants

SECONDS_IN_A_DAY

Attributes

date[RW]
hour[RW]
min[RW]
offset[RW]
sec[RW]
secs_since_bod[RW]
utc_offset_seconds[RW]

Public Class Methods

from_date(date) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 28
def self.from_date(date)
  new(date.year, date.month, date.day, 0, 0, 0, 0)
end
from_date_at_end_of_day(date) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 32
def self.from_date_at_end_of_day(date)
  new(date.year, date.month, date.day, 23, 59, 59, 0)
end
from_date_time(date_time) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 20
def self.from_date_time(date_time)
  new(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.min, date_time.sec, (date_time.offset * SECONDS_IN_A_DAY).to_i)
end
from_time(time) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 24
def self.from_time(time)
  new(time.year, time.month, time.day, time.hour, time.min, time.sec, (time.utc_offset.offset * SECONDS_IN_A_DAY))
end
new(year, month, day, hour, min, sec, offset_seconds) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 14
def initialize(year, month, day, hour, min, sec, offset_seconds)
  @date = Date.civil(year, month, day)
  @secs_since_bod = hms_to_seconds(hour, min, sec)
  @hour, @min, @sec, @offset = hour, min, sec, offset_seconds
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 72
def <=> (other)
  if FastDateTime === other
    [date, secs_since_bod] <=> [other.date, other.secs_since_bod]
  else
    [year, month, day, hour, min, sec] <=> [other.year, other.month, other.day, other.hour, other.min, other.sec]
  end
end
==(other) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 68
def ==(other)
  [date, secs_since_bod, offset] == [other.date, other.secs_since_bod, other.offset]
end
adjust_day_delta(day_delta, new_secs_since_bod) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 136
def adjust_day_delta(day_delta, new_secs_since_bod)
  if new_secs_since_bod == 0
    [day_delta, new_secs_since_bod]
  elsif new_secs_since_bod > 0
    [day_delta + (new_secs_since_bod / SECONDS_IN_A_DAY), new_secs_since_bod % SECONDS_IN_A_DAY]
  else
    [day_delta - (1 + new_secs_since_bod.abs / SECONDS_IN_A_DAY), 
     SECONDS_IN_A_DAY - (new_secs_since_bod.abs % SECONDS_IN_A_DAY)]
  end
 end
change(options) click to toggle source

Return a new FastDateTime based on the receiver but with changes specified by the options

# File lib/ri_cal/fast_date_time.rb, line 95
def change(options)
  FastDateTime.new(
  options[:year]  || year,
  options[:month] || month,
  options[:day]   || day,
  options[:hour]  || hour,
  options[:min]   || (options[:hour] ? 0 : min),
  options[:sec]   || ((options[:hour] || options[:min]) ? 0 : sec),
  options[:offset]  || offset
  )
end
cmp_fast_date_time_value(other) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 227
def cmp_fast_date_time_value(other)
  other <=> self
end
day() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 56
def day
  @date.day
end
days_in_month() click to toggle source

def jd

date.jd

end

# File lib/ri_cal/fast_date_time.rb, line 88
def days_in_month
  date.days_in_month
end
hms_to_seconds(hours, minutes, seconds) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 123
def hms_to_seconds(hours, minutes, seconds)
  seconds + 60 *(minutes + (60 * hours))
end
ical_date_str() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 42
def ical_date_str
  "%04d%02d%02d" % [year, month, day]
end
ical_str() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 38
def ical_str
  "%04d%02d%02dT%02d%02d%02d" % [year, month, day, hour, min, sec]
end
inspect()
Alias for: to_s
iso_weeks_in_year(wkst) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 215
def iso_weeks_in_year(wkst)
  @date.iso_weeks_in_year(wkst)
end
iso_year_and_week_one_start(wkst) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 223
def iso_year_and_week_one_start(wkst)
  @date.iso_year_and_week_one_start(wkst)
end
iso_year_start(wkst) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 219
def iso_year_start(wkst)
  @date.iso_year_start(wkst)
end
mon()
Alias for: month
month() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 50
def month
  @date.month
end
Also aliased as: mon
nth_wday_in_month(n, which_wday) click to toggle source

e.g. to obtain the 3nd Tuesday of the receivers month use

time.nth_wday_in_month(2, 2)
# File lib/ri_cal/fast_date_time.rb, line 170
def nth_wday_in_month(n, which_wday)
  first_of_month = change(:day => 1)
  first_in_month = first_of_month.advance(:days => (which_wday - first_of_month.wday))
  first_in_month = first_in_month.advance(:days => 7) if first_in_month.month != first_of_month.month
  if n > 0
    first_in_month.advance(:days => (7*(n - 1)))
  else
    possible = first_in_month.advance(:days => 21)
    possible = possible.advance(:days => 7) while possible.month == first_in_month.month
    last_in_month = possible.advance(:days => - 7)
    (last_in_month.advance(:days => - (7*(n.abs - 1))))
  end
end
nth_wday_in_year(n, which_wday) click to toggle source

e.g. to obtain the 2nd Monday of the receivers year use

time.nth_wday_in_year(2, 1)
# File lib/ri_cal/fast_date_time.rb, line 193
def nth_wday_in_year(n, which_wday)
  if n > 0
    first_of_year = change(:month => 1, :day => 1)
    first_in_year = first_of_year.advance(:days => (which_wday - first_of_year.wday + 7) % 7)
    first_in_year.advance(:days => (7*(n - 1)))
  else
    december25 = change(:month => 12, :day => 25)
    last_in_year = december25.advance(:days => (which_wday - december25.wday + 7) % 7)
    last_in_year.advance(:days => (7 * (n + 1)))
  end
end
seconds_to_hms(total_seconds) click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 127
def seconds_to_hms(total_seconds)
  sign = total_seconds <=> 0
  remaining = total_seconds.abs
  seconds = sign * (remaining % 60)
  remaining = remaining / 60
  minutes = sign * (remaining % 60)
  [remaining / 60, minutes, seconds]
end
start_of_week_with_wkst(wkst) click to toggle source

Return a DateTime which is the beginning of the first day on or before the receiver with the specified wday

# File lib/ri_cal/fast_date_time.rb, line 208
def start_of_week_with_wkst(wkst)
  wkst ||= 1
  date = @date
  date -= 1 while date.wday != wkst
  date
end
to_datetime() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 64
def to_datetime
  DateTime.civil(year, month, day, hour, min, sec, RiCal.RationalOffset[utc_offset_seconds])
end
to_s() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 80
def to_s
  "#{year}/#{month}/#{day} #{hour}:#{min}:#{sec} #{offset}"
end
Also aliased as: inspect
utc() click to toggle source

def new_offset(ofst)

if ofst == offset
  self
else
  advance(:seconds => offset - ofset, :offset => ofst)
end

end

# File lib/ri_cal/fast_date_time.rb, line 115
def utc
  if offset == 0
    self
  else
    advance(:seconds => -offset, :offset => 0)
  end
end
wday() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 60
def wday
  @date.wday
end
year() click to toggle source
# File lib/ri_cal/fast_date_time.rb, line 46
def year
  @date.year
end