# Re: convert seconds to hours:minutes:seconds

**URL:** <https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241>\
**Category:** Ruby\
**Created:** [December 14, 2005, 12:11pm UTC](https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241 "2005-12-14T12:11:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 14, 2005, 12:11pm UTC](https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241/1 "2005-12-14T12:11:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 14, 2005, 11:20pm UTC](https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241/2 "2005-12-14T23:20:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 15, 2005, 12:24am UTC](https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241/3 "2005-12-15T00:24:09Z")

</div>

On Wednesday 14 December 2005 16:19, Andy D. 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.

> - Andy D.

[_snip_]

---

<div class="post-metadata">

**Author:** ![KroegerS\_Simon\_Sext](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/57b2e6/32.png) [@KroegerS\_Simon\_Sext](https://www.ruby-forum.com/u/KroegerS_Simon_Sext)\
**Post date:** [December 15, 2005, 1:48am UTC](https://www.ruby-forum.com/t/re-convert-seconds-to-hoursseconds/50241/4 "2005-12-15T01:48:32Z")

</div>

On Thu, 15 Dec 2005, Kevin B. wrote:

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

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
