Time question

P

Phlip

joep said:
I am writing a small piece that will include a date that needs to be
compared to the current time.

EX. a = Time.now
b = db_cert_date + (72 months)

Rails's ActiveSupport package lets you say some_date + 72.months. It's that
simple; the secret unit of exchange is probably seconds.

If this were Brand X, using another platform's low-level library would be
eternal torment, but this is Ruby, so just try require 'active_support'!
 
J

joep

I am writing a small piece that will include a date that needs to be
compared to the current time.

EX. a = Time.now
b = db_cert_date + (72 months)

if(a < b)
do something
elsif(a > b)
do something else
else
do this
end

I am looking for the best way to add 72 months onto an old date and
then compare it with the current date. Does anyone have any ideas?

Thanks in advance
 
J

joep

Rails's ActiveSupport package lets you say some_date + 72.months. It's that
simple; the secret unit of exchange is probably seconds.

If this were Brand X, using another platform's low-level library would be
eternal torment, but this is Ruby, so just try require 'active_support'!

Only problem is I'm not using rails. Just straight ruby as a cgi.
 
R

Rob Biedenharn

I am writing a small piece that will include a date that needs to be
compared to the current time.

EX. a = Time.now
b = db_cert_date + (72 months)

if(a < b)
do something
elsif(a > b)
do something else
else
do this
end

I am looking for the best way to add 72 months onto an old date and
then compare it with the current date. Does anyone have any ideas?

Thanks in advance

If db_cert_date was a Date, you could do:

b = db_cert_date >> 72

-Rob

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

Jeremy Hinegardner

Rails's ActiveSupport package lets you say some_date + 72.months. It's that
simple; the secret unit of exchange is probably seconds.

If this were Brand X, using another platform's low-level library would be
eternal torment, but this is Ruby, so just try require 'active_support'!

Depending on how accurate you actually want, active_support's 72.months
assumes 30 days in a month. Here's a quick comparison of adding 72
months in different libraries:

% cat time-test.rb
#!/usr/bin/env ruby
require 'rubygems'
require 'active_support'
require 'chronic'

today_time = Time.now
today_dt = DateTime.now
format = "%Y-%m-%d %H:%M:%S"
puts "Calculate the date of 72 months from today : #{today_time.strftime(format)}"
puts "ActiveSupport (today (Time) + 72.months) : #{(today_time + 72.months).strftime(format)}"
puts "ActiveSupport (today (Time) + 6.years) : #{(today_time + 6.years).strftime(format)}"
puts "Chronic.parse '72 months from now' : #{Chronic.parse('72 months from now').strftime(format)}"
puts "DateTime.now >> 72 : #{(today_dt >> 72).strftime(format)}"

% ruby time-test.rb
Calculate the date of 72 months from today : 2007-09-10 11:32:14
ActiveSupport (today (Time) + 72.months) : 2013-08-09 11:32:14
ActiveSupport (today (Time) + 6.years) : 2013-09-09 23:32:14
Chronic.parse '72 months from now' : 2013-09-10 11:32:14
DateTime.now >> 72 : 2013-09-10 11:32:14

As you can see, there is some disparity. From my perspective, the only ones of
these that are actually correct are the Chronic and the DateTime ones.

Is there a really good date calculation library in ruby that I'm missing?

enjoy,

-jeremy
 
J

joep

If db_cert_date was a Date, you could do:

b = db_cert_date >> 72

-Rob

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

- Show quoted text -

Thanks Rob. This did work quite the way I wanted it to but I think
with a little playing around I can get it. Thanks for the direction.
 
M

Michael Hollins

joep said:
Only problem is I'm not using rails. Just straight ruby as a cgi.

Phlip's point was that you can use active_support without using rails:

gem install activesupport

cheers,
mick
 
T

Todd A. Jacobs

I am writing a small piece that will include a date that needs to be
compared to the current time.

If you require date, you can do something like:

if DateTime.now < (Date.parse(some_date_string.to_s) >> 72 )
puts 'Pay up, chump!'
end

There may be a more elegant way, but this would get the job done.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top