Hello,
I need to create some time variables for our company’s accounting budget
calendar. We have 13 budget periods in a year. The first one ends on the
last Sunday of January of whatever year it is. Once I find that day,
then I can simply add 28 days to it, consecutively, to get the remaining
budget fences. Anyway, I’m basically just starting with the 31st of
January and working back until I find the last Sunday of the month. I’ve
tried a string of if/elsif statements and I get close, but, it seems to
me I could do it with less code. If I create a range between that last
day of the month and all 7 days prior to it, that last Sunday should be
in there somewhere. When I try to work with ranges, though, and ask for
any results, it returns results for literally every second of every day
in that range! So, can anyone help me to ask Ruby to just give me days
back and not any subdivisions of those days?
t1 = Time.mktime(2007,1,24)
t2 = Time.mktime(2007,1,31)
dates = t1 … t2
dates.each for |date|
… #puts “date is a Sunday.”
end
end
Time uses seconds to represents a point in time. Time is fast, because
it’s really just a thin layer between Ruby and the underlying C
libraries.
Date and DateTime use days for their unit, represented by rational
numbers
(instances of the Rational class). They are a lot slower than Time
objects, but have some flexibility that Time doesn’t, and the use of
Days
for their unit means they work well for your application.
If you want to convert a Date into a Time, do this: