jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
967 stars 121 forks source link

Auto-localized unit labels : hour #114

Open Serrulien opened 6 years ago

Serrulien commented 6 years ago

Hello,

shouldn't the auto-localized unit label be the format's upper range instead of the lower ?

See these exemples :

moment.duration(60*60*2, "seconds").format("_HM_ _") // "2:00 mins" whereas I expect "2:00 hours"
moment.duration(2, "hour").format("_HMS_ _") // "2:00:00 secs" wheras I expect "2:00:00 hours"
jsmreese commented 6 years ago

Thanks for opening this issue, that's an interesting use case I hadn't considered.

A couple things to consider:

The time-style auto-format tokens expand into standard tokens before processing the format string. So _HM_ --> h:mm and _HMS_ --> h:mm:ss (by default, in English locale, at least...)

The auto-localized unit labels act on their neighbouring token. So h:mm _ has the _ act on the mm token.

This is so that formats like m _, s _ will work properly.

I can also see the case where you might have moment.duration(26, "hour").format("d __ _HMS_ __") and expect the output 2 days 2:00:00 hours.

In this one the auto-localized unit label isn't the format's upper range... it's possibly the format's contiguous upper range (likely doesn't hold across localization), or if you consider the time-style token as a token, it's the upper range of that set (that starts getting harder to detect, but might be doable).

What might work in the end is a way to specify an auto-localized unit label that doesn't auto-detect its token. So you could specify that this auto-localized unit label is for the seconds unit.

Not sure which solution is better and more intuitive. Thoughts?

jsmreese commented 5 years ago

Thinking about this, it seems like auto-units on any punctuated block need to find the largest magnitude token in the block. _HM_ __ --> h:mm __ --> 2:05 hours _MS_ _ --> m:ss _ --> 3:20 mins d __ _HMS_ __ --> d __ h:mm:ss __ --> 2 days 2:15:35 hours