dm3 / clojure.java-time

Java 8 Date-Time API for Clojure
MIT License
461 stars 45 forks source link

Unix timestamp conversion #75

Closed bdevel closed 1 year ago

bdevel commented 3 years ago

Can we add a new function, or at least an example for converting a Unix timestamp (epoch seconds) to a LocalDateTime object? That seems like a very basic operation and I wasn't able to find a single example in the docs or test cases. Stackoverflow didn't even have examples either.

Use case, getting the modification time of a file, (me.raynes.fs/mod-time "example.txt") returns epoch seconds.

dm3 commented 3 years ago

First you must get an Instant as Unix epoch doesn't have timezone information. java-time/instant constructor accepts a number of milliseconds:

user=> (java-time/instant 100)
#object[java.time.Instant 0x22964b25 "1970-01-01T00:00:00.100Z"]

Then you'll get a LocalDateTime by providing the instant and the time zone to the local-date-time constructor: (j/local-date-time inst "UTC").

Happy to accept a PR with doc improvements.