Time.yesterday ? :)

M

Marcin Jurczuk

Hello group.

Is there any simple method to get Time object like Time.now but with
yesterday date, without worrying about end of month or begining new one ?
 
K

Kirk Haines

Hello group.

Is there any simple method to get Time object like Time.now but with
yesterday date, without worrying about end of month or begining new one ?

Time.now - 86400


Kirk Haines
 
G

Greg Brown

greg@oracle ~ $ irb
irb(main):001:0> Time.now
=> Thu Nov 17 11:38:02 EST 2005
irb(main):002:0> class Time
irb(main):003:1> def self.yesterday
irb(main):004:2> now - 86400
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> Time.yesterday
=> Wed Nov 16 11:38:27 EST 2005
 
M

Marcin Mielżyński

class Time
def Time.yesterday
t=Time.now
Time.at(t.to_i-86400)
end
end

p Time.yesterday
I knew that is something simple - but not so simple :)

Tha

proste :D

lopex
 
G

Gregory Brown

You don't need the intermediate variable

class Time
def self.yesterday
now - 86400
end
end

Time.yesterday works just fine :)
 
J

Jacek Balcerski

Marcin Mielżyński napisał(a):
class Time
def Time.yesterday
t=Time.now
Time.at(t.to_i-86400)
end
end

p Time.yesterday



proste :D

lopex
like a stick :)
 
E

Eric Hodel


No, Time.yesterday is not in active_support, Time#yesterday is.

Also, active_support emits far, far, far too many warnings. I could
*maybe* deal with 1 or two warnings, but not 169:

$ cat yesterday.rb
#!/usr/local/bin/ruby -w

require 'rubygems'
require 'active_support'

p Time.yesterday

$ ruby yesterday.rb
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/
active_support/class_inheritable_attributes.rb:116: warning:
discarding old inherited
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/
active_support/inflections.rb:2: warning: ambiguous first argument;
put parentheses or even spaces
[snip 167 lines of warnings]
yesterday.rb:6: undefined method `yesterday' for Time:Class
(NoMethodError)
 
G

gwtmp01

No, Time.yesterday is not in active_support, Time#yesterday is.

Also, active_support emits far, far, far too many warnings. I
could *maybe* deal with 1 or two warnings, but not 169:

Ditto. Whenever I see that sort of thing, I start to wonder
what I'm getting myself into.


Gary Wright
 
M

Michael Campbell

Marcin Jurczuk said:
Hello group.

Is there any simple method to get Time object like Time.now but with
yesterday date, without worrying about end of month or begining new one ?

Pedantic to be sure, but keep in mind all the variations on the
"... - 86400" scheme universally fail during the Daylight Savings
Time cutovers that some of us are unfortunate to have to suffer.

Probably this won't affect you.
 
R

Reinder Verlinde

Michael Campbell said:
Pedantic to be sure, but keep in mind all the variations on the
"... - 86400" scheme universally fail during the Daylight Savings
Time cutovers that some of us are unfortunate to have to suffer.

Probably this won't affect you.

Even more pedantic: even if it does not, the assumption that every day
has 86400 seconds is still incorrect. A day with a leap second has 86401
or 86399 (has not happened yet) seconds.

Reinder
 
M

Michael Campbell

Reinder Verlinde said:
Even more pedantic: even if it does not, the assumption that every
day has 86400 seconds is still incorrect. A day with a leap second
has 86401 or 86399 (has not happened yet) seconds.

*Chuckle* I completely forgot about that too; good catch. =)
 
A

Adam Sanderson

Well... if a time wasn't essential, and a Date could be used instead:
require 'date'
Date.today - 1

By the way, for anyone playing with date/time functions, date also will
increment/decrement months:
Date.today >> 1 # Adds a month
Date.today << 1 # Subtracts a month

Might be handy,
.adam
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top