-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a human readable representation of duration #14028
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! glad we have support for ISO + human readable durations, thank you!
My only concern with this is that we are tying the English language to a human representation, something that we are not doing with any other kind of data from what I can remember (ie. the other representations are language-agnostic). |
Similar to comment above, I think when there is a to_string/1 function there tends to be a complementary function that parses from string into that data type, and it would probably be an overkill to add one for such human friendly representation. I think this is a good addition but I’m not sure about calling it to_string but no strong opinion. |
I agree that’s a concern point but, at the same time, the ISO representation (which we follow throughout) is also very English biased. P1Y3M5D only makes sense if you know English. |
Whilst true, the standard says:
Which, to my reading, emphasises the machine readable interchange as the priority.
I hope that |
Maybe we could introduce a new function convention as |
We already have some foundation for localization in strftime. Maybe we should follow that instead. |
Although I think I should clarify the goal here is to not provide full localization. In the same way that Decimal.to_string needs to choose a default representation, we need a similar one here. I think we can all agree the ISO one is not sufficient. :( |
since the set of words that need to be localized is fairly small, smaller than for strftime even, a similar way to allow for localization seems appropriate (if at all imho). Something like the following, allowing to specify singular + plural words for each duration unit.
unlike as for the function name, i agree with some of the concerns. How about |
If the intent is DX not UX then the inspect output is enough? And no translation required?(On mobile)Sent from my iPhoneOn 4 Dec 2024, at 08:59, Theodor Fiedler ***@***.***> wrote:
We already have some foundation for localization in strftime. Maybe we should follow that instead.
since the set of words that need to be localized is fairly small, smaller than for strftime even, a similar way to allow for localization seems appropriate (if at all imho). Something like the following, allowing to specify singular + plural words for each duration unit.
Duration.to_string(duration, format_options: [year: {"an", "ans"}, month: {"mois", "mois"}, week: {"semaine", "semaines"}, ...])
unlike Calendar.strftime/1 this function isn't necessarily meant to be used for end-users, but rather for logging and DX which tends to be in english.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: ***@***.***>
|
We cannot assume languages have only two plural forms :) |
right, also The need for localization is still arguable though. If not for something like
FWIW here is what other languages are doing:
|
some languages have a distinction between dual and plural (plural version of 2 is not the same as plural version of 3+). I would leave it up to the localized function the implementation details. |
Plural rules can be complex and definitely don't seem to fit into Elixir core - and that's before considering grammatical inflection. To me the Given the overlap I expect it could even be as simple as an enhancement to |
Thanks everyone for the valuable input. So let's start with the parts we can agree on: all Calendar data types have a
This information is localized but, because we use different separators for dates and times, we can get away with localizing it without relying on english words. I believe it is important for us to have a simple and accessible representation of durations, for consistency. However, durations are more complex because we don't have a generally expected format for durations, such as using So, what are our options here?
Feel free to comment on your preferences or alternatives. |
i'd be in favour of 4). I am torn about allowing to diverge from the standard, but also see no harm in adding a units option so it could be configured to read like initially proposed. Supported options could be Using |
PR updated to follow ISO 80000-3, so we can look at it as a reference. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with this approach is: how do we show 5 months and 3 minutes? 5M 3M, which is ugh.
I understand we probably won't go with ISO8601, but trying to answer based on wikipedia:
To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value)
...which feels too confusing for our purpose indeed. So LGTM for the proposed format and implementation.
Unrelated question, should we implement String.Chars
for consistency with other structs defining a to_string/1
?
I think option 4 is the standard way to go. The options accepted by the proposed implementation also makes it flexible to most needs. |
If we go with ISO 80000-3 I think we should also implement |
We should not implement |
Co-authored-by: Jean Klingler <sabiwara@gmail.com>
If @kipcole9 and @wojtekmach are happy, I will ship this. |
Thanks for asking @josevalim. Looks good to me. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a great compromise. I just have one question about negative durations.
💚 💙 💜 💛 ❤️ |
Without this function, projects like Phoenix.HTML, Postgrex, and similar would have to implement this function over and over.