File: reproducible-properties-timestamp.diff

package info (click to toggle)
openjdk-25 25~15ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 832,092 kB
  • sloc: java: 5,679,805; cpp: 1,330,552; xml: 1,320,570; ansic: 487,102; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,105; python: 8,227; makefile: 2,503; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24
file content (49 lines) | stat: -rw-r--r-- 2,195 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Description: Makes the timestamp in the properties files header reproducible when SOURCE_DATE_EPOCH is specified
Author: Emmanuel Bourg <ebourg@apache.org>
Forwarded: no
--- a/src/java.base/share/classes/java/util/Properties.java
+++ b/src/java.base/share/classes/java/util/Properties.java
@@ -41,6 +41,8 @@ import java.io.UnsupportedEncodingExcept
 import java.nio.charset.Charset;
 import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.UnsupportedCharsetException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
@@ -956,7 +958,7 @@ public class Properties extends Hashtabl
         if (sysPropVal != null && !sysPropVal.isEmpty()) {
             writeComments(bw, sysPropVal);
         } else {
-            bw.write("#" + new Date());
+            bw.write("#" + getFormattedTimestamp());
             bw.newLine();
         }
     }
@@ -1601,4 +1603,25 @@ public class Properties extends Hashtabl
         }
         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.
+     */
+    @SuppressWarnings("removal")
+    private static String getFormattedTimestamp() {
+        final String epoch = AccessController.doPrivileged(
+            (PrivilegedAction<String>)() -> 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);
+        }
+    }
 }