From 51dac66e378c0c5ed79110d7bad3648c321b65c7 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Sat, 10 Aug 2024 10:08:49 +0200 Subject: [PATCH] Makes the timestamp in the properties files header reproducible when SOURCE_DATE_EPOCH is specified Forwarded: no Gbp-Pq: Name reproducible-properties-timestamp.diff --- .../share/classes/java/util/Properties.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/java/util/Properties.java b/src/java.base/share/classes/java/util/Properties.java index 8d38b9df9..3d89e6704 100644 --- a/src/java.base/share/classes/java/util/Properties.java +++ b/src/java.base/share/classes/java/util/Properties.java @@ -955,7 +955,7 @@ public class Properties extends Hashtable { if (sysPropVal != null && !sysPropVal.isEmpty()) { writeComments(bw, sysPropVal); } else { - bw.write("#" + new Date()); + bw.write("#" + getFormattedTimestamp()); bw.newLine(); } } @@ -1600,4 +1600,23 @@ 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 static String getFormattedTimestamp() { + String epoch = System.getenv("SOURCE_DATE_EPOCH"); + if (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(epoch)); + return fmt.format(date); + } + } } -- 2.30.2