A CLI and OCaml library for managing contacts

trim

-96
-44
lib/schema/sortal_schema_date.ml.unused
··· 1 - type t = Ptime.date 2 - 3 - let parse s = 4 - (* Try parsing as RFC 3339 (which may include time) *) 5 - match Ptime.of_rfc3339 s with 6 - | Ok (ptime, _, _) -> Some (Ptime.to_date ptime) 7 - | Error _ -> 8 - (* Try parsing as just a date string YYYY-MM-DD *) 9 - match String.split_on_char '-' s with 10 - | [year_s; month_s; day_s] -> ( 11 - try 12 - let year = int_of_string year_s in 13 - let month = int_of_string month_s in 14 - let day = int_of_string day_s in 15 - if month >= 1 && month <= 12 && day >= 1 && day <= 31 then 16 - Some (year, month, day) 17 - else None 18 - with Failure _ -> None) 19 - | _ -> None 20 - 21 - let format (year, month, day) = 22 - Printf.sprintf "%04d-%02d-%02d" year month day 23 - 24 - let pp ppf t = Format.pp_print_string ppf (format t) 25 - 26 - let to_string = format 27 - let of_string = parse 28 - 29 - let today () = 30 - let open Unix in 31 - let tm = localtime (time ()) in 32 - (tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday) 33 - 34 - let jsont = 35 - let kind = "ISO 8601 date" in 36 - let doc = "An ISO 8601 date string (YYYY-MM-DD)" in 37 - let dec s = 38 - match parse s with 39 - | Some t -> t 40 - | None -> 41 - Jsont.Error.msgf Jsont.Meta.none "%s: invalid ISO 8601 date: %S" kind s 42 - in 43 - let enc = format in 44 - Jsont.map ~kind ~doc ~dec ~enc Jsont.string
-52
lib/schema/sortal_schema_date.mli.unused
··· 1 - (** ISO 8601 date handling for contact data. 2 - 3 - Provides parsing, formatting, and jsont combinators for ISO 8601 dates. 4 - Time components in parsed strings are ignored, only the date is extracted. 5 - 6 - @see <https://www.iso.org/iso-8601-date-and-time-format.html> ISO 8601 *) 7 - 8 - type t = Ptime.date 9 - (** [t] represents a date as (year, month, day) tuple. *) 10 - 11 - val jsont : t Jsont.t 12 - (** [jsont] is a bidirectional JSON type for ISO 8601 dates. 13 - 14 - On decode: accepts JSON strings in ISO 8601 date format (e.g., "2024-11-03") 15 - or RFC 3339 format with times (e.g., "2024-11-03T10:30:00Z"), extracting 16 - only the date component. 17 - 18 - On encode: produces dates in YYYY-MM-DD format. 19 - 20 - {b Example:} 21 - {[ 22 - let date = (2024, 11, 3) in 23 - Jsont_bytesrw.encode_string Date.jsont date 24 - (* Returns: "2024-11-03" *) 25 - ]} *) 26 - 27 - val parse : string -> t option 28 - (** [parse s] parses an ISO 8601 date string. 29 - 30 - Accepts various formats: 31 - - "2024-11-03" (date only) 32 - - "2024-11-03T10:30:00Z" (RFC 3339 with time, extracts date) 33 - - "2024-11-03T10:30:00-08:00" (with timezone, extracts date) 34 - 35 - Returns [None] if the string is not a valid date format. *) 36 - 37 - val format : t -> string 38 - (** [format t] formats a date as ISO 8601 (YYYY-MM-DD). 39 - 40 - {b Example output:} ["2024-11-03"] *) 41 - 42 - val pp : Format.formatter -> t -> unit 43 - (** [pp ppf t] pretty prints a date in ISO 8601 format. *) 44 - 45 - val to_string : t -> string 46 - (** [to_string t] is an alias for {!format}. *) 47 - 48 - val of_string : string -> t option 49 - (** [of_string s] is an alias for {!parse}. *) 50 - 51 - val today : unit -> t 52 - (** [today ()] returns today's date in the local timezone. *)