Add 10 minutes to parsed time stamp?


#1

How can I add 10 minutes to this time stamp? Thanks. MC

ServerTime = ‘Wed Dec 09 16:05:00 -0600 2009’

ServerTime + Time.parse(10)

#<-it does not work without parse since I get error can’t convert Fixnum
into a string. what do you think?


#2

On 06.01.2009 16:09, Mmcolli00 Mom wrote:

How can I add 10 minutes to this time stamp? Thanks. MC

ServerTime = ‘Wed Dec 09 16:05:00 -0600 2009’

Here ServerTime is a String and hence cannot do any time related
calculations. You probably rather want

require ‘date’

irb(main):017:0> server_time = DateTime.parse ‘Wed Dec 09 16:05:00 -0600
2009’
=> #<DateTime: 707090521/288,-1/4,2299161>
irb(main):018:0> server_time.to_s
=> “2009-12-09T16:05:00-06:00”

Now you can add 10 minutes:

irb(main):019:0> (server_time + 10.0 / 24 / 60).to_s
=> “2009-12-09T16:15:00-06:00”

ServerTime + Time.parse(10)

#<-it does not work without parse since I get error can’t convert Fixnum
into a string. what do you think?

Well, what do you think? What does the error message tell you? What
do you conclude from that?

Cheers

robert


#3

On Jan 6, 2009, at 10:09 AM, Mmcolli00 Mom wrote:

How can I add 10 minutes to this time stamp? Thanks. MC

ServerTime = ‘Wed Dec 09 16:05:00 -0600 2009’

First of all, Ruby treats names beginning with an uppercase letter as
Constants. If you really want a variable, use server_time.

ServerTime + Time.parse(10)

#<-it does not work without parse since I get error can’t convert
Fixnum
into a string. what do you think?

irb> require ‘time’
=> true
irb> server_time_as_string = ‘Wed Dec 09 16:05:00 -0600 2009’
=> “Wed Dec 09 16:05:00 -0600 2009”
irb> server_time_as_time = Time.parse(server_time_as_string)
=> Wed Dec 09 17:05:00 -0500 2009

Note that this is just my timezone for the output.

irb> server_time_as_time + 10*60
=> Wed Dec 09 17:15:00 -0500 2009

Time + Fixnum adds seconds so 10 minutes is 10*60 seconds.

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#4

Or even better way using ActiveSupport:

irb(main):001:0> require ‘active_support’
=> true
irb(main):002:0> t = Time.parse(‘Wed Dec 09 16:05:00 -0600 2009’)
=> Thu Dec 10 00:05:00 +0200 2009
irb(main):003:0> t + 10.minutes
=> Thu Dec 10 00:15:00 +0200 2009
irb(main):004:0>


#5

Of course if s/he doesn’t want it then s/he won’t use it. I didn’t try
to force anyone to use anything or to cause any other harm, I were just
showing some more and fancy possibilities.

And you are correct, ActiveSupport indeed requires time automatically.

Also, I have to admit that I didn’t know that “Ruby list” means that
it’s denied to talk about anything more than Ruby core and stdlibs.

Best regards,
Jarmo

Rob B. wrote:

Since the OP thoughtfully asks this question in the Ruby list and not
the Rails list, s/he may not have/want ActiveSupport. If this is the
only thing, pulling ActiveSupport in is certainly overkill.

Also, note that Time.parse isn’t available unless you require
‘time’ (which ActiveSupport might do itself).

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#6

On Jan 8, 2009, at 11:54 AM, Jarmo P. wrote:

Or even better way using ActiveSupport:

irb(main):001:0> require ‘active_support’
=> true
irb(main):002:0> t = Time.parse(‘Wed Dec 09 16:05:00 -0600 2009’)
=> Thu Dec 10 00:05:00 +0200 2009
irb(main):003:0> t + 10.minutes
=> Thu Dec 10 00:15:00 +0200 2009
irb(main):004:0>

Since the OP thoughtfully asks this question in the Ruby list and not
the Rails list, s/he may not have/want ActiveSupport. If this is the
only thing, pulling ActiveSupport in is certainly overkill.

Also, note that Time.parse isn’t available unless you require
‘time’ (which ActiveSupport might do itself).

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#7

On Thu, Jan 8, 2009 at 3:18 PM, Jarmo P. removed_email_address@domain.invalid wrote:

Also, I have to admit that I didn’t know that “Ruby list” means that
it’s denied to talk about anything more than Ruby core and stdlibs.

You’re not forbidden to talk about other libs. It’s just that there
are separate lists for Ruby and Rails, and the general understanding
is that the questions posted here aren’s Rails-specific.

So, the people who posted these questions probably aren’t using Rails
libraries, and thus answers who recommend using them are less useful.
Rob was probably just pointing that out. No offense intended.


#8

On Fri, Feb 6, 2009 at 7:54 AM, Mark T. removed_email_address@domain.invalid wrote:

use ActiveRecord outside of Rails, and ActiveSupport is one of its
dependencies.

I’ve done the same myself, but…

Activesupport is a rather large wad to swallow if you only need a little
of
it’s function. I’m currently working on a new icalendar library for
ruby
http://talklikeaduck.denhaven2.com/articles/2009/01/23/what-ive-been-up-to-of-late-a-new-ruby-icalendar-library

I was using activesupport jut to do some of the date calculations, but
after
debugging all of the myriad examples in rfc2445, I spent the last day
refactoring to get rid of the dependency, since there is enough
resistance
in the overall ruby community to activerecord dependencies.

At the same time, I’m writing the code to interact with the fairly new
time
zone support in activesupport, since I suspect that much of the ultimate
usage for the gem will be for rails apps, and activesupport now has a
fairly
nice api for dealing with times with time zones.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale


#9

You’re not forbidden to talk about other libs. It’s just that there
are separate lists for Ruby and Rails, and the general understanding
is that the questions posted here aren’s Rails-specific.

So, the people who posted these questions probably aren’t using Rails
libraries, and thus answers who recommend using them are less useful.
Rob was probably just pointing that out. No offense intended.

It can be argued that ActiveSupport is not Rails. For example, I often
use ActiveRecord outside of Rails, and ActiveSupport is one of its
dependencies.

– Mark.