[Feature][ActiveSupport::Duration] to_years, to_months, to_days, etc

58 views
Skip to first unread message

Jason York

unread,
Mar 5, 2020, 6:58:56 PM3/5/20
to Ruby on Rails: Core
Hi! Would it be valuable to add some convenience methods to Duration to convert the value to years, months, days?

Ex:

1.week.to_days
=> 7

3.days.to_weeks
=> 0.429

40.hours.to_days
=> 1.67

I wanted to check here before I work on a PR.

Thanks!

Josh Brody

unread,
Mar 5, 2020, 9:04:23 PM3/5/20
to rubyonra...@googlegroups.com
`1.week.days` is what you're looking for. I don't think an alias would be necessarily bad, though. It's similarly done elsewhere.

--
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 rubyonrails-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/5387f55e-341a-4b02-a24c-3e8d55f1c0ff%40googlegroups.com.

Phillip Getto

unread,
Mar 6, 2020, 9:03:13 AM3/6/20
to Ruby on Rails: Core
I think 1.week.days does not yield the desired result. It is the equivalent of 1.week.to_i.days or 604800 days. The other time period name helper methods would produce analogous results.


On Thursday, March 5, 2020 at 9:04:23 PM UTC-5, Josh Brody wrote:
`1.week.days` is what you're looking for. I don't think an alias would be necessarily bad, though. It's similarly done elsewhere.

On Thu, Mar 5, 2020 at 5:58 PM Jason York <jaso...@gmail.com> wrote:
Hi! Would it be valuable to add some convenience methods to Duration to convert the value to years, months, days?

Ex:

1.week.to_days
=> 7

3.days.to_weeks
=> 0.429

40.hours.to_days
=> 1.67

I wanted to check here before I work on a PR.

Thanks!

--
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 rubyonra...@googlegroups.com.

Jason York

unread,
Mar 6, 2020, 1:49:41 PM3/6/20
to Ruby on Rails: Core
Phillip is correct

1.week.days
=> 604800 days
Reply all
Reply to author
Forward
0 new messages