Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format UTCTime / NominalDiffTime into a human readable representation?

Tags:

haskell

I've tried googling about but struggling to find a concrete example or which libraries I should be using for this.

I'd like to format a value of type NominalDiffTime (NominalDiffTime) for example as 05/10/2017 10:10AM.

like image 874
Chris Stryczynski Avatar asked Jan 30 '23 11:01

Chris Stryczynski


1 Answers

Figured it out. I think there is some confusion because of the type alias: type POSIXTime = NominalDiffTime (https://hackage.haskell.org/package/time-1.8.0.3/docs/Data-Time-Clock-POSIX.html#t:POSIXTime).

import Data.Time.Clock
import Data.Time.Format
import Data.Time.Clock.POSIX

myFormatUtcTime :: UTCTime -> String
myFormatUtcTime = formatTime defaultTimeLocale "%H:%M %d/%m/%Y"

myFormatDiffTime :: NominalDiffTime -> String
myFormatDiffTime = formatTime defaultTimeLocale "%H:%M" . posixSecondsToUTCTime
like image 159
Chris Stryczynski Avatar answered May 23 '23 14:05

Chris Stryczynski