Skip to content

Format options for Interval or Duration? #43

Description

@chexxor

MomentJS has a way to turn a duration into a human-readable string:

https://momentjs.com/docs/#/durations/humanize/

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize();  // a day

I've found myself wanting to turn an IsoDuration into a string. I'm not sure if that would belong in this library or not, as it seems tricky to specify the output format, as I haven't seen a similar specification like date's "YYYY". I imagine one way to implement this is like:

formatDuration :: { biggestFirst :: Boolean, ... } -> Duration -> String

-- It might be better to move the unit conversion into a separate function.
msFromDuration :: Duration -> Milliseconds
formatMS :: { fitToBiggestUnit :: Boolean, forceToUnit :: DurationComponent } -> Milliseconds -> String

-- I imagine would need a way to customize this for a locale.
newtype FormatDurationLocale :: FormatDurationLocale (DurationComponent -> String)
formatMS :: FormatDurationLocale -> FormatDurationOpts -> Milliseconds -> String

-- Test it out
-- > formatMS englishDurationLocale defaultDurationOpts { fitToBiggest = true } (msFromDuration tenMinutes)
-- 10 minutes

-- > formatMS (^ same as that ^) (msFromDuration tenDaysTenMinutes)
-- 1 week, 3 days, 10 minutes

-- > formatMS (englishDurationLocale defaultDurationOpts { forceToUnit = Week }) (msFromDuration tenDaysTenMinutes)
-- 1.31 weeks

Have you guys thought about this? Looks like current formatting is only to-from the ISO-8601 duration format.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions