diff options
Diffstat (limited to 'lib/kernel')
| -rw-r--r-- | lib/kernel/date.ml | 25 | ||||
| -rw-r--r-- | lib/kernel/date.mli | 15 | ||||
| -rw-r--r-- | lib/kernel/dune | 5 |
3 files changed, 42 insertions, 3 deletions
diff --git a/lib/kernel/date.ml b/lib/kernel/date.ml index 525b35b..cf5aabd 100644 --- a/lib/kernel/date.ml +++ b/lib/kernel/date.ml @@ -1,4 +1,4 @@ -type weekday = Sun | Mon | Tue | Wed | Thu | Fri | Sat +type weekday = Sun | Mon | Tue | Wed | Thu | Fri | Sat [@@deriving sexp] (* Internal representation: rata die = days since 1970-01-01 (proleptic Gregorian). Howard Hinnant's civil<->days algorithm; OCaml's `/` truncates @@ -56,3 +56,26 @@ let weekday t = (* rata 0 = 1970-01-01 = Thursday (index 4 with Sun=0). *) let w = (((t + 4) mod 7) + 7) mod 7 in [| Sun; Mon; Tue; Wed; Thu; Fri; Sat |].(w) + +let weekday_to_string = function + | Sun -> "sunday" | Mon -> "monday" | Tue -> "tuesday" | Wed -> "wednesday" + | Thu -> "thursday" | Fri -> "friday" | Sat -> "saturday" + +let to_iso8601 t = Printf.sprintf "%04d-%02d-%02d" (year t) (month t) (day t) + +let of_iso8601 s = + match String.split_on_char '-' s with + | [ y; m; d ] + when String.length y = 4 && String.length m = 2 && String.length d = 2 -> ( + match (int_of_string_opt y, int_of_string_opt m, int_of_string_opt d) with + | Some year, Some month, Some day -> make ~year ~month ~day + | _ -> Error (Printf.sprintf "date %S: not an ISO-8601 date" s)) + | _ -> Error (Printf.sprintf "date %S: expected YYYY-MM-DD" s) + +let sexp_of_t t = Sexplib0.Sexp_conv.sexp_of_string (to_iso8601 t) + +let t_of_sexp sexp = + let s = Sexplib0.Sexp_conv.string_of_sexp sexp in + match of_iso8601 s with + | Ok t -> t + | Error msg -> Sexplib0.Sexp_conv.of_sexp_error msg sexp diff --git a/lib/kernel/date.mli b/lib/kernel/date.mli index b684d61..2f632c8 100644 --- a/lib/kernel/date.mli +++ b/lib/kernel/date.mli @@ -3,7 +3,7 @@ arithmetic and comparison are total and cheap. *) type t -type weekday = Sun | Mon | Tue | Wed | Thu | Fri | Sat +type weekday = Sun | Mon | Tue | Wed | Thu | Fri | Sat [@@deriving sexp] (** [make ~year ~month ~day] validates the date; rejects a month/day out of range and any year outside 1583..9999. *) @@ -21,3 +21,16 @@ val to_rata : t -> int val of_rata : int -> t val add_days : t -> int -> t val compare : t -> t -> int + +(** ISO-8601 rendering and validating parsing. [of_iso8601] enforces the same + domain as {!make}. *) +val to_iso8601 : t -> string +val of_iso8601 : string -> (t, string) result + +val weekday_to_string : weekday -> string + +(** Sexp form is an ISO-8601 atom, e.g. [2026-04-05]. [t_of_sexp] validates and + raises [Of_sexp_error] on a malformed or out-of-domain date; loaders convert + that to a [result]. *) +val t_of_sexp : Sexplib0.Sexp.t -> t +val sexp_of_t : t -> Sexplib0.Sexp.t diff --git a/lib/kernel/dune b/lib/kernel/dune index b63c476..b909ac5 100644 --- a/lib/kernel/dune +++ b/lib/kernel/dune @@ -1,2 +1,5 @@ (library - (name colitur_kernel)) + (name colitur_kernel) + (libraries sexplib) + (preprocess + (pps ppx_sexp_conv))) |
