joep
September 10, 2007, 6:27pm
#1
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
joep
September 10, 2007, 6:36pm
#2
joep wrote:
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’!
joep
September 10, 2007, 6:42pm
#3
On Sep 10, 10:03 am, Phlip [email protected] wrote:
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’!
–
Phlip
Only problem is I’m not using rails. Just straight ruby as a cgi.
joep
September 10, 2007, 6:47pm
#4
On 9/10/07, joep [email protected] wrote:
do something else
How 'bout DateTime?
The DateTime#>> operator means “return a new Date object that is n
months
later than the current one.”
For your code to work db_cert_date would have to be a DateTime, too.
irb(main):018:0> a = DateTime.now
=> #<DateTime: 35342700409548349/14400000000,-1/6,2299161>
irb(main):019:0> puts a.strftime(’%m/%d/%Y’)
09/10/2007
=> nil
irb(main):020:0> b = a >> 72
=> #<DateTime: 35374265209548349/14400000000,-1/6,2299161>
irb(main):021:0> puts b.strftime(’%m/%d/%Y’)
09/10/2013
=> nil
joep
September 10, 2007, 6:52pm
#5
On 9/10/07, joep [email protected] wrote:
that
You don’t have to be using rails you just need the active_support gem
installed.
gem install activesupport
After that just require ‘active_support’ as Phlip said.
–
“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”
-Greg Graffin (Bad Religion)
joep
September 10, 2007, 7:38pm
#6
On Tue, Sep 11, 2007 at 01:35:08AM +0900, Phlip wrote:
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
joep
September 10, 2007, 7:56pm
#7
On Sep 10, 12:59 pm, Rob B. [email protected]
wrote:
b = db_cert_date + (72 months)
then compare it with the current date. Does anyone have any ideas?
[email protected] Hide 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.
joep
September 10, 2007, 7:00pm
#8
On Sep 10, 2007, at 12:25 PM, joep wrote:
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 B. http://agileconsultingllc.com
[email protected]
joep
September 11, 2007, 3:06am
#9
joep wrote:
eternal torment, but this is Ruby, so just try require ‘active_support’!
–
Phlip
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
joep
September 11, 2007, 7:46am
#10
On Tue, Sep 11, 2007 at 01:25:04AM +0900, joep wrote:
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.