@@ -51,6 +51,7 @@ module Text.Pandoc.Class.PandocMonad
5151 , setTranslations
5252 , translateTerm
5353 , makeCanonical
54+ , getTimestamp
5455 ) where
5556
5657import Codec.Archive.Zip
@@ -59,7 +60,8 @@ import Control.Monad.Except (MonadError (catchError, throwError),
5960import Data.Digest.Pure.SHA (sha1, showDigest)
6061import Data.Maybe (fromMaybe)
6162import Data.Time (UTCTime)
62−import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds)
63+import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds,
64+ posixSecondsToUTCTime)
6365import Data.Time.LocalTime (TimeZone, ZonedTime, utcToZonedTime)
6466import Network.URI ( escapeURIString, nonStrictRelativeTo,
6567 unEscapeString, parseURIReference, isAllowedInURI,
@@ -74,7 +76,7 @@ import Text.Pandoc.Error
7476import Text.Pandoc.Logging
7577import Text.Pandoc.MIME (MimeType, getMimeType, extensionFromMimeType)
7678import Text.Pandoc.MediaBag (MediaBag, lookupMedia)
77−import Text.Pandoc.Shared (uriPathToPath)
79+import Text.Pandoc.Shared (uriPathToPath, safeRead)
7880import Text.Pandoc.Translations (Term(..), Translations, lookupTerm,
7981 readTranslations)
8082import Text.Pandoc.Walk (walkM)
@@ -175,6 +177,21 @@ report msg = do
175177 when (level <= verbosity) $ logOutput msg
176178 modifyCommonState $ \st -> st{ stLog = msg : stLog st }
177179
180+-- | Get the time from the @SOURCE_DATE_EPOCH@
181+-- environment variable. The variable should contain a
182+-- unix time stamp, the number of seconds since midnight Jan 01
183+-- 1970 UTC. If the variable is not set or cannot be
184+-- parsed as a unix time stamp, the current time is returned.
185+-- This function is designed to make possible reproducible
186+-- builds in formats that include a creation timestamp.
187+getTimestamp :: PandocMonad m => m UTCTime
188+getTimestamp = do
189+ mbSourceDateEpoch <- lookupEnv "SOURCE_DATE_EPOCH"
190+ case mbSourceDateEpoch >>= safeRead of
191+ Just (epoch :: Integer) ->
192+ return $ posixSecondsToUTCTime $ fromIntegral epoch
193+ Nothing -> getCurrentTime
194+
178195-- | Determine whether tracing is enabled. This affects
179196-- the behavior of 'trace'. If tracing is not enabled,
180197-- 'trace' does nothing.