Forum: Ruby Time.yesterday ? :)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mj-usunto (Guest)
on 2005-11-17 17:59
(Received via mailing list)
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
?
khaines (Guest)
on 2005-11-17 18:02
(Received via mailing list)
On Thursday 17 November 2005 8:57 am, Marcin J. wrote:
> 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 H.
mj-usunto (Guest)
on 2005-11-17 18:15
(Received via mailing list)
Kirk H. wrote:

> On Thursday 17 November 2005 8:57 am, Marcin J. wrote:
>> 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 H.
:P
I knew that is something simple - but not so simple :)

Tha
greg7224 (Guest)
on 2005-11-17 18:39
(Received via mailing list)
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
lopexx (Guest)
on 2005-11-17 18:39
(Received via mailing list)
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
pertl (Guest)
on 2005-11-17 18:42
(Received via mailing list)
even shorter:

 def Time.yesterday; now - 86400; end
gregory.t.brown (Guest)
on 2005-11-17 18:54
(Received via mailing list)
You don't need the intermediate variable

class Time
  def self.yesterday
     now - 86400
  end
end

Time.yesterday works just fine :)
gregory.t.brown (Guest)
on 2005-11-17 19:09
(Received via mailing list)
Peter E. wrote:

>  def Time.yesterday; now - 86400; end

Haha very nice.
gene.tani (Guest)
on 2005-11-17 20:58
(Received via mailing list)
It's in Active Support:

http://as.rubyonrails.com/
balcersk (Guest)
on 2005-11-17 21:13
(Received via mailing list)
Marcin MielżyÅ?ski napisaÅ?(a):
>>
>> Tha
>
>
> proste :D
>
> lopex
like a stick :)
drbrain (Guest)
on 2005-11-17 21:13
(Received via mailing list)
On Nov 17, 2005, at 10:57 AM, Gene T. wrote:

>
> It's in Active Support:
>
> http://as.rubyonrails.com/

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)
gwtmp01 (Guest)
on 2005-11-18 00:42
(Received via mailing list)
On Nov 17, 2005, at 2:13 PM, Eric H. wrote:
> 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 W.
michael.campbell (Guest)
on 2005-11-18 01:18
(Received via mailing list)
Marcin J. <removed_email_address@domain.invalid> writes:

> 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.
reinder (Guest)
on 2005-11-18 02:10
(Received via mailing list)
In article <removed_email_address@domain.invalid>,
 Michael C. <removed_email_address@domain.invalid> wrote:

>
> 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
michael.campbell (Guest)
on 2005-11-18 18:04
(Received via mailing list)
Reinder V. <removed_email_address@domain.invalid> writes:

> > 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.

*Chuckle* I completely forgot about that too; good catch. =)
netghost (Guest)
on 2005-11-18 18:28
(Received via mailing list)
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
This topic is locked and can not be replied to.