Time granularity

P

Peter Bailey

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
 
K

khaines

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

Do it with Date instead of Time.

require 'date'
t1 = Date.new(2007,1,24)
t2 = Date.new(2007,1,31)
dates = t1 .. t2
dates.each do |date|
# your date stuff here
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:

Time.local(date.year,date.month,date.day)


Kirk Haines
 
R

Rob Biedenharn

Do it with Date instead of Time.

require 'date'
t1 = Date.new(2007,1,24)
t2 = Date.new(2007,1,31)
dates = t1 .. t2
dates.each do |date|
# your date stuff here
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:

Time.local(date.year,date.month,date.day)


Kirk Haines

Don't even deal with the range:
=> "01/28/07, Sunday"

Date#wday is the number of the weekday: 0=>Sunday, 6=>Saturday
01/31/99, Sunday
01/30/00, Sunday
01/28/01, Sunday
01/27/02, Sunday
01/26/03, Sunday
01/25/04, Sunday
01/30/05, Sunday
01/29/06, Sunday
01/28/07, Sunday

Then start adding!
day|
?> puts day.strftime("%D, %A")01/28/07, Sunday
02/25/07, Sunday
03/25/07, Sunday
04/22/07, Sunday
05/20/07, Sunday
06/17/07, Sunday
07/15/07, Sunday
08/12/07, Sunday
09/09/07, Sunday
10/07/07, Sunday
11/04/07, Sunday
12/02/07, Sunday
12/30/07, Sunday

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top