Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #368 Parse a millisecond value

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

DURATIONS = {years: 525_949_200, days: 1_440_000, hrs: 60_000, sec: 1000, ms: 1}
mod = a.round
m = DURATIONS.each_with_object({}) {|(k, v), h|  h[k], mod = mod.divmod(v) }
#include <string>
#include <vector>
using namespace std;
struct units : vector<pair<string, double>> {
    void set(string x, double y) {
        if (not empty()) y *= front().second;
        insert(begin(), {x, y});
    }
    auto get(int ms) {
        vector<pair<string, int>> m;
        for (auto& [x, y] : *this)
            if (ms >= y) {
                m.emplace_back(x, ms / y);
                ms %= int(y);
            }
        return m;
    }
};
import java.util.LinkedHashMap;
import java.util.Map;
import static java.util.List.of;
enum Units {
    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;
    Units(double x) { this.x = x; }
    static Map<String, Integer> get(long ms) {
        Map m = new LinkedHashMap<>();
        double x = ms, y;
        for (var u : of(values()).reversed())
            if (x >= (y = u.x)) {
                m.put(u.name(), (int) (x / y));
                x %= y;
            }
        return m;
    }
}
var m = Units.get(a);
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
class Units extends TreeMap<Double, String> {
    void set(String x, double y) {
        if (!isEmpty()) y *= lastKey();
        put(y, x);
    }
    Map<String, Integer> get(int ms) {
        var m = new LinkedHashMap();
        double x = ms, y;
        for (var e : reversed().entrySet())
            if (x >= (y = e.getKey())) {
                m.put(e.getValue(), (int) (x / y));
                x %= y;
            }
        return m;
    }
}
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;
...
class Units(list):
    def set(self, x, y):
        if self: y *= self[-1][1];
        self.append((x, y))
    def get(self, ms):
        m = {}
        for x, y in reversed(self):
            if ms >= y:
                m[x], ms = divmod(ms, y)
        return m
u = Units()
u.set('ms', 1)
u.set('s', 1_000)
u.set('m', 60)
u.set('h', 60)
u.set('d', 24)
u.set('y', 365.2425)
m = u.get(a)
u = []
def set(x, y):
    if u: y *= u[-1][1]
    u.append((x, y))
set('ms', 1)
set('s', 1_000)
set('m', 60)
set('h', 60)
set('d', 24)
set('y', 365.2425)
m = {}
for x, y in reversed(u):
    if a >= y:
        m[x], a = divmod(a, y)

New implementation...
< >
reilas