text
stringlengths
0
601k
let of_sexp = Of_sexp . ( wrap_of_sexp of_sexp )
module Utils = struct let flatten_month_ranges ( months : int range Seq . t ) : int Seq . t option = try Some ( Month_ranges . Flatten . flatten months ) with Range . Range_is_invalid -> None let flatten_month_range_list ( months : int range list ) : int list option = try Some ( Month_ranges . Flatten . flatten_list months ) with Range . Range_is_invalid -> None let flatten_iso_week_ranges ( months : int range Seq . t ) : int Seq . t option = try Some ( ISO_week_ranges . Flatten . flatten months ) with Range . Range_is_invalid -> None let flatten_iso_week_range_list ( months : int range list ) : int list option = try Some ( ISO_week_ranges . Flatten . flatten_list months ) with Range . Range_is_invalid -> None let flatten_month_day_ranges ( month_days : int range Seq . t ) : int Seq . t option = try Some ( Month_day_ranges . Flatten . flatten month_days ) with Range . Range_is_invalid -> None let flatten_month_day_range_list ( month_days : int range list ) : int list option = try Some ( Month_day_ranges . Flatten . flatten_list month_days ) with Range . Range_is_invalid -> None let flatten_weekday_ranges ( weekdays : Timedesc . weekday range Seq . t ) : Timedesc . weekday Seq . t option = try Some ( Weekday_ranges . Flatten . flatten weekdays ) with Range . Range_is_invalid -> None let flatten_weekday_range_list ( weekdays : Timedesc . weekday range list ) : Timedesc . weekday list option = try Some ( Weekday_ranges . Flatten . flatten_list weekdays ) with Range . Range_is_invalid -> None end
type t = { mutable main : int ; mutable sub : int ; mutable div_c : int ; mutable tima : Uint8 . t ; mutable div : Uint8 . t ; mutable tma : Uint8 . t ; mutable tac : Uint8 . t ; }
let make ( ) = { main = 0 ; sub = 0 ; div_c = 0 ; div = Uint8 . zero ; tima = Uint8 . zero ; tma = Uint8 . zero ; tac = Uint8 . zero ; }
let get_mode { tac ; _ } = if Uint8 . code tac land 4 != 0 then match Uint8 . code tac land 3 with | 0 -> Some 64 | 1 -> Some 1 | 2 -> Some 4 | 3 -> Some 16 | _ -> None else None
let tick t m_cycles = t . sub <- t . sub + m_cycles ; if t . sub >= 4 then begin t . main <- succ t . main ; t . sub <- t . sub - 4 ; t . div_c <- succ t . div_c ; if t . div_c == 16 then begin t . div <- Uint8 . succ t . div ; t . div_c <- 0 end end ; match get_mode t with | None -> false | Some threshold when t . main > threshold -> begin t . main <- 0 ; t . tima <- Uint8 . succ t . tima ; if t . tima = Uint8 . zero then begin t . tima <- t . tma ; true end else false end | _ -> false
let config = { Scheduler . Config . concurrency = 1 ; display = { verbosity = Short ; status_line = false } ; rpc = None ; stats = None } Scheduler . Run . go ~ on_event ( : fun _ _ -> ( ) ) config ( fun ( ) -> let now ( ) = Unix . gettimeofday ( ) in let start = now ( ) in let duration = 0 . 2 in let + ( ) = Scheduler . sleep duration in assert ( now ( ) . - start >= duration ) ; print_endline " timer finished successfully " ) ; [ % expect { | timer finished successfully } ] | Scheduler . Run . go ~ on_event ( : fun _ _ -> ( ) ) config ( fun ( ) -> [ 0 . 3 ; 0 . 2 ; 0 . 1 ] |> Fiber . parallel_iter ~ f ( : fun duration -> let + ( ) = Scheduler . sleep duration in printfn " finished % 0 . 2f " duration ) ) ; [ % expect { | finished 0 . 10 finished 0 . 20 finished 0 . 30 } ] | Scheduler . Run . go ~ on_event ( : fun _ _ -> ( ) ) config ( fun ( ) -> let pid = let prog = let path = Env . get Env . initial " PATH " |> Option . value_exn |> Bin . parse_path in Bin . which ~ path " sleep " |> Option . value_exn |> Path . to_string in Spawn . spawn ~ prog ~ argv [ : prog ; " 100000 " ] ( ) |> Pid . of_int in let + _ = Scheduler . wait_for_process ~ timeout : 0 . 1 pid in print_endline " sleep timed out " ) ; [ % expect { | sleep timed out } ] |
let int64_pow b n = let rec loop b n acc = if n = 0 then acc else loop Int64 . ( mul b b ) ( n lsr 1 ) ( if n land 1 = 0 then acc else Int64 . ( mul b acc ) ) in loop b n 1L
let pp = Ptime . pp_human ~ frac_s : 6 ( )
let of_string x = let d_ps_of_intlit intlit = let sec = Int64 . of_string intlit in let d = Int64 . div sec 86_400L in let ps = Int64 . ( mul ( rem sec 86_400L ) 1_000_000_000_000L ) in ( Int64 . to_int d , ps ) in match match String . split_on_char ' . ' x with | [ _ ] -> Ptime . Span . of_d_ps ( d_ps_of_intlit x ) | [ sec_lit ; subsec_lit ] -> let ( d , ps_int ) = d_ps_of_intlit sec_lit in let ps_frac = if String . length subsec_lit <= 12 then let scale = int64_pow 10L ( 12 - String . length subsec_lit ) in Int64 . mul scale ( Int64 . of_string subsec_lit ) else Int64 . of_string ( String . sub subsec_lit 0 12 ) in Ptime . Span . of_d_ps ( d , Int64 . add ps_int ps_frac ) | _ -> None with | exception Failure _ -> None | None -> None | Some span -> Ptime . of_span span
let to_string ts = let d , ps = Ptime . Span . to_d_ps ( Ptime . diff ts Ptime . epoch ) in let sec = Int64 . ( add ( mul ( of_int d ) 86_400L ) ( div ps 1_000_000_000_000L ) ) in let subsec = Int64 . ( rem ps 1_000_000_000_000L ) in Printf . sprintf " % Ld . % 06Ld " sec ( Int64 . div subsec 1_000_000L )
let of_yojson json = match match json with | ` Int x -> Ptime . of_span ( Ptime . Span . of_int_s x ) | ` Intlit x -> of_string x | ` String x -> of_string x | _ -> None with | Some ts -> Result . Ok ts | None -> Result . Error " Couldn ' t parse timestamp "
let to_yojson ts = ` String ( to_string ts )
module type Extend_zone = Timezone_intf . Extend_zone
module Zone_cache = struct type z = { mutable full : bool ; basedir : string ; table : t String . Table . t } let the_one_and_only = { full = false ; basedir = Option . value ( Sys . getenv_opt " TZDIR " ) ~ default " :/ usr / share / zoneinfo " / ; table = String . Table . create ( ) } ; ; let find zone = Hashtbl . find the_one_and_only . table zone let find_or_load zonename = match find zonename with | Some z -> Some z | None -> if the_one_and_only . full then None else ( try let filename = the_one_and_only . basedir ^ " " / ^ zonename in let zone = input_tz_file ~ zonename ~ filename in Hashtbl . set the_one_and_only . table ~ key : zonename ~ data : zone ; Some zone with | _ -> None ) ; ; let traverse basedir ~ f = let skip_prefixes = [ " Etc / GMT " ; " right " ; / " posix " / ] in let maxdepth = 10 in let basedir_len = String . length basedir + 1 in let rec dfs dir depth = if depth < 1 then ( ) else Array . iter ( Sys . readdir dir ) ~ f ( : fun fn -> let fn = dir ^ " " / ^ fn in let relative_fn = String . drop_prefix fn basedir_len in match Sys . is_directory fn with | true -> if not ( List . exists skip_prefixes ~ f ( : fun prefix -> String . is_prefix ~ prefix relative_fn ) ) then dfs fn ( depth - 1 ) | false -> f relative_fn ) in dfs basedir maxdepth ; ; let init ( ) = if not the_one_and_only . full then ( traverse the_one_and_only . basedir ~ f ( : fun zone_name -> ignore ( find_or_load zone_name : t option ) ) ; the_one_and_only . full <- true ) ; ; let to_alist ( ) = Hashtbl . to_alist the_one_and_only . table let initialized_zones t = List . sort ~ compare ( : fun a b -> String . ascending ( fst a ) ( fst b ) ) ( to_alist t ) ; ; let find_or_load_matching t1 = let file_size filename = let c = Stdio . In_channel . create filename in let l = Stdio . In_channel . length c in Stdio . In_channel . close c ; l in let t1_file_size = Option . map ( original_filename t1 ) ~ f : file_size in with_return ( fun r -> let return_if_matches zone_name = let filename = String . concat ~ sep " " :/ [ the_one_and_only . basedir ; zone_name ] in let matches = try [ % compare . equal : int64 option ] t1_file_size ( Some ( file_size filename ) ) && [ % compare . equal : Md5 . t option ] ( digest t1 ) Option . ( join ( map ( find_or_load zone_name ) ~ f : digest ) ) with | _ -> false in if matches then r . return ( find_or_load zone_name ) else ( ) in List . iter ! likely_machine_zones ~ f : return_if_matches ; traverse the_one_and_only . basedir ~ f : return_if_matches ; None ) ; ; end
let find zone = let zone = match zone with | " utc " -> " UTC " | " gmt " -> " GMT " | " chi " -> " America / Chicago " | " nyc " -> " America / New_York " | " hkg " -> " Asia / Hong_Kong " | " lon " | " ldn " -> " Europe / London " | " tyo " -> " Asia / Tokyo " | _ -> zone in Zone_cache . find_or_load zone ; ;
let find_exn zone = match find zone with | None -> Error . raise_s [ % message " unknown zone " ( zone : string ) ] | Some z -> z ; ;
let local = let local_zone_name = Sys . getenv_opt " TZ " in let load ( ) = match local_zone_name with | Some zone_name -> find_exn zone_name | None -> let localtime_t = input_tz_file ~ zonename " :/ etc / localtime " ~ filename " :/ etc / localtime " in ( match Zone_cache . find_or_load_matching localtime_t with | Some t -> t | None -> localtime_t ) in Lazy . from_fun load ; ;
module Stable = struct include Core_kernel_private . Time_zone . Stable module V1 = struct type nonrec t = t let t_of_sexp sexp = match sexp with | Sexp . Atom " Local " -> Lazy . force local | Sexp . Atom name -> ( try if String . is_prefix name ~ prefix " : GMT " - || String . is_prefix name ~ prefix " : GMT " + || String . is_prefix name ~ prefix " : UTC " - || String . is_prefix name ~ prefix " : UTC " + || String . equal name " GMT " || String . equal name " UTC " then ( let offset = if String . equal name " GMT " || String . equal name " UTC " then 0 else ( let base = Int . of_string ( String . sub name ~ pos : 4 ~ len ( : String . length name - 4 ) ) in match name . [ 3 ] with | ' ' - -> - 1 * base | ' ' + -> base | _ -> assert false ) in of_utc_offset ~ hours : offset ) else find_exn name with | exc -> of_sexp_error ( sprintf " Timezone . t_of_sexp : % s " ( Exn . to_string exc ) ) sexp ) | _ -> of_sexp_error " Timezone . t_of_sexp : expected atom " sexp ; ; let sexp_of_t t = let name = name t in if String . equal name " / etc / localtime " then failwith " the local time zone cannot be serialized " ; Sexp . Atom name ; ; include Sexpable . Stable . To_stringable . V1 ( struct type nonrec t = t [ @@ deriving sexp ] end ) let compare t1 t2 = String . compare ( to_string t1 ) ( to_string t2 ) let hash_fold_t state t = String . hash_fold_t state ( to_string t ) let hash = Ppx_hash_lib . Std . Hash . of_fold hash_fold_t include ( Binable . Stable . Of_binable . V1 [ @ alert " - legacy " ] ( String ) ( struct type nonrec t = t let to_binable t = let name = name t in if String . equal name " / etc / localtime " then failwith " the local time zone cannot be serialized " ; name ; ; let of_binable s = t_of_sexp ( Sexp . Atom s ) end ) : Binable . S with type t := t ) end end let module_name = " Timezone " include Stable . V1 let of_string = of_string let to_string = to_string end )
module Private = struct module Zone_cache = Zone_cache end
module type Extend_zone = sig type t include Identifiable . S with type t := t val find : string -> t option val find_exn : string -> t val local : t Lazy . t val initialized_zones : unit -> ( string * t ) list val init : unit -> unit end
module type Timezone = sig module type Extend_zone = Extend_zone include Core_kernel_private . Time_zone . S with type t = Time . Zone . t include Extend_zone with type t := t module Stable : sig module V1 : sig type nonrec t = t [ @@ deriving bin_io , compare , hash , sexp ] end include Core_kernel_private . Time_zone . S_stable with type t := t end module Private : sig module Zone_cache : sig type z = { mutable full : bool ; basedir : string ; table : Time . Zone . t String . Table . t } val the_one_and_only : z val init : unit -> unit val find : string -> t option end end end
type unary_op = | Not | Shift of Timedesc . Span . t | Lengthen of Timedesc . Span . t | With_tz of Timedesc . Time_zone . t
type chunking = [ ` Disjoint_intervals | ` By_duration of Timedesc . Span . t | ` By_duration_drop_partial of Timedesc . Span . t | ` At_year_boundary | ` At_month_boundary ]
type chunked_unary_op_on_t = | Chunk_disjoint_interval | Chunk_at_year_boundary | Chunk_at_month_boundary | Chunk_by_duration of { chunk_size : Timedesc . Span . t ; drop_partial : bool ; }
type chunked_unary_op_on_chunked = | Drop of int | Take of int | Take_nth of int | Nth of int | Chunk_again of chunked_unary_op_on_t
type t = | Empty | All | Intervals of ( Timedesc . Span . t * Timedesc . Span . t ) Seq . t | ISO_week_pattern of Int_set . t * Int_set . t | Pattern of Pattern . t | Unary_op of unary_op * t | Inter_seq of t Seq . t | Union_seq of t Seq . t | Pattern_intervals of { mode : [ ` Whole_inc | ` Whole_exc | ` Fst | ` Snd ] ; bound : Timedesc . Span . t ; start : Points . t ; end_ : Points . t ; } | Unchunk of chunked | Unary_op_on_t of chunked_unary_op_on_t * t | Unary_op_on_chunked of chunked_unary_op_on_chunked * chunked
module type S = sig type t include Binable . S with type t := t include Comparable . S with type t := t include Hashable . S with type t := t include Sexpable . S with type t := t include Stringable . S with type t := t module Span : sig type t include Binable . S with type t := t include Comparable . S with type t := t include Comparable . With_zero with type t := t include Hashable . S with type t := t include Sexpable . S with type t := t include Stringable . S with type t := t val nanosecond : t val microsecond : t val millisecond : t val second : t val minute : t val hour : t val day : t val of_ns : float -> t val of_us : float -> t val of_ms : float -> t val of_sec : float -> t val of_min : float -> t val of_hr : float -> t val of_day : float -> t val to_ns : t -> float val to_us : t -> float val to_ms : t -> float val to_sec : t -> float val to_min : t -> float val to_hr : t -> float val to_day : t -> float val zero : t val ( + ) : t -> t -> t val ( - ) : t -> t -> t val abs : t -> t val neg : t -> t val scale : t -> float -> t val ( / ) : t -> float -> t val ( // ) : t -> t -> float val to_proportional_float : t -> float val to_short_string : t -> string val to_string_hum : ? delimiter : char -> ? decimals : int -> ? align_decimal : bool -> ? unit_of_time : Unit_of_time . t -> t -> string val randomize : t -> percent : Percent . t -> t val to_unit_of_time : t -> Unit_of_time . t val of_unit_of_time : Unit_of_time . t -> t end module Ofday : sig type t include Binable with type t := t include Comparable . S with type t := t include Hashable . S with type t := t include Sexpable . S with type t := t val start_of_day : t end val epoch : t val now : unit -> t val add : t -> Span . t -> t val sub : t -> Span . t -> t val diff : t -> t -> Span . t val abs_diff : t -> t -> Span . t val to_string_fix_proto : [ ` Local | ` Utc ] -> t -> string val of_string_fix_proto : [ ` Local | ` Utc ] -> string -> t val to_string_abs : t -> zone : Time . Zone . t -> string val of_string_abs : string -> t val next_multiple : ? can_equal_after : bool -> base : t -> after : t -> interval : Span . t -> unit -> t val of_date_ofday : zone : Time . Zone . t -> Date . t -> Ofday . t -> t val to_ofday : t -> zone : Time . Zone . t -> Ofday . t val to_date : t -> zone : Time . Zone . t -> Date . t val occurrence : [ ` First_after_or_at | ` Last_before_or_at ] -> t -> ofday : Ofday . t -> zone : Time . Zone . t -> t val pause : Span . t -> unit val interruptible_pause : Span . t -> [ ` Ok | ` Remaining of Span . t ] val pause_forever : unit -> never_returns end
module type S_kernel_without_zone = Time0_intf . S
module type S_kernel = Time_intf . S
module Stable = struct include Time_float0 . Stable module With_utc_sexp = struct module V2 = struct type nonrec t = t [ @@ deriving bin_io , compare , hash ] let sexp_of_t t = [ % sexp ( to_string_abs_parts t ~ zone : Zone . utc : string list ) ] let t_of_sexp sexp = try match sexp with | Sexp . List [ Sexp . Atom date ; Sexp . Atom ofday_and_possibly_zone ] -> of_string_gen ~ default_zone ( : fun ( ) -> Zone . utc ) ~ find_zone ( : fun _ -> of_sexp_error " Time . Stable . With_utc . V2 . t_of_sexp : unknown time zone " sexp ) ( date ^ " " ^ ofday_and_possibly_zone ) | _ -> of_sexp_error " Time . Stable . With_utc . V2 . t_of_sexp " sexp with | Of_sexp_error _ as e -> raise e | e -> of_sexp_error ( sprintf " Time . Stable . With_utc . V2 . t_of_sexp : % s " ( Exn . to_string e ) ) sexp ; ; end end module Zone = Zone_stable end
module Absolute = struct type underlying = Float . t include ( Float : sig type t = float [ @@ deriving bin_io , hash , typerep ] include Comparable . S_common with type t := t include module type of struct include Float . O end end ) include Float . Robust_compare . Make ( struct let robust_comparison_tolerance = 1E - 6 end ) let diff t1 t2 = Span . of_sec ( t1 - t2 ) let add t span = t . + Span . to_sec span let sub t span = t . - Span . to_sec span let prev t = Float . one_ulp ` Down t let next t = Float . one_ulp ` Up t let to_span_since_epoch = Span . of_sec let of_span_since_epoch = Span . to_sec end
module Date_and_ofday = struct type t = float let of_synthetic_span_since_epoch span = Span . to_sec span let to_synthetic_span_since_epoch t = Span . of_sec t let of_date_ofday date ofday = let days = Float . of_int ( Date0 . Days . diff ( Date0 . Days . of_date date ) Date0 . Days . unix_epoch ) in ( days . * 86400 . ) . + Span . to_sec ( Ofday . to_span_since_start_of_day ofday ) ; ; let to_absolute relative ~ offset_from_utc = sub relative offset_from_utc let of_absolute absolute ~ offset_from_utc = add absolute offset_from_utc let [ @ cold ] assert_in_bounds ~ sec_since_epoch = let gmtime_lower_bound = - 62_167_219_200 . in let gmtime_upper_bound = 253_402_300_799 . in if Float . ( >= ) sec_since_epoch ( gmtime_upper_bound . + 1 . ) || Float . ( < ) sec_since_epoch gmtime_lower_bound then failwithf " Time . gmtime : out of range ( % f ) " sec_since_epoch ( ) ; ; let sec_per_day = Int63 . of_int 86_400 let to_days_from_epoch t = assert_in_bounds ~ sec_since_epoch : t ; let open Int63 . O in let days_from_epoch_approx = Int63 . of_float t / sec_per_day in if Float . ( < ) t ( Int63 . to_float ( days_from_epoch_approx * sec_per_day ) ) then Int63 . pred days_from_epoch_approx else days_from_epoch_approx ; ; let ofday_of_days_from_epoch t ~ days_from_epoch = let open Int63 . O in let days_from_epoch_in_sec = Int63 . to_float ( days_from_epoch * sec_per_day ) in let remainder = t . - days_from_epoch_in_sec in Span . of_sec remainder |> Ofday . of_span_since_start_of_day_exn ; ; let date_of_days_from_epoch ~ days_from_epoch = Int63 . to_int_exn days_from_epoch |> Date0 . Days . add_days Date0 . Days . unix_epoch |> Date0 . Days . to_date ; ; let to_date t = let days_from_epoch = to_days_from_epoch t in date_of_days_from_epoch ~ days_from_epoch ; ; let to_ofday t = let days_from_epoch = to_days_from_epoch t in ofday_of_days_from_epoch t ~ days_from_epoch ; ; let to_date_ofday t = let days_from_epoch = to_days_from_epoch t in let date = date_of_days_from_epoch ~ days_from_epoch in let ofday = ofday_of_days_from_epoch t ~ days_from_epoch in date , ofday ; ; end
let next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval = if Span . ( <= ) interval Span . zero then failwiths ~ here [ :% here ] " Time . next_multiple got nonpositive interval " interval [ % sexp_of : Span . t ] ; let base_to_after = diff after base in if Span . ( < ) base_to_after Span . zero then base else ( let next = add base ( Span . scale interval ( Float . round ~ dir ` : Down ( Span . ( // ) base_to_after interval ) ) ) in if next > after || ( can_equal_after && next = after ) then next else add next interval ) ; ;
let next_multiple ( ? can_equal_after = false ) ~ base ~ after ~ interval ( ) = next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval ; ;
let prev_multiple ( ? can_equal_before = false ) ~ base ~ before ~ interval ( ) = next_multiple_internal ~ can_equal_after ( : not can_equal_before ) ~ base ~ after ( : sub before interval ) ~ interval ; ;
let now ( ) = let float_ns = Time_now . nanoseconds_since_unix_epoch ( ) |> Int63 . to_float in of_span_since_epoch ( Span . of_sec ( float_ns . * 1E - 9 ) ) ; ;
module Stable = struct module Span = Span . Stable module Ofday = Ofday . Stable end
module Int64_set = Set . Make ( struct type t = int64 let compare = compare end )
module Span_set = Diet . Make ( struct type t = Timedesc . Span . t let compare = Timedesc . Span . compare let zero = Timedesc . Span . zero let pred = Timedesc . Span . pred let succ = Timedesc . Span . succ let sub = Timedesc . Span . sub let add = Timedesc . Span . add let to_string = Timedesc . Span . to_string end )
module Alco = struct let suite = [ ] end
module Qc = struct let slice_start = QCheck . Test . make ~ count : 10_000 ~ name " : slice_start " QCheck . ( pair pos_timestamp sorted_time_slots_maybe_gaps ) ( fun ( start , l ) -> l |> CCList . to_seq |> Time . Intervals . Slice . slice ~ start |> OSeq . for_all ( fun ( x , _ ) -> Timedesc . Span . ( start <= x ) ) ) let slice_end_exc = QCheck . Test . make ~ count : 10_000 ~ name " : slice_end_exc " QCheck . ( pair pos_timestamp sorted_time_slots_maybe_gaps ) ( fun ( end_exc , l ) -> l |> CCList . to_seq |> Time . Intervals . Slice . slice ~ end_exc |> OSeq . for_all ( fun ( _ , y ) -> Timedesc . Span . ( y <= end_exc ) ) ) let normalize_pairs_are_fine = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_pairs_are_fine " time_slots ( fun l -> l |> CCList . to_seq |> Time . Intervals . normalize |> OSeq . for_all ( fun ( x , y ) -> Timedesc . Span . ( x <= y ) ) ) let normalize_time_slots_are_sorted = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_time_slots_are_sorted " time_slots ( fun l -> l |> CCList . to_seq |> Time . Intervals . normalize |> OSeq . fold_left ( fun ( res , last ) ( x , y ) -> if res then match last with | None -> ( true , Some ( x , y ) ) | Some ( last_start , last_end_exc ) -> Timedesc . Span . ( last_start <= x && last_end_exc <= x , Some ( x , y ) ) else ( false , None ) ) ( true , None ) |> fun ( x , _ ) -> x ) let normalize_time_slots_are_unique = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_time_slots_are_unique " time_slots ( fun l -> let l = l |> CCList . to_seq |> Time . Intervals . normalize |> CCList . of_seq in List . length ( List . sort_uniq compare l ) = List . length l ) let normalize_time_slots_are_disjoint_with_gaps = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_time_slots_are_disjoint_with_gaps " time_slots ( fun l -> l |> CCList . to_seq |> Time . Intervals . normalize |> Seq . fold_left ( fun ( res , last ) ( x , y ) -> if res then match last with | None -> ( true , Some ( x , y ) ) | Some ( _ , last_end_exc ) -> Timedesc . Span . ( last_end_exc < x , Some ( x , y ) ) else ( false , None ) ) ( true , None ) |> fun ( x , _ ) -> x ) let normalize_idempotent_wrt_normalized_time_slots = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_idempotent_wrt_normalized_time_slots " sorted_time_slots_with_gaps ( fun l -> l |> CCList . to_seq |> Time . Intervals . normalize |> CCList . of_seq = l ) let normalize_is_lossless = QCheck . Test . make ~ count : 10_000 ~ name " : normalize_is_lossless " sorted_time_slots_maybe_gaps ( fun l -> let original_timestamps = List . fold_left ( fun acc ( x , y ) -> Span_set . ( add ( Interval . make x y ) acc ) ) Span_set . empty l in let normalized_timestamps = l |> CCList . to_seq |> Time . Intervals . normalize ~ skip_sort : true |> Seq . fold_left ( fun acc ( x , y ) -> Span_set . ( add ( Interval . make x y ) acc ) ) Span_set . empty in Span_set . equal original_timestamps normalized_timestamps ) let join_time_slots_are_disjoint_with_gaps = QCheck . Test . make ~ count : 10_000 ~ name " : join_time_slots_are_disjoint_with_gaps " sorted_time_slots_maybe_gaps ( fun l -> l |> CCList . to_seq |> Time . Intervals . join |> Seq . fold_left ( fun ( res , last ) ( x , y ) -> if res then match last with | None -> ( true , Some ( x , y ) ) | Some ( _ , last_end_exc ) -> ( last_end_exc < x , Some ( x , y ) ) else ( false , None ) ) ( true , None ) |> fun ( x , _ ) -> x ) let join_idempotent_wrt_joined_time_slots = QCheck . Test . make ~ count : 10_000 ~ name " : join_idempotent_wrt_joined_time_slots " sorted_time_slots_with_gaps ( fun l -> l |> CCList . to_seq |> Time . Intervals . join |> CCList . of_seq = l ) let invert_disjoint_from_original = QCheck . Test . make ~ count : 10_000 ~ name " : invert_disjoint_from_original " QCheck . ( triple pos_timestamp pos_timestamp sorted_time_slots_maybe_gaps ) ( fun ( start , end_exc , l ) -> QCheck . assume Timedesc . Span . ( start <= end_exc ) ; let sliced = l |> CCList . to_seq |> Time . Intervals . Slice . slice ~ start ~ end_exc |> CCList . of_seq in let inverted = l |> CCList . to_seq |> Time . Intervals . invert ~ start ~ end_exc |> CCList . of_seq in let sliced_count = List . length sliced in let inverted_count = List . length inverted in List . length ( List . sort_uniq compare ( sliced @ inverted ) ) = sliced_count + inverted_count ) let invert_fit_gaps = QCheck . Test . make ~ count : 10_000 ~ name " : invert_fit_gaps " QCheck . ( triple pos_timestamp pos_timestamp sorted_time_slots_maybe_gaps ) ( fun ( start , end_exc , l ) -> QCheck . assume ( start < end_exc ) ; let res = l |> CCList . to_seq |> Time . Intervals . invert ~ start ~ end_exc |> ( fun inverted -> OSeq . append ( Time . Intervals . Slice . slice ~ start ~ end_exc ( CCList . to_seq l ) ) inverted ) |> Time . Intervals . normalize |> CCList . of_seq in l <> [ ] && List . for_all Timedesc . Span . ( fun ( x , y ) -> y < start || end_exc < x ) l || [ ( start , end_exc ) ] = res ) let relative_complement_result_disjoint_from_not_mem_of = QCheck . Test . make ~ count : 10_000 ~ name " : relative_complement_disjoint_from_not_mem_of " QCheck . ( pair sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( mem_of , not_mem_of ) -> let res = Time . Intervals . relative_complement ~ not_mem_of ( : CCList . to_seq not_mem_of ) ( CCList . to_seq mem_of ) |> CCList . of_seq in let not_mem_of_count = List . length not_mem_of in let res_count = List . length res in List . length ( List . sort_uniq compare ( not_mem_of @ res ) ) = not_mem_of_count + res_count ) let relative_complement_result_subset_of_mem_of = QCheck . Test . make ~ count : 10_000 ~ name " : relative_complement_result_subset_of_mem_of " QCheck . ( pair sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( mem_of , not_mem_of ) -> let res = Time . Intervals . relative_complement ~ not_mem_of ( : CCList . to_seq not_mem_of ) ( CCList . to_seq mem_of ) in Time . Intervals . Inter . inter ( CCList . to_seq mem_of ) res |> OSeq . equal ~ eq : Time . Interval ' . equal res ) let relative_complement_self = QCheck . Test . make ~ count : 10_000 ~ name " : relative_complement_self " sorted_time_slots_maybe_gaps ( fun l -> let s = CCList . to_seq l in Time . Intervals . relative_complement ~ not_mem_of : s s |> CCList . of_seq = [ ] ) let inter_with_self = QCheck . Test . make ~ count : 10_000 ~ name " : inter_with_self " sorted_time_slots_maybe_gaps ( fun l -> let s = l |> CCList . to_seq in let res = Time . Intervals . Inter . inter s s in OSeq . equal ~ eq : Time . Interval ' . equal s res ) let inter_commutative = QCheck . Test . make ~ count : 10_000 ~ name " : inter_commutative " QCheck . ( pair sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( l1 , l2 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let inter1 = Time . Intervals . Inter . inter s1 s2 in let inter2 = Time . Intervals . Inter . inter s2 s1 in OSeq . equal ~ eq : Time . Interval ' . equal inter1 inter2 ) let inter_associative = QCheck . Test . make ~ count : 10_000 ~ name " : inter_associative " QCheck . ( triple sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( l1 , l2 , l3 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let s3 = l3 |> CCList . to_seq in let inter1 = Time . Intervals . Inter . ( inter ( inter s1 s2 ) s3 ) in let inter2 = Time . Intervals . Inter . ( inter s1 ( inter s2 s3 ) ) in OSeq . equal ~ eq : Time . Interval ' . equal inter1 inter2 ) let union_with_self = QCheck . Test . make ~ count : 10_000 ~ name " : union_with_self " sorted_time_slots_with_gaps ( fun l -> let s = l |> CCList . to_seq in let res = Time . Intervals . Union . union s s in OSeq . equal ~ eq : Time . Interval ' . equal s res ) let union_commutative = QCheck . Test . make ~ count : 10_000 ~ name " : union_commutative " QCheck . ( pair sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( l1 , l2 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let inter1 = Time . Intervals . Union . union s1 s2 in let inter2 = Time . Intervals . Union . union s2 s1 in OSeq . equal ~ eq : Time . Interval ' . equal inter1 inter2 ) let union_associative = QCheck . Test . make ~ count : 10_000 ~ name " : union_associative " QCheck . ( triple sorted_time_slots_with_gaps sorted_time_slots_with_gaps sorted_time_slots_with_gaps ) ( fun ( l1 , l2 , l3 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let s3 = l3 |> CCList . to_seq in let res1 = Time . Intervals . ( Union . union ( Union . union s1 s2 ) s3 ) in let res2 = Time . Intervals . ( Union . union s1 ( Union . union s2 s3 ) ) in OSeq . equal ~ eq : Time . Interval ' . equal res1 res2 ) let inter_union_distributive1 = QCheck . Test . make ~ count : 10_000 ~ name " : inter_union_distributive1 " QCheck . ( triple sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( l1 , l2 , l3 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let s3 = l3 |> CCList . to_seq in let res1 = Time . Intervals . ( Union . union s1 ( Inter . inter s2 s3 ) ) in let res2 = Time . Intervals . ( Inter . inter ( Union . union s1 s2 ) ( Union . union s1 s3 ) ) in OSeq . equal ~ eq : Time . Interval ' . equal res1 res2 ) let inter_union_distributive2 = QCheck . Test . make ~ count : 10_000 ~ name " : inter_union_distributive2 " QCheck . ( triple sorted_time_slots_with_gaps sorted_time_slots_maybe_gaps sorted_time_slots_maybe_gaps ) ( fun ( l1 , l2 , l3 ) -> let s1 = l1 |> CCList . to_seq in let s2 = l2 |> CCList . to_seq in let s3 = l3 |> CCList . to_seq in let res1 = Time . Intervals . ( Inter . inter s1 ( Union . union s2 s3 ) ) in let res2 = Time . Intervals . ( Union . union ( Inter . inter s1 s2 ) ( Inter . inter s1 s3 ) ) in OSeq . equal ~ eq : Time . Interval ' . equal res1 res2 ) let suite = [ slice_start ; slice_end_exc ; normalize_pairs_are_fine ; normalize_time_slots_are_sorted ; normalize_time_slots_are_unique ; normalize_time_slots_are_disjoint_with_gaps ; normalize_idempotent_wrt_normalized_time_slots ; normalize_is_lossless ; join_time_slots_are_disjoint_with_gaps ; join_idempotent_wrt_joined_time_slots ; invert_disjoint_from_original ; invert_fit_gaps ; relative_complement_result_disjoint_from_not_mem_of ; relative_complement_result_subset_of_mem_of ; relative_complement_self ; inter_with_self ; inter_commutative ; inter_associative ; union_with_self ; union_commutative ; union_associative ; inter_union_distributive1 ; inter_union_distributive2 ; ] end
module type Zone = sig module Time : Time0_intf . S include Zone . S with type t = Zone . t and module Time_in_seconds := Time val abbreviation : t -> Time . t -> string val absolute_time_of_date_and_ofday : t -> Time . Date_and_ofday . t -> Time . t val date_and_ofday_of_absolute_time : t -> Time . t -> Time . Date_and_ofday . t val next_clock_shift : t -> strictly_after : Time . t -> ( Time . t * Time . Span . t ) option val prev_clock_shift : t -> at_or_before : Time . t -> ( Time . t * Time . Span . t ) option end
module type Basic = sig module Time : Time0_intf . S include module type of struct include Time end [ @ ocaml . remove_aliases ] val now : unit -> t module Zone : Zone with module Time := Time val add : t -> Span . t -> t val sub : t -> Span . t -> t val diff : t -> t -> Span . t val abs_diff : t -> t -> Span . t end
module type Shared = sig type t include Quickcheck . S_range with type t := t module Span : sig type t end module Ofday : sig type t end val is_earlier : t -> than : t -> bool val is_later : t -> than : t -> bool val of_date_ofday : zone : Zone . t -> Date . t -> Ofday . t -> t val of_date_ofday_precise : Date . t -> Ofday . t -> zone : Zone . t -> [ ` Once of t | ` Twice of t * t | ` Never of t ] val to_date_ofday : t -> zone : Zone . t -> Date . t * Ofday . t val to_date_ofday_precise : t -> zone : Zone . t -> Date . t * Ofday . t * [ ` Only | ` Also_at of t | ` Also_skipped of Date . t * Ofday . t ] val to_date : t -> zone : Zone . t -> Date . t val to_ofday : t -> zone : Zone . t -> Ofday . t val reset_date_cache : unit -> unit val epoch : t val convert : from_tz : Zone . t -> to_tz : Zone . t -> Date . t -> Ofday . t -> Date . t * Ofday . t val utc_offset : t -> zone : Zone . t -> Span . t include Stringable with type t := t val to_filename_string : t -> zone : Zone . t -> string val of_filename_string : string -> zone : Zone . t -> t val to_string_abs : t -> zone : Zone . t -> string val to_string_abs_trimmed : t -> zone : Zone . t -> string val to_string_abs_parts : t -> zone : Zone . t -> string list val to_string_trimmed : t -> zone : Zone . t -> string val to_sec_string : t -> zone : Zone . t -> string val of_localized_string : zone : Zone . t -> string -> t val of_string_gen : default_zone ( : unit -> Zone . t ) -> find_zone ( : string -> Zone . t ) -> string -> t val to_string_iso8601_basic : t -> zone : Zone . t -> string val occurrence : [ ` First_after_or_at | ` Last_before_or_at ] -> t -> ofday : Ofday . t -> zone : Zone . t -> t end
module type S = sig include Basic include Shared with type t := t with module Span := Span with module Ofday := Ofday end
module type Time = sig module type S = S module Make ( Time : Time0_intf . S ) : S with module Time := Time end
module type S = sig val duration : Measurement . key -> ( unit -> ' a ) -> ' a val duration_lwt : Measurement . key -> ( unit -> ' a Lwt . t ) -> ' a Lwt . t val timestamp_pre : Measurement . key -> ( unit -> ' a ) -> ' a val flush : unit -> unit Lwt . t end
module Make ( S : State . S with type elt := Measurement . t ) ( P : Publisher . S ) ( C : Clock . S ) : S = struct let span start stop = stop . - start let duration ( label , metadata ) f = let start = C . current_time ( ) in let res = f ( ) in let stop = C . current_time ( ) in let duration = span start stop in let measurement = Measurement . create label metadata duration in S . push measurement ; res let duration_lwt ( label , metadata ) f = let start = C . current_time ( ) in f ( ) >>= fun res -> let stop = C . current_time ( ) in let duration = span start stop in let measurement = Measurement . create label metadata duration in S . push measurement ; return res let timestamp_pre ( label , metadata ) f = let timestamp = C . current_time ( ) in let measurement = Measurement . create label metadata timestamp in S . push measurement ; f ( ) let flush ( ) = S . get_all_and_reset ( ) |> P . publish end
let arch_sixtyfour = Sys . word_size = 64
let add = Span . ( + )
let sub = Span . ( - )
let diff = Span . ( - )
let abs_diff t u = Span . abs ( diff t u )
let to_span_since_epoch t = t
let of_span_since_epoch s = s
let to_int63_ns_since_epoch t : Int63 . t = Span . to_int63_ns ( to_span_since_epoch t )
let of_int63_ns_since_epoch i = of_span_since_epoch ( Span . of_int63_ns i )
let is_earlier t1 ~ than : t2 = t1 < t2
let is_later t1 ~ than : t2 = t1 > t2
let add_overflowed x y ~ sum = if Span . ( > ) y Span . zero then Span . ( < ) sum x else Span . ( > ) sum x ; ;
let sub_overflowed x y ~ diff = if Span . ( > ) y Span . zero then Span . ( > ) diff x else Span . ( < ) diff x ; ;
let add_exn x y = let sum = add x y in if add_overflowed x y ~ sum then overflow ( ) else sum ; ;
let sub_exn x y = let diff = sub x y in if sub_overflowed x y ~ diff then overflow ( ) else diff ; ;
let add_saturating x y = let sum = add x y in if add_overflowed x y ~ sum then if Span . ( y > zero ) then Span . max_value_representable else Span . min_value_representable else sum ; ;
let sub_saturating x y = let diff = sub x y in if sub_overflowed x y ~ diff then if Span . ( y > zero ) then Span . min_value_representable else Span . max_value_representable else diff ; ;
let to_int_ns_since_epoch = if arch_sixtyfour then fun t -> Int63 . to_int_exn ( to_int63_ns_since_epoch t ) else fun _ -> failwith " Time_ns . to_int_ns_since_epoch : unsupported on 32bit machines " ; ;
let of_int_ns_since_epoch i = of_int63_ns_since_epoch ( Int63 . of_int i )
let to_time_float_round_nearest t = Time_float . of_span_since_epoch ( Span . to_span_float_round_nearest ( to_span_since_epoch t ) ) ; ;
let to_time_float_round_nearest_microsecond t = Time_float . of_span_since_epoch ( Span . to_span_float_round_nearest_microsecond ( to_span_since_epoch t ) ) ; ;
let min_time_value_for_1us_rounding = to_time_float_round_nearest min_value_for_1us_rounding ; ;
let max_time_value_for_1us_rounding = to_time_float_round_nearest max_value_for_1us_rounding ; ;
let check_before_conversion_for_1us_rounding time = if Time_float . ( < ) time min_time_value_for_1us_rounding || Time_float . ( > ) time max_time_value_for_1us_rounding then failwiths ~ here [ :% here ] " Time_ns does not support this time " time [ % sexp_of : Time_float . Stable . With_utc_sexp . V2 . t ] ; ;
let of_time_float_round_nearest time = of_span_since_epoch ( Span . of_span_float_round_nearest ( Time_float . to_span_since_epoch time ) ) ; ;
let of_time_float_round_nearest_microsecond time = check_before_conversion_for_1us_rounding time ; of_span_since_epoch ( Span . of_span_float_round_nearest_microsecond ( Time_float . to_span_since_epoch time ) ) ; ; failwiths ~ here [ :% here ] " Time_ns . next_multiple got nonpositive interval " interval [ % sexp_of : Span . t ] ; ;
let next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval = if Span . ( <= ) interval Span . zero then raise_next_multiple_got_nonpositive_interval interval ; let base_to_after = diff after base in if Span . ( < ) base_to_after Span . zero then base else ( let next = add base ( Span . scale_int63 interval ( Span . div base_to_after interval ) ) in if next > after || ( can_equal_after && next = after ) then next else add next interval ) ; ;
let next_multiple ( ? can_equal_after = false ) ~ base ~ after ~ interval ( ) = next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval ; ;
let prev_multiple ( ? can_equal_before = false ) ~ base ~ before ~ interval ( ) = next_multiple_internal ~ can_equal_after ( : not can_equal_before ) ~ base ~ after ( : sub before interval ) ~ interval ; ;
let random ? state ( ) = Span . random ? state ( )
module Utc : sig val to_date_and_span_since_start_of_day : t -> Date0 . t * Span . t val of_date_and_span_since_start_of_day : Date0 . t -> Span . t -> t let to_date_and_span_since_start_of_day t = let open Int63 . O in let ( !< ) i = Int63 . of_int_exn i in let ( !> ) t = Int63 . to_int_exn t in let ns_since_epoch = to_int63_ns_since_epoch t in let ns_per_day = !< 86_400 * !< 1_000_000_000 in let approx_days_from_epoch = ns_since_epoch / ns_per_day in let days_from_epoch = if ns_since_epoch < !< 0 && approx_days_from_epoch * ns_per_day <> ns_since_epoch then approx_days_from_epoch - !< 1 else approx_days_from_epoch in let ns_since_start_of_day = ns_since_epoch - ( ns_per_day * days_from_epoch ) in let date = Date0 . Days . add_days Date0 . Days . unix_epoch !> days_from_epoch |> Date0 . Days . to_date in let span_since_start_of_day = Span . of_int63_ns ns_since_start_of_day in date , span_since_start_of_day ; ; let of_date_and_span_since_start_of_day date span_since_start_of_day = assert ( Span . ( >= ) span_since_start_of_day Span . zero && Span . ( < ) span_since_start_of_day Span . day ) ; let days_from_epoch = Date0 . Days . diff ( Date0 . Days . of_date date ) Date0 . Days . unix_epoch in let span_in_days_since_epoch = Span . scale_int Span . day days_from_epoch in let span_since_epoch = Span . ( + ) span_in_days_since_epoch span_since_start_of_day in of_span_since_epoch span_since_epoch ; ; end
module Alternate_sexp = struct type nonrec t = t module Ofday_as_span = struct open Int . O let seconds_to_string seconds_span = let seconds = Span . to_int_sec seconds_span in let h = seconds / 3600 in let m = seconds / 60 % 60 in let s = seconds % 60 in sprintf " % 02d :% 02d :% 02d " h m s ; ; let two_digit_of_string string = assert ( String . length string = 2 && String . for_all string ~ f : Char . is_digit ) ; Int . of_string string ; ; let seconds_of_string seconds_string = match String . split seconds_string ~ on ' ' :: with | [ h_string ; m_string ; s_string ] -> let h = two_digit_of_string h_string in let m = two_digit_of_string m_string in let s = two_digit_of_string s_string in Span . of_int_sec ( ( ( ( h * 60 ) + m ) * 60 ) + s ) | _ -> assert false ; ; let ns_of_100_ms = 100_000_000 let ns_of_10_ms = 10_000_000 let ns_of_1_ms = 1_000_000 let ns_of_100_us = 100_000 let ns_of_10_us = 10_000 let ns_of_1_us = 1_000 let ns_of_100_ns = 100 let ns_of_10_ns = 10 let ns_of_1_ns = 1 let sub_second_to_string sub_second_span = let open Int . O in let ns = Span . to_int63_ns sub_second_span |> Int63 . to_int_exn in if ns = 0 then " " else if ns % ns_of_100_ms = 0 then sprintf " . % 01d " ( ns / ns_of_100_ms ) else if ns % ns_of_10_ms = 0 then sprintf " . % 02d " ( ns / ns_of_10_ms ) else if ns % ns_of_1_ms = 0 then sprintf " . % 03d " ( ns / ns_of_1_ms ) else if ns % ns_of_100_us = 0 then sprintf " . % 04d " ( ns / ns_of_100_us ) else if ns % ns_of_10_us = 0 then sprintf " . % 05d " ( ns / ns_of_10_us ) else if ns % ns_of_1_us = 0 then sprintf " . % 06d " ( ns / ns_of_1_us ) else if ns % ns_of_100_ns = 0 then sprintf " . % 07d " ( ns / ns_of_100_ns ) else if ns % ns_of_10_ns = 0 then sprintf " . % 08d " ( ns / ns_of_10_ns ) else sprintf " . % 09d " ns ; ; let sub_second_of_string string = if String . is_empty string then Span . zero else ( let digits = String . chop_prefix_exn string ~ prefix " . " : in assert ( String . for_all digits ~ f : Char . is_digit ) ; let multiplier = match String . length digits with | 1 -> ns_of_100_ms | 2 -> ns_of_10_ms | 3 -> ns_of_1_ms | 4 -> ns_of_100_us | 5 -> ns_of_10_us | 6 -> ns_of_1_us | 7 -> ns_of_100_ns | 8 -> ns_of_10_ns | 9 -> ns_of_1_ns | _ -> assert false in Span . of_int63_ns ( Int63 . of_int ( Int . of_string digits * multiplier ) ) ) ; ; let to_string span = assert ( Span . ( >= ) span Span . zero && Span . ( < ) span Span . day ) ; let seconds_span = span |> Span . to_int_sec |> Span . of_int_sec in let sub_second_span = Span . ( - ) span seconds_span in seconds_to_string seconds_span ^ sub_second_to_string sub_second_span ; ; let of_string string = let len = String . length string in let prefix_len = 8 in let suffix_len = len - prefix_len in let seconds_string = String . sub string ~ pos : 0 ~ len : prefix_len in let sub_second_string = String . sub string ~ pos : prefix_len ~ len : suffix_len in let seconds_span = seconds_of_string seconds_string in let sub_second_span = sub_second_of_string sub_second_string in Span . ( + ) seconds_span sub_second_span ; ; end let to_string t = let date , span_since_start_of_day = Utc . to_date_and_span_since_start_of_day t in Date0 . to_string date ^ " " ^ Ofday_as_span . to_string span_since_start_of_day ^ " Z " ; ; let of_string string = let date_string , ofday_string_with_zone = String . lsplit2_exn string ~ on ' : ' in let ofday_string = String . chop_suffix_exn ofday_string_with_zone ~ suffix " : Z " in let date = Date0 . of_string date_string in let ofday = Ofday_as_span . of_string ofday_string in Utc . of_date_and_span_since_start_of_day date ofday ; ; include Sexpable . Of_stringable ( struct type nonrec t = t let to_string = to_string let of_string = of_string end ) module Stable = struct module V1 = struct type nonrec t = t [ @@ deriving bin_io , compare , sexp ] end end end
module Stable = struct module Alternate_sexp = Alternate_sexp . Stable module Span = Span . Stable module Ofday = Ofday . Stable end
module To_and_of_string : sig val of_date_ofday : zone : Zone . t -> Date . t -> Ofday . t -> t val of_date_ofday_precise : Date . t -> Ofday . t -> zone : Zone . t -> [ ` Once of t | ` Twice of t * t | ` Never of t ] val to_date_ofday : t -> zone : Zone . t -> Date . t * Ofday . t val to_date_ofday_precise : t -> zone : Zone . t -> Date . t * Ofday . t * [ ` Only | ` Also_at of t | ` Also_skipped of Date . t * Ofday . t ] val to_date : t -> zone : Zone . t -> Date . t val to_ofday : t -> zone : Zone . t -> Ofday . t val convert : from_tz : Zone . t -> to_tz : Zone . t -> Date . t -> Ofday . t -> Date . t * Ofday . t val reset_date_cache : unit -> unit val utc_offset : t -> zone : Zone . t -> Span . t include Stringable with type t := t val to_filename_string : t -> zone : Zone . t -> string val of_filename_string : string -> zone : Zone . t -> t val to_string_trimmed : t -> zone : Zone . t -> string val to_sec_string : t -> zone : Zone . t -> string val of_localized_string : zone : Zone . t -> string -> t val of_string_gen : default_zone ( : unit -> Zone . t ) -> find_zone ( : string -> Zone . t ) -> string -> t val to_string_abs : t -> zone : Zone . t -> string val to_string_abs_trimmed : t -> zone : Zone . t -> string val to_string_abs_parts : t -> zone : Zone . t -> string list val to_string_iso8601_basic : t -> zone : Zone . t -> string val occurrence : [ ` First_after_or_at | ` Last_before_or_at ] -> t -> ofday : Ofday . t -> zone : Zone . t -> t module Date_and_ofday = struct type t = Int63 . t let to_synthetic_span_since_epoch t = Span . of_int63_ns t let of_date_ofday date ofday = let days = Date0 . Days . diff ( Date0 . Days . of_date date ) Date0 . Days . unix_epoch |> Int63 . of_int in let open Int63 . O in ( days * Span . to_int63_ns Span . day ) + Span . to_int63_ns ( Ofday . to_span_since_start_of_day ofday ) ; ; let to_absolute relative ~ offset_from_utc = sub_exn ( Span . of_int63_ns relative ) offset_from_utc ; ; let of_absolute absolute ~ offset_from_utc = Span . to_int63_ns ( add_exn absolute offset_from_utc ) ; ; let ns_per_day = Span . to_int63_ns Span . day let to_days_from_epoch t = let open Int63 . O in let days_from_epoch_approx = t / ns_per_day in if t < days_from_epoch_approx * ns_per_day then Int63 . pred days_from_epoch_approx else days_from_epoch_approx ; ; let ofday_of_days_from_epoch t ~ days_from_epoch = let open Int63 . O in let days_from_epoch_in_ns = days_from_epoch * ns_per_day in let remainder = t - days_from_epoch_in_ns in Span . of_int63_ns remainder |> Ofday . of_span_since_start_of_day_exn ; ; let date_of_days_from_epoch ~ days_from_epoch = Int63 . to_int_exn days_from_epoch |> Date0 . Days . add_days Date0 . Days . unix_epoch |> Date0 . Days . to_date ; ; let to_date t = let days_from_epoch = to_days_from_epoch t in date_of_days_from_epoch ~ days_from_epoch ; ; let to_ofday t = let days_from_epoch = to_days_from_epoch t in ofday_of_days_from_epoch t ~ days_from_epoch ; ; end module Zone0 = Zone module Zone : sig type time = t type t = Zone . t [ @@ deriving sexp_of ] module Index = Zone . Index val utc : t val index_has_prev_clock_shift : t -> Index . t -> bool val index_has_next_clock_shift : t -> Index . t -> bool val index : t -> time -> Index . t val index_offset_from_utc_exn : t -> Index . t -> time val index_prev_clock_shift_time_exn : t -> Index . t -> time val index_next_clock_shift_time_exn : t -> Index . t -> time val absolute_time_of_date_and_ofday : t -> Date_and_ofday . t -> time val date_and_ofday_of_absolute_time : t -> time -> Date_and_ofday . t val next_clock_shift : t -> strictly_after : time -> ( time * Span . t ) option val prev_clock_shift : t -> at_or_before : time -> ( time * Span . t ) option end = struct type time = t include Zone let of_span_in_seconds span_in_seconds = Time_in_seconds . Span . to_int63_seconds_round_down_exn span_in_seconds |> Span . of_int63_seconds ; ; let of_time_in_seconds time_in_seconds = Time_in_seconds . to_span_since_epoch time_in_seconds |> Time_in_seconds . Span . to_int63_seconds_round_down_exn |> Span . of_int63_seconds |> of_span_since_epoch ; ; let to_time_in_seconds_round_down_exn time = to_span_since_epoch time |> Span . to_int63_seconds_round_down_exn |> Time_in_seconds . Span . of_int63_seconds |> Time_in_seconds . of_span_since_epoch ; ; let to_date_and_ofday_in_seconds_round_down_exn relative = Date_and_ofday . to_synthetic_span_since_epoch relative |> Span . to_int63_seconds_round_down_exn |> Time_in_seconds . Span . of_int63_seconds |> Time_in_seconds . Date_and_ofday . of_synthetic_span_since_epoch ; ; let index t time = index t ( to_time_in_seconds_round_down_exn time ) let index_of_date_and_ofday t relative = index_of_date_and_ofday t ( to_date_and_ofday_in_seconds_round_down_exn relative ) ; ; let index_offset_from_utc_exn t index = of_span_in_seconds ( index_offset_from_utc_exn t index ) ; ; let index_prev_clock_shift_time_exn t index = of_time_in_seconds ( index_prev_clock_shift_time_exn t index ) ; ; let index_next_clock_shift_time_exn t index = of_time_in_seconds ( index_next_clock_shift_time_exn t index ) ; ; let index_prev_clock_shift_amount_exn t index = of_span_in_seconds ( index_prev_clock_shift_amount_exn t index ) ; ; let index_prev_clock_shift t index = match index_has_prev_clock_shift t index with | false -> None | true -> Some ( index_prev_clock_shift_time_exn t index , index_prev_clock_shift_amount_exn t index ) ; ; let index_next_clock_shift t index = index_prev_clock_shift t ( Index . next index ) let prev_clock_shift t ~ at_or_before : time = index_prev_clock_shift t ( index t time ) let next_clock_shift t ~ strictly_after : time = index_next_clock_shift t ( index t time ) let date_and_ofday_of_absolute_time t time = let index = index t time in let offset_from_utc = index_offset_from_utc_exn t index in Date_and_ofday . of_absolute time ~ offset_from_utc ; ; let absolute_time_of_date_and_ofday t relative = let index = index_of_date_and_ofday t relative in let offset_from_utc = index_offset_from_utc_exn t index in Date_and_ofday . to_absolute relative ~ offset_from_utc ; ; end let of_date_ofday ~ zone date ofday = let relative = Date_and_ofday . of_date_ofday date ofday in Zone . absolute_time_of_date_and_ofday zone relative ; ; let of_date_ofday_precise date ofday ~ zone = let start_of_day = of_date_ofday ~ zone date Ofday . start_of_day in let proposed_time = add start_of_day ( Ofday . to_span_since_start_of_day ofday ) in match Zone . next_clock_shift zone ~ strictly_after : start_of_day with | None -> ` Once proposed_time | Some ( shift_start , shift_amount ) -> let shift_backwards = Span . ( shift_amount < zero ) in let s , e = if shift_backwards then add shift_start shift_amount , shift_start else shift_start , add shift_start shift_amount in if proposed_time < s then ` Once proposed_time else if s <= proposed_time && proposed_time < e then if shift_backwards then ` Twice ( proposed_time , sub proposed_time shift_amount ) else ` Never shift_start else ` Once ( sub proposed_time shift_amount ) ; ; module Date_cache = struct type nonrec t = { mutable zone : Zone . t ; mutable cache_start_incl : t ; mutable cache_until_excl : t ; mutable effective_day_start : t ; mutable date : Date0 . t } end let date_cache : Date_cache . t = { zone = Zone . utc ; cache_start_incl = epoch ; cache_until_excl = epoch ; effective_day_start = epoch ; date = Date0 . unix_epoch } ; ; let reset_date_cache ( ) = date_cache . zone <- Zone . utc ; date_cache . cache_start_incl <- epoch ; date_cache . cache_until_excl <- epoch ; date_cache . effective_day_start <- epoch ; date_cache . date <- Date0 . unix_epoch ; ; let is_in_cache time ~ zone = phys_equal zone date_cache . zone && time >= date_cache . cache_start_incl && time < date_cache . cache_until_excl ; ; let set_date_cache time ~ zone = match is_in_cache time ~ zone with | true -> ( ) | false -> let index = Zone . index zone time in let offset_from_utc = Zone . index_offset_from_utc_exn zone index in let rel = Date_and_ofday . of_absolute time ~ offset_from_utc in let date = Date_and_ofday . to_date rel in let span = Date_and_ofday . to_ofday rel |> Ofday . to_span_since_start_of_day in let effective_day_start = sub ( Date_and_ofday . to_absolute rel ~ offset_from_utc ) span in let effective_day_until = add effective_day_start Span . day in let cache_start_incl = match Zone . index_has_prev_clock_shift zone index with | false -> effective_day_start | true -> effective_day_start |> max ( Zone . index_prev_clock_shift_time_exn zone index ) in let cache_until_excl = match Zone . index_has_next_clock_shift zone index with | false -> effective_day_until | true -> effective_day_until |> min ( Zone . index_next_clock_shift_time_exn zone index ) in date_cache . zone <- zone ; date_cache . cache_start_incl <- cache_start_incl ; date_cache . cache_until_excl <- cache_until_excl ; date_cache . effective_day_start <- effective_day_start ; date_cache . date <- date ; ; let to_date time ~ zone = set_date_cache time ~ zone ; date_cache . date ; ; let to_ofday time ~ zone = set_date_cache time ~ zone ; diff time date_cache . effective_day_start |> Ofday . of_span_since_start_of_day_exn ; ; let to_date_ofday time ~ zone = to_date time ~ zone , to_ofday time ~ zone let to_date_ofday_precise time ~ zone = let date , ofday = to_date_ofday time ~ zone in let clock_shift_after = Zone . next_clock_shift zone ~ strictly_after : time in let clock_shift_before_or_at = Zone . prev_clock_shift zone ~ at_or_before : time in let also_skipped_earlier amount = match Ofday . sub ofday amount with | Some ofday -> ` Also_skipped ( date , ofday ) | None -> raise_s [ % message " Time . to_date_ofday_precise " ~ span_since_epoch ( : to_span_since_epoch time : Span . t ) ( zone : Zone . t ) ] in let ambiguity = match clock_shift_before_or_at , clock_shift_after with | Some ( start , amount ) , _ when add start ( Span . abs amount ) > time -> if Span . ( amount > zero ) then also_skipped_earlier amount else ( assert ( Span . ( amount < zero ) ) ; ` Also_at ( sub time ( Span . abs amount ) ) ) | _ , Some ( start , amount ) when sub start ( Span . abs amount ) <= time -> if Span . ( amount > zero ) then ` Only else ( assert ( Span . ( amount < zero ) ) ; ` Also_at ( add time ( Span . abs amount ) ) ) | _ -> ` Only in date , ofday , ambiguity ; ; let convert ~ from_tz ~ to_tz date ofday = let start_time = of_date_ofday ~ zone : from_tz date ofday in to_date_ofday ~ zone : to_tz start_time ; ; let utc_offset t ~ zone = let utc_epoch = Zone . date_and_ofday_of_absolute_time zone t in Span . ( - ) ( Date_and_ofday . to_synthetic_span_since_epoch utc_epoch ) ( to_span_since_epoch t ) ; ; let offset_string time ~ zone = let utc_offset = utc_offset time ~ zone in let is_utc = Span . ( = ) utc_offset Span . zero in if is_utc then " Z " else String . concat [ ( if Span . ( < ) utc_offset Span . zero then " " - else " " ) + ; Ofday . to_string_trimmed ( Ofday . of_span_since_start_of_day_exn ( Span . abs utc_offset ) ) ] ; ; let to_string_abs_parts = let attempt time ~ zone = let date , ofday = to_date_ofday time ~ zone in let offset_string = offset_string time ~ zone in [ Date0 . to_string date ; String . concat ~ sep " " : [ Ofday . to_string ofday ; offset_string ] ] in fun time ~ zone -> try attempt time ~ zone with | ( _ : exn ) -> attempt time ~ zone : Zone . utc ; ; let to_string_abs_trimmed time ~ zone = let date , ofday = to_date_ofday time ~ zone in let offset_string = offset_string time ~ zone in String . concat ~ sep " : " [ Date0 . to_string date ; Ofday . to_string_trimmed ofday ^ offset_string ] ; ; let to_string_abs time ~ zone = String . concat ~ sep " : " ( to_string_abs_parts ~ zone time ) let to_string t = to_string_abs t ~ zone : Zone . utc let to_string_iso8601_basic time ~ zone = String . concat ~ sep " : T " ( to_string_abs_parts ~ zone time ) ; ; let to_string_trimmed t ~ zone = let date , sec = to_date_ofday ~ zone t in Date0 . to_string date ^ " " ^ Ofday . to_string_trimmed sec ; ; let to_sec_string t ~ zone = let date , sec = to_date_ofday ~ zone t in Date0 . to_string date ^ " " ^ Ofday . to_sec_string sec ; ; let to_filename_string t ~ zone = let date , ofday = to_date_ofday ~ zone t in Date0 . to_string date ^ " _ " ^ String . tr ~ target ' ' :: ~ replacement ' ' :- ( String . drop_suffix ( Ofday . to_string ofday ) 3 ) ; ; let of_filename_string s ~ zone = try match String . lsplit2 s ~ on ' : _ ' with | None -> failwith " no space in filename string " | Some ( date , ofday ) -> let date = Date0 . of_string date in let ofday = String . tr ~ target ' ' :- ~ replacement ' ' :: ofday in let ofday = Ofday . of_string ofday in of_date_ofday date ofday ~ zone with | exn -> invalid_argf " Time . of_filename_string ( % s ) : % s " s ( Exn . to_string exn ) ( ) ; ; let of_localized_string ~ zone str = try match String . lsplit2 str ~ on ' : ' with | None -> invalid_arg ( sprintf " no space in date_ofday string : % s " str ) | Some ( date , time ) -> let date = Date0 . of_string date in let ofday = Ofday . of_string time in of_date_ofday ~ zone date ofday with | e -> Exn . reraise e " Time . of_localized_string " ; ; let occurrence before_or_after t ~ ofday ~ zone = let first_guess_date = to_date t ~ zone in let first_guess = of_date_ofday ~ zone first_guess_date ofday in let cmp , increment = match before_or_after with | ` Last_before_or_at -> ( <= ) , - 1 | ` First_after_or_at -> ( >= ) , 1 in if cmp first_guess t then first_guess else of_date_ofday ~ zone ( Date0 . add_days first_guess_date increment ) ofday ; ; let ensure_colon_in_offset offset = let offset_length = String . length offset in if Int . ( <= ) offset_length 2 && Char . is_digit offset . [ 0 ] && Char . is_digit offset . [ offset_length - 1 ] then offset ^ " : 00 " else if Char . ( = ) offset . [ 1 ] ' ' : || Char . ( = ) offset . [ 2 ] ' ' : then offset else if Int . ( < ) offset_length 3 || Int . ( > ) offset_length 4 then failwithf " invalid offset % s " offset ( ) else String . concat [ String . slice offset 0 ( offset_length - 2 ) ; " " : ; String . slice offset ( offset_length - 2 ) offset_length ] ; ; exception Time_ns_of_string of string * Exn . t [ @@ deriving sexp ] let of_string_gen ~ default_zone ~ find_zone s = try let date , ofday , tz = match String . split s ~ on ' : ' with | [ day ; month ; year ; ofday ] -> String . concat [ day ; " " ; month ; " " ; year ] , ofday , None | [ date ; ofday ; tz ] -> date , ofday , Some tz | [ date ; ofday ] -> date , ofday , None | [ s ] -> ( match String . rsplit2 ~ on ' : T ' s with | Some ( date , ofday ) -> date , ofday , None | None -> failwith " no spaces or T found " ) | _ -> failwith " too many spaces " in let ofday_to_sec od = Span . to_sec ( Ofday . to_span_since_start_of_day od ) in let ofday , utc_offset = match tz with | Some _ -> ofday , None | None -> if Char . ( = ) ofday . [ String . length ofday - 1 ] ' Z ' then String . sub ofday ~ pos : 0 ~ len ( : String . length ofday - 1 ) , Some 0 . else ( match String . lsplit2 ~ on ' ' :+ ofday with | Some ( l , r ) -> l , Some ( ofday_to_sec ( Ofday . of_string ( ensure_colon_in_offset r ) ) ) | None -> ( match String . lsplit2 ~ on ' ' :- ofday with | Some ( l , r ) -> l , Some ( - 1 . . * ofday_to_sec ( Ofday . of_string ( ensure_colon_in_offset r ) ) ) | None -> ofday , None ) ) in let date = Date0 . of_string date in let ofday = Ofday . of_string ofday in match tz with | Some tz -> of_date_ofday ~ zone ( : find_zone tz ) date ofday | None -> ( match utc_offset with | None -> let zone = default_zone ( ) in of_date_ofday ~ zone date ofday | Some utc_offset -> let utc_t = of_date_ofday ~ zone : Zone . utc date ofday in sub utc_t ( Span . of_sec utc_offset ) ) with | e -> raise ( Time_ns_of_string ( s , e ) ) ; ; let of_string s = let default_zone ( ) = raise_s [ % message " time has no time zone or UTC offset " s ] in let find_zone zone_name = failwithf " unable to lookup Zone % s . Try using Core . Time . of_string " zone_name ( ) in of_string_gen ~ default_zone ~ find_zone s ; ; end
let min_value_representable = of_span_since_epoch Span . min_value_representable
let max_value_representable = of_span_since_epoch Span . max_value_representable
module For_ppx_module_timer = struct open Ppx_module_timer_runtime let ( ) = Duration . format := ( module struct let duration_of_span s = s |> Span . to_int63_ns |> Duration . of_nanoseconds let span_of_duration d = d |> Duration . to_nanoseconds |> Span . of_int63_ns let of_string string = string |> Span . of_string |> duration_of_span let to_string_with_same_unit durations = let spans = durations |> List . map ~ f : span_of_duration in let unit_of_time = spans |> List . max_elt ~ compare : Span . compare |> Option . value_map ~ f : Span . to_unit_of_time ~ default : Unit_of_time . Nanosecond in spans |> List . map ~ f ( : Span . to_string_hum ~ unit_of_time ~ align_decimal : true ) ; ; end ) ; ; end
module type Span = sig type t = private Int63 . t [ @@ deriving hash ] include Span_intf . S with type underlying = Int63 . t and type t := t val of_sec_with_microsecond_precision : float -> t val of_int_us : int -> t val of_int_ms : int -> t val to_int_us : t -> int val to_int_ms : t -> int val to_int_sec : t -> int val min_value_representable : t val max_value_representable : t val min_value_for_1us_rounding : t val max_value_for_1us_rounding : t val min_value : t [ @@ deprecated " [ since 2019 - 02 ] use [ min_value_representable ] or [ min_value_for_1us_rounding ] \ instead " ] val max_value : t [ @@ deprecated " [ since 2019 - 02 ] use [ max_value_representable ] or [ max_value_for_1us_rounding ] \ instead " ] val scale_int : t -> int -> t val scale_int63 : t -> Int63 . t -> t val div : t -> t -> Int63 . t val to_int63_ns : t -> Int63 . t val of_int63_ns : Int63 . t -> t val to_int_ns : t -> int val of_int_ns : int -> t val since_unix_epoch : unit -> t val random : ? state : Random . State . t -> unit -> t val to_span : t -> Span_float . t [ @@ deprecated " [ since 2019 - 01 ] use [ to_span_float_round_nearest ] or \ [ to_span_float_round_nearest_microsecond ] " ] val of_span : Span_float . t -> t [ @@ deprecated " [ since 2019 - 01 ] use [ of_span_float_round_nearest ] or \ [ of_span_float_round_nearest_microsecond ] " ] val to_span_float_round_nearest : t -> Span_float . t val to_span_float_round_nearest_microsecond : t -> Span_float . t val of_span_float_round_nearest : Span_float . t -> t val of_span_float_round_nearest_microsecond : Span_float . t -> t module Alternate_sexp : sig type nonrec t = t [ @@ deriving sexp ] end [ @@ deprecated " [ since 2018 - 04 ] use [ Span . sexp_of_t ] and [ Span . t_of_sexp ] instead " ] module Private : sig val of_parts : Parts . t -> t val to_parts : t -> Parts . t end end
module type Ofday = sig module Span : Span type t = private Int63 . t include Ofday_intf . S with type underlying = Int63 . t and type t := t and module Span := Span val approximate_end_of_day : t val add_exn : t -> Span . t -> t val sub_exn : t -> Span . t -> t val every : Span . t -> start : t -> stop : t -> t list Or_error . t end
module type Time_ns = sig module Span : Span module Ofday : Ofday with module Span := Span type t = private Int63 . t [ @@ deriving hash , typerep , bin_io ] include Comparisons . S with type t := t module Alternate_sexp : sig type nonrec t = t [ @@ deriving sexp ] end include Time_intf . Shared with type t := t with module Span := Span with module Ofday := Ofday val epoch : t val min_value_representable : t val max_value_representable : t val min_value_for_1us_rounding : t val max_value_for_1us_rounding : t val min_value : t [ @@ deprecated " [ since 2019 - 02 ] use [ min_value_representable ] or [ min_value_for_1us_rounding ] \ instead " ] val max_value : t [ @@ deprecated " [ since 2019 - 02 ] use [ max_value_representable ] or [ max_value_for_1us_rounding ] \ instead " ] val now : unit -> t val add : t -> Span . t -> t val add_saturating : t -> Span . t -> t val sub_saturating : t -> Span . t -> t val sub : t -> Span . t -> t val next : t -> t val prev : t -> t val diff : t -> t -> Span . t val abs_diff : t -> t -> Span . t val to_span_since_epoch : t -> Span . t val of_span_since_epoch : Span . t -> t val to_int63_ns_since_epoch : t -> Int63 . t val of_int63_ns_since_epoch : Int63 . t -> t val to_int_ns_since_epoch : t -> int val of_int_ns_since_epoch : int -> t val next_multiple : ? can_equal_after : bool -> base : t -> after : t -> interval : Span . t -> unit -> t val prev_multiple : ? can_equal_before : bool -> base : t -> before : t -> interval : Span . t -> unit -> t val random : ? state : Random . State . t -> unit -> t val of_time : Time_float . t -> t [ @@ deprecated " [ since 2019 - 01 ] use [ of_time_float_round_nearest ] or \ [ of_time_float_round_nearest_microsecond ] " ] val to_time : t -> Time_float . t [ @@ deprecated " [ since 2019 - 01 ] use [ to_time_float_round_nearest ] or \ [ to_time_float_round_nearest_microsecond ] " ] val to_time_float_round_nearest : t -> Time_float . t val to_time_float_round_nearest_microsecond : t -> Time_float . t val of_time_float_round_nearest : Time_float . t -> t val of_time_float_round_nearest_microsecond : Time_float . t -> t module Utc : sig val to_date_and_span_since_start_of_day : t -> Date0 . t * Span . t val of_date_and_span_since_start_of_day : Date0 . t -> Span . t -> t end module Stable : sig module Alternate_sexp : sig module V1 : Stable_without_comparator with type t = Alternate_sexp . t end module Span : sig module V2 : sig type t = Span . t [ @@ deriving hash ] type nonrec comparator_witness = Span . comparator_witness include Stable_int63able with type t := t with type comparator_witness := comparator_witness include Comparable . Stable . V1 . S with type comparable := t with type comparator_witness := comparator_witness end end module Ofday : sig module V1 : Stable_int63able with type t = Ofday . t end end end
let x = Tezos_time_measurement_runtime . Default . Time_measurement . duration " label42 " ( fun ( ) -> 40 + 2 ) ] } ) *
type measurement_key = { label : string ; metadata : expression Option . t }
type rewriter = | Duration of measurement_key * location | Duration_lwt of measurement_key * location | Timestamp_pre of measurement_key * location | Flush of location
type rewriter_constants = { fn_name : string ; attribute_name : string }
let duration key loc = Duration ( key , loc )
let duration_lwt key loc = Duration_lwt ( key , loc )
let timestamp_pre key loc = Timestamp_pre ( key , loc )
let flush loc = Flush loc
let create_rewriter_constants fn_name = let namespaced name = " time " ^ " . " ^ name in { fn_name ; attribute_name = namespaced fn_name }
let duration_constants = create_rewriter_constants " duration "
let duration_lwt_constants = create_rewriter_constants " duration_lwt "