Skip to content

Commit

Permalink
Run through ocamlformat
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanlong committed Jun 21, 2021
1 parent e3cef33 commit 3c4fc65
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
24 changes: 12 additions & 12 deletions pgx_value_ptime/src/pgx_value_ptime.ml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
include Pgx.Value


let of_date (year, month, day) =
Printf.sprintf "%04d-%02d-%02d" year month day |> Pgx.Value.of_string
;;

let to_date' text =
match text ^ "T00:00:00Z" |> Ptime.of_rfc3339 with
| Result.Ok (t, _, _) -> Ptime.to_date t
| _ -> convert_failure "date" text
;;

let to_date_exn v = Pgx.Value.to_string_exn v |> to_date'
let to_date v = Pgx.Value.to_string v |> Option.map to_date'


let of_time ?tz_offset_s t =
let tz_offset_s = Option.value tz_offset_s ~default:0 in
Ptime.to_rfc3339 ~tz_offset_s ~frac_s:12 t |> Pgx.Value.of_string

;;

let time_of_string text =
match Ptime.of_rfc3339 text with
| Result.Ok (t, offset, _) -> (t, Option.value ~default:0 offset)
| Result.Ok (t, offset, _) -> t, Option.value ~default:0 offset
| _ -> convert_failure "time" text

;;

let to_time' text =
(*
Expand All @@ -46,13 +46,13 @@ let to_time' text =
let localtz = seq [ tz; char ':'; digit; digit; eol ] |> compile in
let localtz_no_min = seq [ tz; eol ] |> compile in
time_of_string
@@
match matches utctz text, matches localtz text, matches localtz_no_min text with
| [], [], [] -> text ^ "Z"
| _, _, [] -> text
| [], [], _ -> text ^ ":00"
| _ -> convert_failure "time" text

@@
match matches utctz text, matches localtz text, matches localtz_no_min text with
| [], [], [] -> text ^ "Z"
| _, _, [] -> text
| [], [], _ -> text ^ ":00"
| _ -> convert_failure "time" text
;;

let to_time_exn v = Pgx.Value.to_string_exn v |> to_time'
let to_time v = Pgx.Value.to_string v |> Option.map to_time'
2 changes: 0 additions & 2 deletions pgx_value_ptime/src/pgx_value_ptime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ include module type of Pgx.Value with type v := v and type t := t
val of_date : Ptime.date -> t
val to_date_exn : t -> Ptime.date
val to_date : t -> Ptime.date option

val of_time : ?tz_offset_s:Ptime.tz_offset_s -> Ptime.t -> t
val to_time_exn : t -> Ptime.t * Ptime.tz_offset_s
val to_time : t -> (Ptime.t * Ptime.tz_offset_s) option

val time_of_string : string -> Ptime.t * Ptime.tz_offset_s
85 changes: 51 additions & 34 deletions pgx_value_ptime/test/test_pgx_value_ptime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,106 @@ let print_time (t, tz_offset_s) =
let sec = Ptime.to_float_s t
and txt = Ptime.to_rfc3339 t ~tz_offset_s ~frac_s:6 in
Printf.sprintf "<%s | Seconds: %f, Offset: %d>" txt sec tz_offset_s
;;

let value_testable =
let print_time value = match Pgx.Value.to_string value with
let print_time value =
match Pgx.Value.to_string value with
| Some text -> text
| None -> "<None>" in
| None -> "<None>"
in
let formatter ppf value = Format.pp_print_string ppf (print_time value) in
Alcotest.testable formatter (=)

Alcotest.testable formatter ( = )
;;

let check_value = Alcotest.check value_testable


let test_to_date _ =
let check_date = Alcotest.(check (triple int int int)) in
let value = Pgx.Value.of_string "2021-11-14" in
let expected = (2021, 11, 14) in
let expected = 2021, 11, 14 in
check_date "check date parsing" expected (Value.to_date_exn value);
let value = Pgx.Value.of_string "0900-06-13" in
let expected = (900, 6, 13) in
let expected = 900, 6, 13 in
check_date "check date with leading zeros" expected (Value.to_date_exn value)

;;

let test_of_date _ =
let date = (2021, 11, 14) in
let date = 2021, 11, 14 in
let expected = Pgx.Value.of_string "2021-11-14" in
check_value "check date rendering" expected (Value.of_date date);
let date = (900, 6, 13) in
let date = 900, 6, 13 in
let expected = Pgx.Value.of_string "0900-06-13" in
check_value "dates with leading zeros render properly" expected (Value.of_date date)

;;

let date_tests =
[
Alcotest.test_case "of_date renders a Ptime date to a Pgx Value" `Quick test_of_date;
Alcotest.test_case "to_date parses a Pgx Value to a Ptime date" `Quick test_to_date
[ Alcotest.test_case "of_date renders a Ptime date to a Pgx Value" `Quick test_of_date
; Alcotest.test_case "to_date parses a Pgx Value to a Ptime date" `Quick test_to_date
]

;;

(* Show only the human-readable version of the date-time. *)
let check_time =
let compare_times (t1, o1) (t2, o2) =
let tf1 = Ptime.to_float_s t1
and tf2 = Ptime.to_float_s t2 in
(abs_float (tf1 -. tf2) < 1.0e-6) && (o1 = o2) in
abs_float (tf1 -. tf2) < 1.0e-6 && o1 = o2
in
let time_testable =
Alcotest.testable (fun ppf t -> Format.pp_print_string ppf (print_time t)) compare_times in
Alcotest.testable
(fun ppf t -> Format.pp_print_string ppf (print_time t))
compare_times
in
Alcotest.check time_testable

;;

let test_time_of_string _ =
let hour = 3600 in
let hourf = 3600. in
let to_pt x = Ptime.of_float_s x |> Option.value ~default:Ptime.min in
check_time "minimum time parses" (Ptime.min, 0) (Value.time_of_string "0000-01-01T00:00:00Z");
check_time
"minimum time parses"
(Ptime.min, 0)
(Value.time_of_string "0000-01-01T00:00:00Z");
let pt = to_pt (12. *. hourf) in
check_time "time with tz offset parses" (pt, ~-4 * hour) (Value.time_of_string "1970-01-01T08:00:00-04:00");
let pt = to_pt (12. *. hourf +. 0.12345) in
check_time "a time with milliseconds parses" (pt, 0) (Value.time_of_string "1970-01-01T12:00:00.12345Z");
check_time
"time with tz offset parses"
(pt, ~-4 * hour)
(Value.time_of_string "1970-01-01T08:00:00-04:00");
let pt = to_pt ((12. *. hourf) +. 0.12345) in
check_time
"a time with milliseconds parses"
(pt, 0)
(Value.time_of_string "1970-01-01T12:00:00.12345Z");
(* On linux, one can run "TZ='UTC' date -d @1458086118" in a shell to confirm this conversion is correct.*)
check_time "a recent time parses" (to_pt 1458086118., ~-4 * hour) (Value.time_of_string "2016-03-15 19:55:18-04:00")

check_time
"a recent time parses"
(to_pt 1458086118., ~-4 * hour)
(Value.time_of_string "2016-03-15 19:55:18-04:00")
;;

let time_roundtrip str = Value.of_string str |> Value.to_time_exn

let test_time_tz_handling _ =
let (utc_t, tz_offset_s) = Value.time_of_string "2016-03-15 19:55:18-04:00" in
let utc_t, tz_offset_s = Value.time_of_string "2016-03-15 19:55:18-04:00" in
check_time "without TZ" (utc_t, 0) (time_roundtrip "2016-03-15 23:55:18");
check_time "zulu" (utc_t, 0) (time_roundtrip "2016-03-15 23:55:18Z");
check_time "hour TZ" (utc_t, tz_offset_s) (time_roundtrip "2016-03-15 19:55:18-04");
check_time "full TZ" (utc_t, tz_offset_s) (time_roundtrip "2016-03-15 19:55:18-04:00")

;;

let test_time_conversion_roundtrip _ =
let print_time (t, tz_offset_s) = Ptime.to_rfc3339 t ~tz_offset_s ~frac_s:6 in
let expected_str = "2016-03-15T23:55:18.123456Z" in
Alcotest.(check string) "parse-print" expected_str (time_roundtrip expected_str |> print_time);
let (t, tz_offset_s) = Value.time_of_string expected_str in
Alcotest.(check string)
"parse-print"
expected_str
(time_roundtrip expected_str |> print_time);
let t, tz_offset_s = Value.time_of_string expected_str in
let actual = Value.of_time t ~tz_offset_s |> Value.to_time_exn in
check_time "print-parse" (t, tz_offset_s) actual

;;

let time_tests =
[ Alcotest.test_case "test time_of_string" `Quick test_time_of_string
Expand All @@ -98,9 +118,6 @@ let time_tests =
`Quick
test_time_conversion_roundtrip
]
;;


let () = Alcotest.run "pgx_async_conversions" [
"date", date_tests;
"time", time_tests
]
let () = Alcotest.run "pgx_async_conversions" [ "date", date_tests; "time", time_tests ]

0 comments on commit 3c4fc65

Please sign in to comment.