ocaml-community / calendar

OCaml library for handling dates and times.
Other
42 stars 9 forks source link

CalendarPrinter.from_fstring cannot actually parse most formats #35

Open dmbaturin opened 3 years ago

dmbaturin commented 3 years ago

The DatePrinter.from_fsrting function appears to work correctly with all possible formats.

utop # CalendarLib.Printer.DatePrinter.from_fstring "%Y-%m-%d %H:%M%z" "1970-01-01 00:00+0000" |> CalendarLib.Printer.DatePrinter.sprint "%d/%m/%Y" ;;
- : string = "01/01/1970"

If you want to handle both date and time, however... The CalendarPrinter.from_fstring function fails with a generic exception for most formats.

utop # CalendarLib.Printer.CalendarPrinter.from_fstring "%Y-%m-%d %H:%M%z" "1970-01-01 00:00+0000" |> CalendarLib.Printer.CalendarPrinter.sprint "%d/%m/%Y" ;;
Exception: Invalid_argument "Cannot create the calendar".

It only seems to works for shortcut format specifiers that include both date and time, like %c.

utop # CalendarLib.Printer.CalendarPrinter.from_fstring "%c" "Thu Sep 18 14:10:51 2003" |> CalendarLib.Printer.CalendarPrinter.sprint "%d/%m/%Y" ;;
- : string = "18/09/2003"