Cleaned up arithmetic operations of time durations
This commit is contained in:
parent
6af2e242f8
commit
70574329fc
|
@ -11,8 +11,8 @@ let dateDispatch = (call: ExpressionValue.functionCall, env: DistributionOperati
|
|||
| Ok(t) => EvDate(t)->Ok->Some
|
||||
| Error(e) => RETodo(e)->Error->Some
|
||||
}
|
||||
| ("fromMilliseconds", [EvNumber(f)]) => EvDate(E.Date.fromFloat(f))->Ok->Some
|
||||
| ("toMilliseconds", [EvDate(f)]) => EvNumber(E.Date.toFloat(f))->Ok->Some
|
||||
| ("dateFromNumber", [EvNumber(f)]) => EvDate(E.Date.fromFloat(f))->Ok->Some
|
||||
| ("toNumber", [EvDate(f)]) => EvNumber(E.Date.toFloat(f))->Ok->Some
|
||||
| ("subtract", [EvDate(d1), EvDate(d2)]) =>
|
||||
switch E.Date.subtract(d1, d2) {
|
||||
| Ok(d) => EvTimeDuration(d)->Ok
|
||||
|
@ -31,24 +31,21 @@ let durationDispatch = (call: ExpressionValue.functionCall, _: DistributionOpera
|
|||
switch call {
|
||||
| ("toString", [EvTimeDuration(t)]) => EvString(E.Duration.toString(t))->Ok->Some
|
||||
| ("hours", [EvNumber(f)]) => EvTimeDuration(E.Duration.fromHours(f))->Ok->Some
|
||||
| ("minutes", [EvNumber(f)]) => EvTimeDuration(E.Duration.fromMinutes(f))->Ok->Some
|
||||
| ("days", [EvNumber(f)]) => EvTimeDuration(E.Duration.fromDays(f))->Ok->Some
|
||||
| ("years", [EvNumber(f)]) => EvTimeDuration(E.Duration.fromYears(f))->Ok->Some
|
||||
| ("toHours", [EvTimeDuration(f)]) => EvNumber(E.Duration.toHours(f))->Ok->Some
|
||||
| ("toMinutes", [EvTimeDuration(f)]) => EvNumber(E.Duration.toMinutes(f))->Ok->Some
|
||||
| ("toDays", [EvTimeDuration(f)]) => EvNumber(E.Duration.toDays(f))->Ok->Some
|
||||
| ("toYears", [EvTimeDuration(f)]) => EvNumber(E.Duration.toYears(f))->Ok->Some
|
||||
| (
|
||||
("add" | "subtract" | "multiply" | "divide") as op,
|
||||
[EvTimeDuration(d1), EvTimeDuration(d2)],
|
||||
) => {
|
||||
let op = switch op {
|
||||
| "subtract" => E.Duration.subtract
|
||||
| "multiply" => E.Duration.multiply
|
||||
| "divide" => E.Duration.divide
|
||||
| "add"
|
||||
| _ => E.Duration.add
|
||||
}
|
||||
EvTimeDuration(op(d1, d2))->Ok->Some
|
||||
}
|
||||
| ("add", [EvTimeDuration(d1), EvTimeDuration(d2)]) =>
|
||||
EvTimeDuration(E.Duration.add(d1, d2))->Ok->Some
|
||||
| ("subtract", [EvTimeDuration(d1), EvTimeDuration(d2)]) =>
|
||||
EvTimeDuration(E.Duration.subtract(d1, d2))->Ok->Some
|
||||
| ("multiply", [EvTimeDuration(d1), EvNumber(d2)]) =>
|
||||
EvTimeDuration(E.Duration.multiply(d1, d2))->Ok->Some
|
||||
| ("divide", [EvTimeDuration(d1), EvNumber(d2)]) =>
|
||||
EvTimeDuration(E.Duration.divide(d1, d2))->Ok->Some
|
||||
| _ => None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -843,9 +843,11 @@ module Duration = {
|
|||
let year = Belt.Float.fromInt(24 * 60 * 60 * 1000) *. 365.25
|
||||
let fromFloat = (f: float): t => f
|
||||
let toFloat = (d: t): float => d
|
||||
let fromMinutes = (h: float): t => h *. minute
|
||||
let fromHours = (h: float): t => h *. hour
|
||||
let fromDays = (d: float): t => d *. day
|
||||
let fromYears = (y: float): t => y *. year
|
||||
let toMinutes = (t: t): float => t /. minute
|
||||
let toHours = (t: t): float => t /. hour
|
||||
let toDays = (t: t): float => t /. day
|
||||
let toYears = (t: t): float => t /. year
|
||||
|
@ -854,13 +856,14 @@ module Duration = {
|
|||
let shouldPluralize = f => f != 1.0
|
||||
let display = (f: float, s: string) =>
|
||||
`${Float.with3DigitsPrecision(f)} ${s}${shouldPluralize(f) ? "s" : ""}`
|
||||
if t >= year {
|
||||
let abs = Js.Math.abs_float(t)
|
||||
if abs >= year {
|
||||
display(t /. year, "year")
|
||||
} else if t >= day {
|
||||
} else if abs >= day {
|
||||
display(t /. day, "day")
|
||||
} else if t >= hour {
|
||||
} else if abs >= hour {
|
||||
display(t /. hour, "hour")
|
||||
} else if t >= minute {
|
||||
} else if abs >= minute {
|
||||
display(t /. minute, "minute")
|
||||
} else {
|
||||
Float.toFixed(t) ++ "ms"
|
||||
|
@ -868,8 +871,8 @@ module Duration = {
|
|||
}
|
||||
let add = (t1: t, t2: t): t => t1 +. t2
|
||||
let subtract = (t1: t, t2: t): t => t1 -. t2
|
||||
let multiply = (t1: t, t2: t): t => t1 *. t2
|
||||
let divide = (t1: t, t2: t): t => t1 /. t2
|
||||
let multiply = (t1: t, t2: float): t => t1 *. t2
|
||||
let divide = (t1: t, t2: float): t => t1 /. t2
|
||||
}
|
||||
|
||||
module Date = {
|
||||
|
|
Loading…
Reference in New Issue
Block a user