Skip to content

Commit

Permalink
Added of_date_and_time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenldl committed Apr 5, 2023
1 parent 796ba40 commit 1a2ceb2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
25 changes: 23 additions & 2 deletions timedesc/date_time.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

Expand All @@ -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)))
Expand Down
30 changes: 30 additions & 0 deletions timedesc/timedesc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a2ceb2

Please sign in to comment.