From 1a2ceb234dfb24bb240f8619a0d318b15427ce84 Mon Sep 17 00:00:00 2001 From: Darren Li Date: Wed, 5 Apr 2023 11:21:28 +1000 Subject: [PATCH] Added of_date_and_time functions --- timedesc/date_time.ml | 25 +++++++++++++++++++++++-- timedesc/timedesc.mli | 30 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/timedesc/date_time.ml b/timedesc/date_time.ml index c05122eb..91789ff9 100644 --- a/timedesc/date_time.ml +++ b/timedesc/date_time.ml @@ -327,6 +327,27 @@ module Ymd_date_time = struct Span.For_human'.(offset.hours) Span.For_human'.(offset.minutes) + let of_date_and_time ?tz date time : (t, error) result = + match Zoneless'.to_zoned ?tz { date; time } with + | Error e -> Error (e :> error) + | Ok dt -> Ok dt + + let of_date_and_time_exn ?tz date time : t = + match of_date_and_time ?tz date time with + | Ok x -> x + | Error e -> raise (Error_exn e) + + let of_date_and_time_unambiguous ?tz ~offset_from_utc date time + : (t, error) result = + match Zoneless'.to_zoned_unambiguous ?tz ~offset_from_utc { date; time } with + | Error e -> Error (e :> error) + | Ok dt -> Ok dt + + let of_date_and_time_unambiguous_exn ?tz ~offset_from_utc date time : t = + match of_date_and_time_unambiguous ?tz ~offset_from_utc date time with + | Ok x -> x + | Error e -> raise (Error_exn e) + let make ?tz ?ns ?s_frac ~year ~month ~day ~hour ~minute ~second () : (t, error) result = match Date.Ymd'.make ~year ~month ~day with @@ -335,7 +356,7 @@ module Ymd_date_time = struct match Time.make ~hour ~minute ~second ?ns ?s_frac () with | Error e -> Error (e :> error) | Ok time -> ( - match Zoneless'.to_zoned ?tz { date; time } with + match of_date_and_time ?tz date time with | Error e -> Error (e :> error) | Ok dt -> Ok dt)) @@ -353,7 +374,7 @@ module Ymd_date_time = struct | Error e -> Error (e :> error) | Ok time -> ( match - Zoneless'.to_zoned_unambiguous ?tz ~offset_from_utc { date; time } + of_date_and_time_unambiguous ?tz ~offset_from_utc date time with | Ok dt -> Ok dt | Error e -> Error (e :> error))) diff --git a/timedesc/timedesc.mli b/timedesc/timedesc.mli index f5d68f5e..e39248d0 100644 --- a/timedesc/timedesc.mli +++ b/timedesc/timedesc.mli @@ -1269,6 +1269,36 @@ val make_unambiguous_exn : t (** @raise Error_exn if [make_umabiguous] fails *) +val of_date_and_time : + ?tz:Time_zone.t -> + Date.t -> + Time.t -> + (t, error) result +(** See {!val:make} for details. *) + +val of_date_and_time_exn : + ?tz:Time_zone.t -> + Date.t -> + Time.t -> + t +(** @raise Error_exn if [of_date_and_time_exn] fails *) + +val of_date_and_time_unambiguous : + ?tz:Time_zone.t -> + offset_from_utc:Span.t -> + Date.t -> + Time.t -> + (t, error) result +(** See {!val:make_unambiguous} for details. *) + +val of_date_and_time_unambiguous_exn : + ?tz:Time_zone.t -> + offset_from_utc:Span.t -> + Date.t -> + Time.t -> + t +(** @raise Error_exn if [of_date_and_time_unambiguous_exn] fails *) + (** {2 Accessors} *) val date : t -> Date.t