Description: Honour SOURCE_DATE_EPOCH
 Honour the SOURCE_DATE_EPOCH environment variable to get reproducible
 timestamps if it is set.
 See https://reproducible-builds.org/specs/source-date-epoch/
Author: Alexis Bienvenüe <pado@passoire.fr>
Forwarded: https://github.com/pyx-project/pyx/pull/3

--- a/pyx/pdfwriter.py
+++ b/pyx/pdfwriter.py
@@ -21,7 +21,7 @@
 # along with PyX; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
-import io, copy, logging, time
+import io, copy, logging, os, time
 logger = logging.getLogger("pyx")
 try:
     import zlib
@@ -162,13 +162,18 @@
         PDFobject.__init__(self, "info")
 
     def write(self, file, writer, registry):
-        if time.timezone < 0:
-            # divmod on positive numbers, otherwise the minutes have a different sign from the hours
-            timezone = "-%02i'%02i'" % divmod(-time.timezone/60, 60)
-        elif time.timezone > 0:
-            timezone = "+%02i'%02i'" % divmod(time.timezone/60, 60)
-        else:
+        if os.environ.get('SOURCE_DATE_EPOCH'):
+            creation_date = time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH')))
             timezone = "Z00'00'"
+        else:
+            if time.timezone < 0:
+                # divmod on positive numbers, otherwise the minutes have a different sign from the hours
+                timezone = "-%02i'%02i'" % divmod(-time.timezone/60, 60)
+            elif time.timezone > 0:
+                timezone = "+%02i'%02i'" % divmod(time.timezone/60, 60)
+            else:
+                timezone = "Z00'00'"
+            creation_date = time.localtime()
 
         def pdfstring(s):
             r = ""
@@ -189,7 +194,8 @@
         if writer.keywords:
             file.write("/Keywords (%s)\n" % pdfstring(writer.keywords))
         file.write("/Creator (PyX %s)\n" % version.version)
-        file.write("/CreationDate (D:%s%s)\n" % (time.strftime("%Y%m%d%H%M"), timezone))
+        file.write("/CreationDate (D:%s%s)\n" %
+                   (time.strftime("%Y%m%d%H%M",creation_date), timezone))
         file.write(">>\n")
 
 
--- a/pyx/pswriter.py
+++ b/pyx/pswriter.py
@@ -20,7 +20,7 @@
 # along with PyX; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
-import io, copy, time, math
+import io, copy, time, math, os
 from . import bbox, config, style, version, unit, trafo, writer
 
 
@@ -121,11 +121,15 @@
         self.encodings = {}
 
     def writeinfo(self, file):
+        if os.environ.get('SOURCE_DATE_EPOCH'):
+            creation_date = time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH')))
+        else:
+            creation_date = time.localtime()
         file.write("%%%%Creator: PyX %s\n" % version.version)
         if self.title is not None:
             file.write("%%%%Title: %s\n" % self.title)
         file.write("%%%%CreationDate: %s\n" %
-                   time.asctime(time.localtime(time.time())))
+                   time.asctime(creation_date))
 
     def getfontmap(self):
         if self._fontmap is None:
