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