[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

relative dates

charlie bowman

1/30/2006 12:16:00 AM

today = Time.now

how can I do this? "last_week = 7.days.ago"

I know the above won't work, but how can I get the date of last week?

--
Posted via http://www.ruby-....


13 Answers

Antonio Cangiano

1/30/2006 1:56:00 AM

0

charlie bowman wrote:
> today = Time.now
>
> how can I do this? "last_week = 7.days.ago"
>
> I know the above won't work, but how can I get the date of last week?
>

Hi Charlie,
you could define something like this:

class Integer
def weeks
self * 7.days
end

def days
self * 24.hours
end

def hours
self * 60.minutes
end

def minutes
self * 60
end

def ago(time = Time.now)
time - self
end
end

puts 7.days.ago #=> Mon Jan 23 01:50:09 GMT 2006
puts 1.weeks.ago #=> Mon Jan 23 01:50:09 GMT 2006

Of course you can alias days with day, weeks with week, add months,
years and so on. You can also add extra methods like Rails does (until,
since, from_now, etc...)

Cheers,
Antonio
--
Antonio Cangiano
My Ruby blog: http://www.antonioca...

Ezra Zygmuntowicz

1/30/2006 2:05:00 AM

0


On Jan 29, 2006, at 4:15 PM, charlie bowman wrote:

> today = Time.now
>
> how can I do this? "last_week = 7.days.ago"
>
> I know the above won't work, but how can I get the date of last week?
>
> --
> Posted via http://www.ruby-....
>

Charlie-

In rails actionpack supplies that method for you. Look here:

>> require 'rubygems'
=> false
>> require_gem 'actionpack'
=> true
>> 7.days.ago
=> Sun Jan 22 18:04:05 PST 2006
>>



Cheers-
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
http://yakima...
ezra@yakima-herald.com
blog: http://b...




Cameron McBride

1/30/2006 3:22:00 AM

0

> how can I do this? "last_week = 7.days.ago"

or subtract in seconds:
last_week = today - 7*24*60*60

Cameron


Alex LeDonne

1/30/2006 4:29:00 AM

0

On 1/29/06, Cameron McBride <cameron.mcbride@gmail.com> wrote:
> > how can I do this? "last_week = 7.days.ago"
>
> or subtract in seconds:
> last_week = today - 7*24*60*60
>
> Cameron
>

This is probably going to be easier with Date than with Time...

irb(main):001:0> require 'date'
=> true
irb(main):002:0> now = Date.today; now.strftime()
=> "2006-01-29"
irb(main):003:0> last_week = now - 7; last_week.strftime()
=> "2006-01-22"

If you are concerned about times, you can use DateTime in place of Date above.

-A


charlie bowman

1/30/2006 4:56:00 AM

0

Thanks, I had no idea that it was so simple in Ruby!


today = Time.now
puts (today - 7)


I thought I would have to write the method myself!






A LeDonne wrote:
> On 1/29/06, Cameron McBride <cameron.mcbride@gmail.com> wrote:
>> > how can I do this? "last_week = 7.days.ago"
>>
>> or subtract in seconds:
>> last_week = today - 7*24*60*60
>>
>> Cameron
>>
>
> This is probably going to be easier with Date than with Time...
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> now = Date.today; now.strftime()
> => "2006-01-29"
> irb(main):003:0> last_week = now - 7; last_week.strftime()
> => "2006-01-22"
>
> If you are concerned about times, you can use DateTime in place of Date
> above.
>
> -A


--
Posted via http://www.ruby-....


Alex LeDonne

1/30/2006 5:15:00 AM

0

On 1/29/06, charlie bowman <cbowmanschool@yahoo.com> wrote:
> Thanks, I had no idea that it was so simple in Ruby!
>
>
> today = Time.now
> puts (today - 7)

Careful!

If you're using Time, you're subtracting SECONDS:
irb(main):001:0> today = Time.now
=> Mon Jan 30 00:11:51 EST 2006
irb(main):002:0> puts( today - 7 )
Mon Jan 30 00:11:44 EST 2006
=> nil

That's why I included the " require 'date' " in my example, and
suggested that you could use DateTime if you needed times.

>
>
> I thought I would have to write the method myself!
>
>
>
>
>
>
> A LeDonne wrote:
> > On 1/29/06, Cameron McBride <cameron.mcbride@gmail.com> wrote:
> >> > how can I do this? "last_week = 7.days.ago"
> >>
> >> or subtract in seconds:
> >> last_week = today - 7*24*60*60
> >>
> >> Cameron
> >>
> >
> > This is probably going to be easier with Date than with Time...
> >
> > irb(main):001:0> require 'date'
> > => true
> > irb(main):002:0> now = Date.today; now.strftime()
> > => "2006-01-29"
> > irb(main):003:0> last_week = now - 7; last_week.strftime()
> > => "2006-01-22"
> >
> > If you are concerned about times, you can use DateTime in place of Date
> > above.
> >
> > -A


Nathaniel S. H. Brown

1/30/2006 5:32:00 AM

0

You might want to check out RUNT:

http://runt.ruby...

DateBox might also be worth a gander:

http://datebox....

-Nb

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nathaniel S. H. Brown http:...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


> -----Original Message-----
> From: A LeDonne [mailto:aledonne.listmail@gmail.com]
> Sent: January 29, 2006 9:15 PM
> To: ruby-talk ML; cbowmanschool@yahoo.com
> Subject: Re: relative dates
>
> On 1/29/06, charlie bowman <cbowmanschool@yahoo.com> wrote:
> > Thanks, I had no idea that it was so simple in Ruby!
> >
> >
> > today = Time.now
> > puts (today - 7)
>
> Careful!
>
> If you're using Time, you're subtracting SECONDS:
> irb(main):001:0> today = Time.now
> => Mon Jan 30 00:11:51 EST 2006
> irb(main):002:0> puts( today - 7 )
> Mon Jan 30 00:11:44 EST 2006
> => nil
>
> That's why I included the " require 'date' " in my example,
> and suggested that you could use DateTime if you needed times.
>
> >
> >
> > I thought I would have to write the method myself!
> >
> >
> >
> >
> >
> >
> > A LeDonne wrote:
> > > On 1/29/06, Cameron McBride <cameron.mcbride@gmail.com> wrote:
> > >> > how can I do this? "last_week = 7.days.ago"
> > >>
> > >> or subtract in seconds:
> > >> last_week = today - 7*24*60*60
> > >>
> > >> Cameron
> > >>
> > >
> > > This is probably going to be easier with Date than with Time...
> > >
> > > irb(main):001:0> require 'date'
> > > => true
> > > irb(main):002:0> now = Date.today; now.strftime() => "2006-01-29"
> > > irb(main):003:0> last_week = now - 7; last_week.strftime() =>
> > > "2006-01-22"
> > >
> > > If you are concerned about times, you can use DateTime in
> place of
> > > Date above.
> > >
> > > -A
>



Ezra Zygmuntowicz

1/30/2006 7:00:00 AM

0


On Jan 29, 2006, at 8:28 PM, A LeDonne wrote:

> On 1/29/06, Cameron McBride <cameron.mcbride@gmail.com> wrote:
>>> how can I do this? "last_week = 7.days.ago"

In rails you are already done:

>> require_gem 'actionpack'
=> true
>> last_week = 7.days.ago
=> Sun Jan 22 22:59:26 PST 2006
>> p last_week
Sun Jan 22 22:59:26 PST 2006

-Ezra



>>
>> or subtract in seconds:
>> last_week = today - 7*24*60*60
>>
>> Cameron
>>
>
> This is probably going to be easier with Date than with Time...
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> now = Date.today; now.strftime()
> => "2006-01-29"
> irb(main):003:0> last_week = now - 7; last_week.strftime()
> => "2006-01-22"
>
> If you are concerned about times, you can use DateTime in place of
> Date above.
>
> -A
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
http://yakima...
ezra@yakima-herald.com
blog: http://b...




charlie bowman

1/30/2006 1:27:00 PM

0

Your're right! as soon as I implemented it I noticed the seconds change.
I just came up with the number of seconds in a day and used that. I
would be nice if it were as simple in ruby as it is in rails, but I know
speed is more important and ease of use.....usually.

A LeDonne wrote:
> On 1/29/06, charlie bowman <cbowmanschool@yahoo.com> wrote:
>> Thanks, I had no idea that it was so simple in Ruby!
>>
>>
>> today = Time.now
>> puts (today - 7)
>
> Careful!
>
> If you're using Time, you're subtracting SECONDS:
> irb(main):001:0> today = Time.now
> => Mon Jan 30 00:11:51 EST 2006
> irb(main):002:0> puts( today - 7 )
> Mon Jan 30 00:11:44 EST 2006
> => nil
>
> That's why I included the " require 'date' " in my example, and
> suggested that you could use DateTime if you needed times.


--
Posted via http://www.ruby-....


netghost

1/30/2006 5:47:00 PM

0

Depending on if you need the time component, it may just be eaiser to
work with the Date object.

d = Date.today
lw = d - 7

'lw' will be 7 days ago. In this case adding and subtracting seconds
will be fine, but be careful about it, days aren't always 24 hours long
(think about daylight savings time), and it can really catch up with
you sometimes.

.adam