Quoteing rpardee@comcast.net, on Fri, Feb 18, 2005 at 02:44:56AM +0900:
Thanks Wannes & Robert--that clears things up for me.
I wasn't actually aware of the Time class--interesting.
So here's a follow-up question. I understand that if I want to, I can
inject (is that the right word?) my own methods into standard
classes--so I can add my own add_hours() to DateTime if I like.
here's an example of how to do this, from http://vpim.rubyforge.org
Remember that the trivial "convert your units to seconds and add it to
Time" doesn't work in the face of leap years, daylight savings time,
etc, but Date has knowledge of this that you can exploit.
----- vpim/time.rb -----
=begin
$Id: time.rb,v 1.5 2005/02/02 02:55:59 sam Exp $
Copyright (C) 2005 Sam Roberts
This library is free software; you can redistribute it and/or modify it
under the same terms as the ruby language itself, see the file COPYING for
details.
=end
require 'date'
# Extensions to builtin Time allowing addition to Time by multiples of other
# intervals than a second.
class Time
# Returns a new Time, +years+ later than this time. Feb 29 of a
# leap year will be rounded up to Mar 1 if the target date is not a leap
# year.
def plus_year(years)
Time.local(year + years, month, day, hour, min, sec, usec)
end
# Returns a new Time, +months+ later than this time. The day will be
# rounded down if it is not valid for that month.
# 31 plus 1 month will be on Feb 28!
def plus_month(months)
d = Date.new(year, month, day)
d >>= months
Time.local(d.year, d.month, d.day, hour, min, sec, usec)
end
# Returns a new Time, +days+ later than this time.
# Does this do as I expect over DST? What if the hour doesn't exist
# in the next day, due to DST changes?
def plus_day(days)
d = Date.new(year, month, day)
d += days
Time.local(d.year, d.month, d.day, hour, min, sec, usec)
end
end
interpreter? If yes, how about adding the info on my new method to ri
and rdoc--would that be doable & if so, how?
rdoc -f ri ... *.rb
Cheers,
Sam