I'm getting quite lost in all the different date and time libraries available to me in Haskell. What I'm trying to do is format the file modification time as "yyyy-MM-dd". Can someone show me how to do that?
I've got this code-snippet:
main = do
times <- mapM getModificationTime $ dataFiles config -- dataFiles returns [FilePath].
-- I'd like to do something like the following:
(putStr . unlines . map formatDate) times
This is what I've attempted. Please don't laugh!
formatDate t =
let (year, month, day) = toGregorian $ utctDay t
in intercalate "-" $ map show [year, fromIntegral month, fromIntegral day]
I know it's not completely up to my specifications yet, because I won't get months and days left-filled with zero. I can fix that. But I figure there's a more elegant way to get what I'm after, which is why I've titled my post with what I'm after rather than the current error I'm getting, which, for the sake of completeness is Couldn't match expected type 'UTCTime' with actual type 'ClockTime'
.
Thanks for whatever help you can give or clarity you can provide.
Something like this should convert the ClockTime
into a UTCTime
:
import System.Time
import Data.Time.Clock.POSIX
utcTimeFromClockTime :: ClockTime -> UTCTime
utcTimeFromClockTime (TOD seconds picoseconds)
= posixSecondsToUTCTime . fromRational
$ fromInteger seconds + fromInteger picoseconds / 1000000000000
You probably want to convert that to use your local time zone. Use utcToLocalZonedTime :: UTCTime -> IO ZonedTime
from Data.Time.LocalTime.
Either way, you then have a timestamp you can format:
import Data.Time.Format
import System.Locale
formatIsoDate :: FormatTime t => t -> String
formatIsoDate = formatTime defaultTimeLocale "%F"
(Sorry, I've not tested any of this code.)