File: reproducible-properties-timestamp.diff

package info (click to toggle)
openjdk-11 11.0.29~4ea-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 780,672 kB
  • sloc: java: 5,208,618; xml: 1,192,267; cpp: 1,138,375; ansic: 461,923; javascript: 162,416; sh: 16,738; objc: 13,729; python: 4,757; asm: 3,570; makefile: 2,970; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (52 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (4)
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
50
51
52
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
@@ -53,6 +53,9 @@
 import sun.nio.cs.UTF_8;
 import sun.nio.cs.ISO_8859_1;
 
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
 /**
  * The {@code Properties} class represents a persistent set of
  * properties. The {@code Properties} can be saved to a stream
@@ -929,7 +932,7 @@
         if (comments != null) {
             writeComments(bw, comments);
         }
-        bw.write("#" + new Date().toString());
+        bw.write("#" + getFormattedTimestamp());
         bw.newLine();
         synchronized (this) {
             for (Map.Entry<Object, Object> e : entrySet()) {
@@ -1579,4 +1582,27 @@
         }
         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 String getFormattedTimestamp() {
+        String epoch = AccessController.doPrivileged(new PrivilegedAction<String>(){
+            public String run() { return 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);
+        }
+    }
 }