Numeric to time/period format (Ex: 1.to_s(:time, format: '%H:%M:%S'))

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Numeric to time/period format (Ex: 1.to_s(:time, format: '%H:%M:%S'))

Wilson de Almeida
Hello Rails Community!

Every time that I need to calculate a period of time and need to display it in a custom format I use a converter that works like this code below.

31104002.to_s(:time, format: '%y-%m-%d %H:%M:%S')
> '01-00-00 00:00:02'

3602.to_s(:time, format: '%d %H:%M:%S')
> '01 00:00:02'

3602.to_s(:time, format: '%d days, %H hours, %M minutes and %S seconds')
> '01 days, 00 hours, 00 minutes and 02 seconds'

Is it helpful? Interesting?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Numeric to time/period format (Ex: 1.to_s(:time, format: '%H:%M:%S'))

Prem Sichanugrist
You can currently do that using a (probably not easy to find name) #strftime.


I don't think there's a need to clone that method and make it as part of #to_s though.

-Prem

On Thu, Jun 28, 2018 at 1:51 AM Wilson de Almeida <[hidden email]> wrote:
Hello Rails Community!

Every time that I need to calculate a period of time and need to display it in a custom format I use a converter that works like this code below.

31104002.to_s(:time, format: '%y-%m-%d %H:%M:%S')
> '01-00-00 00:00:02'

3602.to_s(:time, format: '%d %H:%M:%S')
> '01 00:00:02'

3602.to_s(:time, format: '%d days, %H hours, %M minutes and %S seconds')
> '01 days, 00 hours, 00 minutes and 02 seconds'

Is it helpful? Interesting?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Numeric to time/period format (Ex: 1.to_s(:time, format: '%H:%M:%S'))

Wilson de Almeida
Hi Prem, thanks for you reply :)

strftime is a Time method, not Numeric. The diff between dates results a Float. If you ask this result if is_a?(Numeric) returns true, is_a?(Time) returns false.

My proposal is a helper like "strfperiod" if it is in Ruby. Working with diff time periods, not dates.

Example: 
https://gist.github.com/wilsondealmeida/e5903ac9693c6df949256c06b68b1656

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Numeric to time/period format (Ex: 1.to_s(:time, format: '%H:%M:%S'))

Wilson de Almeida
I create a gem to do the example:

https://rubygems.org/gems/numeric_period_format

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.