Logo

Programming-Idioms

Parse the millisecond value a, to a collection of duration values, m.

For example, `1,000,000 ms` is `{min=16, sec=40}`.

https://en.wikipedia.org/wiki/Gregorian_calendar
https://en.wikipedia.org/wiki/Time
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import static java.util.List.of;
import java.util.LinkedHashMap;
import java.util.Map;
enum Unit {
    ms(1), s(1_000 * ms.x), m(60 * s.x),
    h(60 * m.x), d(24 * h.x),
    y(365.2425 * d.x);
    double x;
    Unit(double x) { this.x = x; }
    static Map<Unit, Integer> parse(long ms) {
        Map m = new LinkedHashMap<>();
        double x = ms, y;
        for (Unit u : of(values()).reversed())
            if (x >= (y = u.x)) {
                m.put(u, (int) (x / y));
                x = x % y;
            }
        return m;
    }
}
Map<Unit, Integer> m = Unit.parse(a);
math
Type
   TT = record H, M, S, Ms: Integer; end;
   TTList = array of TT;
var
  t: TT;
  m: TTList;
  a: Integer;
begin
...
  divmod(a, MSecsPerSec*SecsPerHour, t.H, a);
  divmod(a, MSecsPerSec*SecsPerMin, t.M, a);
  divmod(a, MSecsPerSec, t.S, t.Ms);
  SetLength(m, Length(m)+1);
  m[High(m)] := t;
...