add 10 minutes to parsed time stamp?

M

Mmcolli00 Mom

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?
 
R

Robert Klemme

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
 
R

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jarmo Pertman

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

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jarmo Pertman

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
 
B

Bira

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

Mark Thomas

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

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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.

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

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,989
Messages
2,570,207
Members
46,785
Latest member
undedgini

Latest Threads

Top