byteverse / chronos

Haskell time library focusing on performance
Other
54 stars 22 forks source link

Converting a Time (or DateTime) to seconds #85

Open Kleidukos opened 10 months ago

Kleidukos commented 10 months ago

Hi, @andrewthad, I'm using cryptographic APIs that need sometimes a timestamp in seconds. Reading the API reference of Chronos it's not obvious which function I should use to convert a time or datetime object to an integer that expresses the time in seconds (instead of nanoseconds).

Is this a situation where the Torsor package is useful?

andrewthad commented 10 months ago

Unfortunately, you have to open it up with the data constructor

newtype Time = Time { getTime :: Int64 }

and then divide it by a billion. I've personally stumbled across the need for timestamps with various precisions (typically millisecond precision) several times since writing this library, and I'm not aware of a good solution to this, at least not in a way that's compatible with chronos's API.

Kleidukos commented 10 months ago

Ok, thank you! quot ns 10^9 is good enough for me. :)