It seems like the main popular libraries for handling dates/times in OCaml are Calendar and Ptime, but having experimented with them, they don’t handle parsing/printing out UTC timestamps RFC5322 formatted times.
I was able to find a parser for UTC timestamps RFC5322 formatted times that I can vendor from the OCaml-webmachine repository, but not yet sure how to print them at the moment:
Does there exist an out-of-the-box library for this case?
If not, how can I pretty print UTC timestamps RFC5322 formatted times using Ptime or Calendar?
Note sure exactly what you mean by “UTC timestamps” but given your link, why did you think it would ?
Ptime never claimed to support something else than RFC 3339 timestamps, I don’t see what’s incorrect here. Your format is simply not supported by Ptime. (Sorry to be so nitpicky on these words).
If you can use ISO timestamps (toISOString) instead, there’s a library
called ISO8601 to parse them. They might also be in the more specific
subset that Ptime.of_rfc3339 accepts.
Thanks for the pointers to the functions -barring no out-of-the-box library, I did plan to pretty print them myself, so these references will likely cut down the time I spend on implementation.
I really don’t suppose it would be too difficult to print out the timestamps myself, it’s just that it would require reading and implementing the RFC, and then worrying about if I had handled all the edge cases myself, hence I was asking whether there was any existing library/prior project that had ran into this issue already.
Sorry to nitpick on your response - I wasn’t trying to pick on Ptime in particular or trying to make the claim that it should support conversions to this format, my reason for referencing Ptime was simply because it seemed to be a popular choice for encoding times in OCaml (hence why I also pointed to calendar).
From searching online, it seems many other programming languages have conversion functions to this format as part of their ecosystem, so I wanted to double check whether others had run into this problem before, because it could be very likely that there is some implementation of this function hiding in some existing OCaml codebase somewhere that I may have missed.
I’m parsing/consuming the output of external servers, so I’m hesitant to change the date format in case other things fail.
Okay, reading the MDN page I linked to, if the documentation is accurate, then I suppose it won’t be too much hassle to write my own printer, and I can just leech of the work of the OCaml-webmachine people (thx) for the parser.
Nice @yawaramin. I bet though that the problem is that for parsing you might not face only GMT timestamps, and then you need get into the business of interpreting named time zones…
For machines these timestamps with non-normalized data and named time zones should really die.
Yeah in retrospect I probably jumped too quickly to comment. I see now webmachine is basically using scanf with some more transformation and error checking.
I still don’t know what a “UTC timestamp” really is, but you might want to also consider timedesc, part of timere. It parses and prints ISO 8601, supports time zones, includes other representations such as Date and ISO_week, and can be extended with timere to add powerful functionality for reasoning about time.
Prior to 1995, there were three different formats commonly used by servers to communicate timestamps. For compatibility with old implementations, all three are defined here.
The preferred format is a fixed-length and single-zone subset of the date and time specification used by the Internet Message Format [RFC5322].
HTTP-date = IMF-fixdate / obs-date
An example of the preferred format is
Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
I guess RFC5322 is the particular time format that I was trying to handle.
Ohh. yes, that would be great! It was pretty disheartening to run into the problem of trying to handle what I thought was a pretty common time format (as many programming languages have easy to access libraries to deal with this format), ask on the OCaml discuss, only to be sidetracked into discussing the semantics of what format timestamps should be encoded in at all.
Any other date time formats you find missing in OCaml ecosystem? (Takes a while to test before release, so might as well add more than just a single format at a time.)