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.
A6bf3348cbf9f24149c1b6d57cc9b9c9?d=identicon&s=25 mj-usunto (Guest)
on 2005-11-17 16: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
?
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 khaines (Guest)
on 2005-11-17 17:02
(Received via mailing list)
On Thursday 17 November 2005 8:57 am, Marcin Jurczuk 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 Haines
A6bf3348cbf9f24149c1b6d57cc9b9c9?d=identicon&s=25 mj-usunto (Guest)
on 2005-11-17 17:15
(Received via mailing list)
Kirk Haines wrote:

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

Tha
780500a357d1f693a33c89e072a925aa?d=identicon&s=25 greg7224 (Guest)
on 2005-11-17 17: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
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 lopexx (Guest)
on 2005-11-17 17: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
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 pertl (Guest)
on 2005-11-17 17:42
(Received via mailing list)
even shorter:

 def Time.yesterday; now - 86400; end
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-17 17: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 :)
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-17 18:09
(Received via mailing list)
Peter Ertl wrote:

>  def Time.yesterday; now - 86400; end

Haha very nice.
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 gene.tani (Guest)
on 2005-11-17 19:58
(Received via mailing list)
It's in Active Support:

http://as.rubyonrails.com/
B3a768bfd90e22e4fb091626019d351b?d=identicon&s=25 balcersk (Guest)
on 2005-11-17 20:13
(Received via mailing list)
Marcin MielżyÅ?ski napisaÅ?(a):
>>
>> Tha
>
>
> proste :D
>
> lopex
like a stick :)
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Guest)
on 2005-11-17 20:13
(Received via mailing list)
On Nov 17, 2005, at 10:57 AM, Gene Tani 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)
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 gwtmp01 (Guest)
on 2005-11-17 23:42
(Received via mailing list)
On Nov 17, 2005, at 2:13 PM, Eric Hodel 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 Wright
577053972bc30f59cf29714765a8b49f?d=identicon&s=25 michael.campbell (Guest)
on 2005-11-18 00:18
(Received via mailing list)
Marcin Jurczuk <mj-usunto@tkb.pl> 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.
33eda546f35b0667856505f21f940529?d=identicon&s=25 reinder (Guest)
on 2005-11-18 01:10
(Received via mailing list)
In article <ufypux4u2.fsf@atlmicampbell.checkfree.com>,
 Michael Campbell <michael.campbell@gmail.com> 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
577053972bc30f59cf29714765a8b49f?d=identicon&s=25 michael.campbell (Guest)
on 2005-11-18 17:04
(Received via mailing list)
Reinder Verlinde <reinder@verlinde.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. =)
65bd9e4c5aebde25ebf16d599339d570?d=identicon&s=25 netghost (Guest)
on 2005-11-18 17: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.