Forum: Ruby convert seconds to hours:minutes:seconds

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.
4542033c61ec4b800c778fadb6c728c0?d=identicon&s=25 `p (Guest)
on 2005-12-14 10:35
(Received via mailing list)
hello!
i am trying to convert a number of seconds to a nicely formatted string
like this:

7683 seconds = 02:08:03

is there an easy way to accomplish this?
B464ff27d7d3b0750a7bfe2717c49d2d?d=identicon&s=25 Chris Pine (Guest)
on 2005-12-14 11:14
(Received via mailing list)
On 12/14/05, `p <a@o.e> wrote:
> hello!
> i am trying to convert a number of seconds to a nicely formatted string
> like this:
>
> 7683 seconds = 02:08:03

Well, assuming you know how to arithmetic, conversions, and string
concatenation (and if not, check out http://pine.fm/LearnToProgram/),
all you're missing is how to pad with zeroes.

I do talk about the rjust method, but not that you can use it to pad
with things other than spaces:

  2.to_s.rjust(5,'0')

This pads the string '2' (from 2.to_s) with the string '0'.

Another, fancier (or just more complicated?  :) way:

  irb(main):001:0> '%.5d' % 2
  => "00002"

Hope that helps,

Chris
C6858f4b8ec263c8c3c58759494e926a?d=identicon&s=25 Andy Delcambre (Guest)
on 2005-12-14 11:23
(Received via mailing list)
As long as you dont go past 24 hours you can use the Time class:

irb(main):016:0> Time.at(7683).gmtime.strftime('%R:%S')
=> "02:08:03"

If you dont use gmtime, it will go based off your localtime, where the
epoch = the offset from GMT.

also by doing the arithmatic:
irb(main):023:0> time = 7683
=> 7683
irb(main):024:0> hours = time/3600.to_i
=> 2
irb(main):025:0> minutes = (time/60 - hours * 60).to_i
=> 8
irb(main):026:0> seconds = (time - (minutes * 60 + hours * 3600))
=> 3
irb(main):030:0> printf("%02d:%02d:%02d\n", hours, minutes, seconds)
02:08:03
=> nil

HTH
 - Andy Delcambre
4542033c61ec4b800c778fadb6c728c0?d=identicon&s=25 `p (Guest)
on 2005-12-14 12:35
(Received via mailing list)
Andy Delcambre wrote:
> As long as you dont go past 24 hours you can use the Time class:
>
> irb(main):016:0> Time.at(7683).gmtime.strftime('%R:%S')
> => "02:08:03"

hey thanks! that's what i was looking for.
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2005-12-14 13:32
(Received via mailing list)
On Wednesday 14 December 2005 05:21 am, Andy Delcambre wrote:
> As long as you dont go past 24 hours you can use the Time class:
>
> irb(main):016:0> Time.at(7683).gmtime.strftime('%R:%S')
> => "02:08:03"

Next question: How do I get the number of seconds since Epoch?

SteveT

Steve Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
4542033c61ec4b800c778fadb6c728c0?d=identicon&s=25 `p (Guest)
on 2005-12-14 14:33
(Received via mailing list)
Steve Litt wrote:
> On Wednesday 14 December 2005 05:21 am, Andy Delcambre wrote:
>
>>As long as you dont go past 24 hours you can use the Time class:
>>
>>irb(main):016:0> Time.at(7683).gmtime.strftime('%R:%S')
>>=> "02:08:03"
>
>
> Next question: How do I get the number of seconds since Epoch?

time.to_i
http://www.ruby-doc.org/core/classes/Time.html#M000196
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2005-12-14 15:12
(Received via mailing list)
On Wednesday 14 December 2005 08:32 am, `p wrote:
> time.to_i
> http://www.ruby-doc.org/core/classes/Time.html#M000196

Cool! Thanks!

#!/usr/bin/ruby
b4 = Time.new
sleep(3)
after = Time.new
interval = after.to_i - b4.to_i
print "Started at ", b4.asctime, ", ended at ", after.asctime
print "\n   Interval is ", interval.to_s, ".\n"

SteveT

Steve Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-14 15:24
(Received via mailing list)
On Dec 14, 2005, at 8:09 AM, Steve Litt wrote:

> #!/usr/bin/ruby
> b4 = Time.new
> sleep(3)
> after = Time.new
> interval = after.to_i - b4.to_i

You can just subtract normally.  Time knows what to do:

   interval = after - b4

> print "Started at ", b4.asctime, ", ended at ", after.asctime
> print "\n   Interval is ", interval.to_s, ".\n"

It's a good idea to get into the habit of using interpolation with
Ruby.  That let's Ruby take care of stringifying your values.  We can
also lose those \n characters:

   puts "Started at #{b4} and ended at #{after}."
   puts "Interval is #{interval}."

James Edward Gray II
1b62a85b59ccab03b84ee5ec378f75b4?d=identicon&s=25 Steve Litt (Guest)
on 2005-12-14 16:33
(Received via mailing list)
On Wednesday 14 December 2005 09:21 am, James Edward Gray II wrote:
>
> > print "Started at ", b4.asctime, ", ended at ", after.asctime
> > print "\n   Interval is ", interval.to_s, ".\n"
>
> It's a good idea to get into the habit of using interpolation
> with Ruby.  That let's Ruby take care of stringifying your
> values.  We can also lose those \n characters:
>
>    puts "Started at #{b4} and ended at #{after}."
>    puts "Interval is #{interval}."

Confirmed! That's much cleaner. I've been looking for something like
that, and didn't want to kill a kitten by using printf() :-)

Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
04d072ab8843cfd3d1714faf3a2a0fb2?d=identicon&s=25 mathew (Guest)
on 2005-12-16 23:00
(Received via mailing list)
James Edward Gray II wrote:
> It's a good idea to get into the habit of using interpolation with
> Ruby.  That let's Ruby take care of stringifying your values.  We can
> also lose those \n characters:
>
>   puts "Started at #{b4} and ended at #{after}."
>   puts "Interval is #{interval}."

It's also faster. Or at least, it was on my system when I ran some
benchmarks.


mathew
This topic is locked and can not be replied to.