Skip to content

Commit e2da9ac

Browse files
committed
Avoid non-portable uses of date utility
On some platforms, the date utility doesn't support the "%s" format specifier or supports neither the "-f" option nor the "-j" option. Use the boot jdk to run a java program instead. Signed-off-by: Keith W. Campbell <[email protected]>
1 parent ddd6285 commit e2da9ac

File tree

4 files changed

+112
-30
lines changed

4 files changed

+112
-30
lines changed

make/InitSupport.gmk

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
# questions.
2424
#
2525

26+
# ===========================================================================
27+
# (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
28+
# ===========================================================================
29+
2630
################################################################################
2731
# This file contains helper functions for Init.gmk.
2832
# It is divided in two parts, depending on if a SPEC is present or not
@@ -310,18 +314,10 @@ else # $(HAS_SPEC)=true
310314
# level of reproducible builds
311315
define SetupReproducibleBuild
312316
ifeq ($$(SOURCE_DATE), updated)
313-
SOURCE_DATE := $$(shell $$(DATE) +"%s")
317+
SOURCE_DATE := $$(shell $$(JAVA) $$(TOPDIR)/make/src/classes/DateUtil.java)
314318
endif
315319
export SOURCE_DATE_EPOCH := $$(SOURCE_DATE)
316-
ifeq ($$(IS_GNU_DATE), yes)
317-
export SOURCE_DATE_ISO_8601 := $$(shell $$(DATE) --utc \
318-
--date="@$$(SOURCE_DATE_EPOCH)" \
319-
+"%Y-%m-%dT%H:%M:%SZ" 2> /dev/null)
320-
else
321-
export SOURCE_DATE_ISO_8601 := $$(shell $$(DATE) -u \
322-
-j -f "%s" "$$(SOURCE_DATE_EPOCH)" \
323-
+"%Y-%m-%dT%H:%M:%SZ" 2> /dev/null)
324-
endif
320+
export SOURCE_DATE_ISO_8601 := $$(shell $$(JAVA) $$(TOPDIR)/make/src/classes/DateUtil.java --date="$$(SOURCE_DATE_EPOCH)" --format="yyyy-MM-dd'T'HH:mm:ss'Z'")
325321
endef
326322

327323
# Parse COMPARE_BUILD into COMPARE_BUILD_*

make/autoconf/jdk-options.m4

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
# questions.
2424
#
2525

26+
# ===========================================================================
27+
# (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
28+
# ===========================================================================
29+
2630
###############################################################################
2731
# Check which variant of the JDK that we want to build.
2832
# Currently we have:
@@ -217,13 +221,9 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_JDK_OPTIONS],
217221
elif test "x$with_copyright_year" != x; then
218222
COPYRIGHT_YEAR="$with_copyright_year"
219223
elif test "x$SOURCE_DATE_EPOCH" != x; then
220-
if test "x$IS_GNU_DATE" = xyes; then
221-
COPYRIGHT_YEAR=`date --date=@$SOURCE_DATE_EPOCH +%Y`
222-
else
223-
COPYRIGHT_YEAR=`date -j -f %s $SOURCE_DATE_EPOCH +%Y`
224-
fi
224+
COPYRIGHT_YEAR=`$JAVA $TOPDIR/make/src/classes/DateUtil.java --format=yyyy --date="$SOURCE_DATE_EPOCH"`
225225
else
226-
COPYRIGHT_YEAR=`$DATE +'%Y'`
226+
COPYRIGHT_YEAR=`$JAVA $TOPDIR/make/src/classes/DateUtil.java --format=yyyy`
227227
fi
228228
AC_SUBST(COPYRIGHT_YEAR)
229229
@@ -675,7 +675,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_REPRODUCIBLE_BUILD],
675675
AC_MSG_RESULT([determined at build time, from 'updated'])
676676
elif test "x$with_source_date" = xcurrent; then
677677
# Set the current time
678-
SOURCE_DATE=$($DATE +"%s")
678+
SOURCE_DATE=$($JAVA $TOPDIR/make/src/classes/DateUtil.java)
679679
AC_MSG_RESULT([$SOURCE_DATE, from 'current'])
680680
elif test "x$with_source_date" = xversion; then
681681
# Use the date from version-numbers.conf

make/autoconf/util.m4

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
# questions.
2424
#
2525

26+
# ===========================================================================
27+
# (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
28+
# ===========================================================================
29+
2630
m4_include([util_paths.m4])
2731

2832
###############################################################################
@@ -236,19 +240,7 @@ AC_DEFUN([UTIL_GET_MATCHING_VALUES],
236240
# $2: input date/time string
237241
AC_DEFUN([UTIL_GET_EPOCH_TIMESTAMP],
238242
[
239-
if test "x$IS_GNU_DATE" = xyes; then
240-
# GNU date
241-
timestamp=$($DATE --utc --date=$2 +"%s" 2> /dev/null)
242-
else
243-
# BSD date
244-
timestamp=$($DATE -u -j -f "%F %T" "$2" "+%s" 2> /dev/null)
245-
if test "x$timestamp" = x; then
246-
# Perhaps the time was missing
247-
timestamp=$($DATE -u -j -f "%F %T" "$2 00:00:00" "+%s" 2> /dev/null)
248-
# If this did not work, we give up and return the empty string
249-
fi
250-
fi
251-
$1=$timestamp
243+
$1=$($JAVA $TOPDIR/make/src/classes/DateUtil.java --date="$2")
252244
])
253245

254246
###############################################################################

make/src/classes/DateUtil.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* ===========================================================================
3+
* (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved
4+
* ===========================================================================
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* IBM designates this particular file as subject to the "Classpath" exception
10+
* as provided by IBM in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, see <http://www.gnu.org/licenses/>.
20+
* ===========================================================================
21+
*/
22+
import java.time.Instant;
23+
import java.time.LocalDate;
24+
import java.time.LocalDateTime;
25+
import java.time.ZoneOffset;
26+
import java.time.format.DateTimeFormatter;
27+
import java.time.format.DateTimeParseException;
28+
import java.util.Locale;
29+
30+
public class DateUtil {
31+
32+
public static void main(String... args) {
33+
String date = "";
34+
String format = "";
35+
36+
for (String arg : args) {
37+
if (arg.startsWith("--date=")) {
38+
date = arg.substring(7).trim();
39+
} else if (arg.startsWith("--format=")) {
40+
format = arg.substring(9).trim();
41+
} else {
42+
showUsageAndExit();
43+
}
44+
}
45+
46+
LocalDateTime time = parseTime(date);
47+
48+
if (format.isEmpty()) {
49+
System.out.println(time.toEpochSecond(ZoneOffset.UTC));
50+
} else {
51+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format, Locale.ROOT);
52+
53+
System.out.println(formatter.format(time));
54+
}
55+
}
56+
57+
private static LocalDateTime parseTime(String text) {
58+
if (text.isEmpty()) {
59+
return LocalDateTime.now(ZoneOffset.UTC);
60+
}
61+
62+
if (text.matches("\\d+")) {
63+
return LocalDateTime.ofEpochSecond(Long.parseLong(text), 0, ZoneOffset.UTC);
64+
}
65+
66+
try {
67+
return LocalDateTime.ofInstant(Instant.parse(text), ZoneOffset.UTC);
68+
} catch (DateTimeParseException e) {
69+
// try next format
70+
}
71+
72+
try {
73+
return LocalDateTime.parse(text);
74+
} catch (DateTimeParseException e) {
75+
// try next format
76+
}
77+
78+
try {
79+
return LocalDate.parse(text).atStartOfDay();
80+
} catch (DateTimeParseException e) {
81+
System.err.format("Cannot parse time: '%s'%n", text);
82+
System.exit(1);
83+
return null;
84+
}
85+
}
86+
87+
private static void showUsageAndExit() {
88+
System.err.println("Usage: DateUtil [options]");
89+
System.err.println(" --date=<time> time in epoch seconds, or in iso-8601 or yyyy-MM-dd format");
90+
System.err.println(" --format=<format> output format");
91+
System.exit(1);
92+
}
93+
94+
}

0 commit comments

Comments
 (0)