Convert seconds to hours:minutes:seconds

Everytime you use printf, God kills a kitten.

time = 7683
p [time/3600, time/60 % 60, time % 60].map{|t| t.to_s.rjust(2,
'0')}.join(':')

=> "02:08:03"

cheers

Simon

···

-----Original Message-----
From: Andy Delcambre [mailto:adelcambre@gmail.com]
Sent: Wednesday, December 14, 2005 11:21 AM
To: ruby-talk ML
Subject: Re: convert seconds to hours:minutes:seconds

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

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
>
> is there an easy way to accomplish this?
>
>

What is wrong with printf, I learned C first and most recently have
done a lot of programming with python which includes printf style
variable interpolation. It is my favorite way of formatting output as
it is what I know. Does god kill a kitten out of personal preference
or some deep seated problem with printf in ruby?

- Andy Delcambre

···

On 12/14/05, Kroeger, Simon (ext) <simon.kroeger.ext@siemens.com> wrote:

Everytime you use printf, God kills a kitten.

time = 7683
p [time/3600, time/60 % 60, time % 60].map{|t| t.to_s.rjust(2,
'0')}.join(':')

=> "02:08:03"

cheers

Simon

> -----Original Message-----
> From: Andy Delcambre [mailto:adelcambre@gmail.com]
> Sent: Wednesday, December 14, 2005 11:21 AM
> To: ruby-talk ML
> Subject: Re: convert seconds to hours:minutes:seconds
>
> 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
>
> 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
> >
> > is there an easy way to accomplish this?
> >
> >
>
>

What is wrong with printf, I learned C first and most recently have
done a lot of programming with python which includes printf style
variable interpolation. It is my favorite way of formatting output as
it is what I know. Does god kill a kitten out of personal preference
or some deep seated problem with printf in ruby?

C/C++ programmers are comfortable with printf. If someone overloaded the %
operator and started using that in C++, you'd get annoyed. It's not
something that's a widely used portion of the language. So if anyone else
ever has to read your code, you're asking them to approach it from the C
point of view instead of the Ruby point of view. Being that the common
denominator is that they'll know Ruby, this is inconsiderate.

Can you do it? Sure. But Ruby programmers, as a rule, do not think about
string processing this way. At least save yourself some keystrokes and use
the exact same format string and % if you must.

- Andy Delcambre

[*snip*]

···

On Wednesday 14 December 2005 16:19, Andy Delcambre wrote:

sure. but here you have

   harp:~ > irb
   irb(main):001:0> Time::now.strftime '%H:%M:%S'
   => "17:44:28"

in case op didn't know...

regards.

-a

···

On Thu, 15 Dec 2005, Kevin Brown wrote:

On Wednesday 14 December 2005 16:19, Andy Delcambre wrote:

What is wrong with printf, I learned C first and most recently have
done a lot of programming with python which includes printf style
variable interpolation. It is my favorite way of formatting output as
it is what I know. Does god kill a kitten out of personal preference
or some deep seated problem with printf in ruby?

C/C++ programmers are comfortable with printf. If someone overloaded the %
operator and started using that in C++, you'd get annoyed. It's not
something that's a widely used portion of the language. So if anyone else
ever has to read your code, you're asking them to approach it from the C
point of view instead of the Ruby point of view. Being that the common
denominator is that they'll know Ruby, this is inconsiderate.

Can you do it? Sure. But Ruby programmers, as a rule, do not think about
string processing this way. At least save yourself some keystrokes and use
the exact same format string and % if you must.

--

ara [dot] t [dot] howard [at] noaa [dot] gov
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================