summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2022-10-19 21:16:26 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2022-10-19 21:20:50 -0700
commit5d71276ecadf9201b9548d82bb908077d28ad27d (patch)
tree6c138a9ab988c9b194fb4cb082bf7b99291b4fd6 /src
parentdb189498a85104964f7aec43c8939bd2af8c1d33 (diff)
T.P.Class: make `getPOSIXTime`, `getZonedTime` sensitive to...
`SOURCE_DATE_EPOCH` environment variable if set. (`getTimestamp` was already sensitive.) This ensures that EPUB builds are reproducible. Closes #7093.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Class/PandocMonad.hs39
-rw-r--r--src/Text/Pandoc/Data.hs2
2 files changed, 20 insertions, 21 deletions
diff --git a/src/Text/Pandoc/Class/PandocMonad.hs b/src/Text/Pandoc/Class/PandocMonad.hs
index 4ead0340d..57a65abc8 100644
--- a/src/Text/Pandoc/Class/PandocMonad.hs
+++ b/src/Text/Pandoc/Class/PandocMonad.hs
@@ -23,6 +23,7 @@ and writers.
module Text.Pandoc.Class.PandocMonad
( PandocMonad(..)
+ , getTimestamp
, getPOSIXTime
, getZonedTime
, readFileFromDirs
@@ -50,7 +51,6 @@ module Text.Pandoc.Class.PandocMonad
, toLang
, makeCanonical
, findFileWithDataFallback
- , getTimestamp
, checkUserDataDir
) where
@@ -172,21 +172,6 @@ report msg = do
when (level <= verbosity) $ logOutput msg
modifyCommonState $ \st -> st{ stLog = msg : stLog st }
--- | Get the time from the @SOURCE_DATE_EPOCH@
--- environment variable. The variable should contain a
--- unix time stamp, the number of seconds since midnight Jan 01
--- 1970 UTC. If the variable is not set or cannot be
--- parsed as a unix time stamp, the current time is returned.
--- This function is designed to make possible reproducible
--- builds in formats that include a creation timestamp.
-getTimestamp :: PandocMonad m => m UTCTime
-getTimestamp = do
- mbSourceDateEpoch <- lookupEnv "SOURCE_DATE_EPOCH"
- case mbSourceDateEpoch >>= safeRead of
- Just (epoch :: Integer) ->
- return $ posixSecondsToUTCTime $ fromIntegral epoch
- Nothing -> getCurrentTime
-
-- | Determine whether tracing is enabled. This affects
-- the behavior of 'trace'. If tracing is not enabled,
-- 'trace' does nothing.
@@ -256,14 +241,28 @@ getResourcePath = getsCommonState stResourcePath
setResourcePath :: PandocMonad m => [FilePath] -> m ()
setResourcePath ps = modifyCommonState $ \st -> st{stResourcePath = ps}
--- | Get the POSIX time.
+-- | Get the current UTC time. If the @SOURCE_DATE_EPOCH@ environment
+-- variable is set to a unix time (number of seconds since midnight
+-- Jan 01 1970 UTC), it is used instead of the current time, to support
+-- reproducible builds.
+getTimestamp :: PandocMonad m => m UTCTime
+getTimestamp = do
+ mbSourceDateEpoch <- lookupEnv "SOURCE_DATE_EPOCH"
+ case mbSourceDateEpoch >>= safeRead of
+ Just (epoch :: Integer) ->
+ return $ posixSecondsToUTCTime $ fromIntegral epoch
+ Nothing -> getCurrentTime
+
+-- | Get the POSIX time. If @SOURCE_DATE_EPOCH@ is set to a unix time,
+-- it is used instead of the current time.
getPOSIXTime :: PandocMonad m => m POSIXTime
-getPOSIXTime = utcTimeToPOSIXSeconds <$> getCurrentTime
+getPOSIXTime = utcTimeToPOSIXSeconds <$> getTimestamp
--- | Get the zoned time.
+-- | Get the zoned time. If @SOURCE_DATE_EPOCH@ is set to a unix time,
+-- value (POSIX time), it is used instead of the current time.
getZonedTime :: PandocMonad m => m ZonedTime
getZonedTime = do
- t <- getCurrentTime
+ t <- getTimestamp
tz <- getCurrentTimeZone
return $ utcToZonedTime tz t
diff --git a/src/Text/Pandoc/Data.hs b/src/Text/Pandoc/Data.hs
index 66821f5b8..6644428d7 100644
--- a/src/Text/Pandoc/Data.hs
+++ b/src/Text/Pandoc/Data.hs
@@ -202,7 +202,7 @@ getDefaultReferencePptx = do
]
let toLazy = BL.fromChunks . (:[])
let pathToEntry path = do
- epochtime <- floor . utcTimeToPOSIXSeconds <$> getCurrentTime
+ epochtime <- floor <$> getPOSIXTime
contents <- toLazy <$> readDataFile ("pptx/" ++ path)
return $ toEntry path epochtime contents
datadir <- getUserDataDir