123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687(*---------------------------------------------------------------------------
Copyright (c) 2025 The more programmers. All rights reserved.
SPDX-License-Identifier: ISC
---------------------------------------------------------------------------*)typeuint64=int64moduleSpan=struct(* Time spans are by a nanosecond magnitude stored in an unsigned 64-bit
integer. Allows to represent spans for ~584.5 Julian years. *)typet=uint64letzero=0Lletone=1Lletmax_span=-1Lletequal=Int64.equalletcompare=Int64.unsigned_compareletis_shorters~than=comparesthan<0letis_longers~than=comparesthan>0letadd=Int64.addletabs_diffs0s1=ifcompares0s1<0thenInt64.subs1s0elseInt64.subs0s1(* Durations *)let(*)nspan=Int64.mul(Int64.of_intn)spanletns=1Lletus=1_000Lletms=1_000_000Llets=1_000_000_000Lletmin=60_000_000_000Llethour=3600_000_000_000Lletday=86400_000_000_000Lletyear=31_557_600_000_000_000L(* Conversions *)letto_uint64_nss=sletof_uint64_nsns=nsletmax_float_int=9007199254740992.(* 2^53. *)letint64_min_int_float=Int64.to_floatInt64.min_intletint64_max_int_float=Int64.to_floatInt64.max_intletof_float_nssf=ifsf<0.||sf>=max_float_int||not(Float.is_finitesf)thenNoneelseSome(Int64.of_floatsf)letto_float_nss=ifInt64.compare0Ls<=0thenInt64.to_floatselseint64_max_int_float+.(-.int64_min_int_float+.Int64.to_floats)letpp=B0__fmt.uint64_ns_spanletpp_nsppfs=B0__fmt.pfppf"%Luns"send(* Timestamps *)typet=uint64letto_uint64_nst=tletof_uint64_nsns=nsletmin_stamp=0Lletmax_stamp=-1Lletppppfs=B0__fmt.pfppf"%Lu"s(* Predicates *)letequal=Int64.equalletcompare=Int64.unsigned_compareletis_earliert~than=comparetthan<0letis_latert~than=comparetthan>0(* Arithmetic *)letspant0t1=ifcomparet0t1<0thenInt64.subt1t0elseInt64.subt0t1letadd_spantspan=letsum=Int64.addtspaninifcomparetsum<=0thenSomesumelseNoneletsub_spantspan=ifcomparetspan<0thenNoneelseSome(Int64.subtspan)