tobyclemson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-31 14:44 UTC
Time from seconds.
Hi I was just wondering if anyone can think of a simpler way of doing the following: def number_to_time(seconds) seconds = seconds.to_i if seconds > 1.hours hours = seconds / 1.hour hours = "0#{hours}" if hours < 10 seconds = seconds % 1.hour else hours = "00" end if seconds > 1.minute minutes = seconds / 1.minute minutes = "0#{minutes}" if minutes < 10 seconds = (seconds % 1.minute).to_s.sub(''.0'', '''') else minutes = "00" end return "#{hours}:#{minutes}:#{seconds}" rescue nil end Thanks, Toby --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Something like: def number_to_time(seconds) Time.at(seconds.to_i-3600).strftime("%H:%M:%S") end Piet.> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of > tobyclemson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > Sent: dinsdag 31 juli 2007 16:44 > To: Ruby on Rails: Talk > Subject: [Rails] Time from seconds. > > > Hi > > I was just wondering if anyone can think of a simpler way of doing the > following: > > def number_to_time(seconds) > seconds = seconds.to_i > if seconds > 1.hours > hours = seconds / 1.hour > hours = "0#{hours}" if hours < 10 > seconds = seconds % 1.hour > else > hours = "00" > end > if seconds > 1.minute > minutes = seconds / 1.minute > minutes = "0#{minutes}" if minutes < 10 > seconds = (seconds % 1.minute).to_s.sub(''.0'', '''') > else > minutes = "00" > end > return "#{hours}:#{minutes}:#{seconds}" > rescue > nil > end > > Thanks, > Toby > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tobyclemson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-31 15:29 UTC
Re: Time from seconds.
Well it would be really nice and concise if that worked but unfortunately it doesn''t work for seconds > 1.day i.e., it will show 00:05:55 rather than 24:05:55. I would like a %H:%M:%S type output but with %H representing the number of hours which can be greater than 24. On Jul 31, 4:07 pm, "Piet Hadermann" <piet.haderm...-/1BeJOKqmzrQT0dZR+AlfA@public.gmane.org> wrote:> Something like: > > def number_to_time(seconds) > Time.at(seconds.to_i-3600).strftime("%H:%M:%S") > end > > Piet. > > > -----Original Message----- > > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of > > tobyclem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > Sent: dinsdag 31 juli 2007 16:44 > > To: Ruby on Rails: Talk > > Subject: [Rails] Time from seconds. > > > Hi > > > I was just wondering if anyone can think of a simpler way of doing the > > following: > > > def number_to_time(seconds) > > seconds = seconds.to_i > > if seconds > 1.hours > > hours = seconds / 1.hour > > hours = "0#{hours}" if hours < 10 > > seconds = seconds % 1.hour > > else > > hours = "00" > > end > > if seconds > 1.minute > > minutes = seconds / 1.minute > > minutes = "0#{minutes}" if minutes < 10 > > seconds = (seconds % 1.minute).to_s.sub(''.0'', '''') > > else > > minutes = "00" > > end > > return "#{hours}:#{minutes}:#{seconds}" > > rescue > > nil > > end > > > Thanks, > > Toby--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 31 Jul 2007, at 16:44, tobyclemson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> I was just wondering if anyone can think of a simpler way of doing the > following: > > def number_to_time(seconds) > seconds = seconds.to_i > if seconds > 1.hours > hours = seconds / 1.hour > hours = "0#{hours}" if hours < 10 > seconds = seconds % 1.hour > else > hours = "00" > end > if seconds > 1.minute > minutes = seconds / 1.minute > minutes = "0#{minutes}" if minutes < 10 > seconds = (seconds % 1.minute).to_s.sub(''.0'', '''') > else > minutes = "00" > end > return "#{hours}:#{minutes}:#{seconds}" > rescue > nil > endHow about def number_to_time(seconds) [seconds/3600, seconds/60 % 60, seconds % 60].map{|t| t.to_s.rjust (2,''0'')}.join('':'') end Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tobyclemson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi > > I was just wondering if anyone can think of a simpler way of doing the > following: > > def number_to_time(seconds) > seconds = seconds.to_i > if seconds > 1.hours > hours = seconds / 1.hour > hours = "0#{hours}" if hours < 10 > seconds = seconds % 1.hour > else > hours = "00" > end > if seconds > 1.minute > minutes = seconds / 1.minute > minutes = "0#{minutes}" if minutes < 10 > seconds = (seconds % 1.minute).to_s.sub(''.0'', '''') > else > minutes = "00" > end > return "#{hours}:#{minutes}:#{seconds}" > rescue > nil > endHow about: Time.at(seconds).utc.strftime("%H:%M:%S") -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---