diff options
Diffstat (limited to 'lib/sql/date.ml')
-rw-r--r-- | lib/sql/date.ml | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/sql/date.ml b/lib/sql/date.ml new file mode 100644 index 0000000..e8933c7 --- /dev/null +++ b/lib/sql/date.ml @@ -0,0 +1,24 @@ +(** Parse a text value into a date *) + +let first_day = CalendarLib.Date.make 1899 12 30 + +let f : Sqlite3.Data.t -> Sqlite3.Data.t -> Sqlite3.Data.t = + fun str data -> + match (str, data) with + | Sqlite3.Data.TEXT format_, Sqlite3.Data.TEXT content -> ( + try + let date = CalendarLib.Printer.Date.from_fstring format_ content in + let period = + CalendarLib.Date.sub date first_day + |> CalendarLib.Date.Period.nb_days |> Int64.of_int + in + Sqlite3.Data.INT period + with + | Invalid_argument e -> + prerr_endline e; + Sqlite3.Data.NULL) + | _ -> + (* If the data is already a date, it should be preserved as is *) + data + +let register : Sqlite3.db -> unit = fun db -> Sqlite3.create_fun2 db "date" f |