if (sysPropVal != null && !sysPropVal.isEmpty()) {
writeComments(bw, sysPropVal);
} else {
- bw.write("#" + new Date());
+ bw.write("#" + getFormattedTimestamp());
bw.newLine();
}
}
}
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);
+ }
+ }
}