From: Emmanuel Bourg Date: Wed, 20 Jul 2022 16:04:41 +0000 (+0100) Subject: Makes the timestamp in the properties files header reproducible when SOURCE_DATE_EPOC... X-Git-Tag: archive/raspbian/17.0.6+10-1+rpi1~1^2~11 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=7ae1243a2c2ebd6791a9045c6d78d480f3d59640;p=openjdk-17.git Makes the timestamp in the properties files header reproducible when SOURCE_DATE_EPOCH is specified Forwarded: no Gbp-Pq: Name reproducible-properties-timestamp.diff --- diff --git a/src/java.base/share/classes/java/util/Properties.java b/src/java.base/share/classes/java/util/Properties.java index 21b2982b8..9e466b9fb 100644 --- a/src/java.base/share/classes/java/util/Properties.java +++ b/src/java.base/share/classes/java/util/Properties.java @@ -903,7 +903,7 @@ public class Properties extends Hashtable { if (comments != null) { writeComments(bw, comments); } - bw.write("#" + new Date().toString()); + bw.write("#" + getFormattedTimestamp()); bw.newLine(); synchronized (this) { for (Map.Entry e : entrySet()) { @@ -1555,4 +1555,22 @@ public class Properties extends Hashtable { } this.map = map; } + + /** + * Returns a formatted timestamp to be used in the properties file header. + * The date used is the current date, unless the SOURCE_DATE_EPOCH + * environment variable is specified. In this case the format used is + * locale and timezone insensitive to ensure the output is reproducible. + */ + private String getFormattedTimestamp() { + if (System.getenv("SOURCE_DATE_EPOCH") == null) { + return new Date().toString(); + } else { + // Use the SOURCE_DATE_EPOCH timestamp and make the format locale/timezone insensitive + java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", java.util.Locale.ENGLISH); + fmt.setTimeZone(java.util.TimeZone.getTimeZone("UTC")); + Date date = new Date(1000 * Long.parseLong(System.getenv("SOURCE_DATE_EPOCH"))); + return fmt.format(date); + } + } }