Skip to content
This repository was archived by the owner on Dec 13, 2024. It is now read-only.

Commit 9c604b0

Browse files
authored
Merge pull request #33 from keithc-ca/date
Avoid non-portable uses of date utility
2 parents b9e3daf + 3ad0774 commit 9c604b0

File tree

5 files changed

+117
-40
lines changed

5 files changed

+117
-40
lines changed

make/InitSupport.gmk

+5-1
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
@@ -311,7 +315,7 @@ else # $(HAS_SPEC)=true
311315
define SetupReproducibleBuild
312316
ifeq ($$(SOURCE_DATE), updated)
313317
# For static values of SOURCE_DATE (not "updated"), these are set in spec.gmk
314-
export SOURCE_DATE_EPOCH := $$(shell $$(DATE) +"%s")
318+
export SOURCE_DATE_EPOCH := $$(shell $$(JAVA) $$(TOPDIR)/make/src/classes/DateUtil.java)
315319
export SOURCE_DATE_ISO_8601 := $$(call EpochToISO8601, $$(SOURCE_DATE_EPOCH))
316320
endif
317321
endef

make/autoconf/jdk-options.m4

+8-12
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
# Set the debug level
2832
# release: no debug information, all optimizations, no asserts.
@@ -203,13 +207,9 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_JDK_OPTIONS],
203207
elif test "x$with_copyright_year" != x; then
204208
COPYRIGHT_YEAR="$with_copyright_year"
205209
elif test "x$SOURCE_DATE" != xupdated; then
206-
if test "x$IS_GNU_DATE" = xyes; then
207-
COPYRIGHT_YEAR=`$DATE --date=@$SOURCE_DATE +%Y`
208-
else
209-
COPYRIGHT_YEAR=`$DATE -j -f %s $SOURCE_DATE +%Y`
210-
fi
210+
COPYRIGHT_YEAR=`$JAVA $TOPDIR/make/src/classes/DateUtil.java --format=yyyy --date="$SOURCE_DATE_EPOCH"`
211211
else
212-
COPYRIGHT_YEAR=`$DATE +'%Y'`
212+
COPYRIGHT_YEAR=`$JAVA $TOPDIR/make/src/classes/DateUtil.java --format=yyyy`
213213
fi
214214
AC_SUBST(COPYRIGHT_YEAR)
215215
@@ -674,7 +674,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_REPRODUCIBLE_BUILD],
674674
AC_MSG_RESULT([determined at build time, from 'updated'])
675675
elif test "x$with_source_date" = xcurrent; then
676676
# Set the current time
677-
SOURCE_DATE=$($DATE +"%s")
677+
SOURCE_DATE=$($JAVA $TOPDIR/make/src/classes/DateUtil.java)
678678
AC_MSG_RESULT([$SOURCE_DATE, from 'current'])
679679
elif test "x$with_source_date" = xversion; then
680680
# Use the date from version-numbers.conf
@@ -706,11 +706,7 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_REPRODUCIBLE_BUILD],
706706
# If we have a fixed value for SOURCE_DATE, we need to set SOURCE_DATE_EPOCH
707707
# for the rest of configure.
708708
SOURCE_DATE_EPOCH="$SOURCE_DATE"
709-
if test "x$IS_GNU_DATE" = xyes; then
710-
SOURCE_DATE_ISO_8601=`$DATE --utc --date="@$SOURCE_DATE" +"$ISO_8601_FORMAT_STRING" 2> /dev/null`
711-
else
712-
SOURCE_DATE_ISO_8601=`$DATE -u -j -f "%s" "$SOURCE_DATE" +"$ISO_8601_FORMAT_STRING" 2> /dev/null`
713-
fi
709+
SOURCE_DATE_ISO_8601=`$JAVA $TOPDIR/make/src/classes/DateUtil.java --date="$SOURCE_DATE" --format="yyyy-MM-dd'T'HH:mm:ss'Z'"`
714710
fi
715711
716712
REPRODUCIBLE_BUILD_DEFAULT=$with_source_date_present

make/autoconf/util.m4

+5-18
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
###############################################################################
@@ -234,24 +238,7 @@ AC_DEFUN([UTIL_GET_MATCHING_VALUES],
234238
# $2: input date/time string
235239
AC_DEFUN([UTIL_GET_EPOCH_TIMESTAMP],
236240
[
237-
if test "x$IS_GNU_DATE" = xyes; then
238-
# GNU date
239-
timestamp=$($DATE --utc --date=$2 +"%s" 2> /dev/null)
240-
else
241-
# BSD date
242-
# ISO-8601 date&time in Zulu 'date'T'time'Z
243-
timestamp=$($DATE -u -j -f "%FT%TZ" "$2" "+%s" 2> /dev/null)
244-
if test "x$timestamp" = x; then
245-
# BSD date cannot handle trailing milliseconds.
246-
# Try again ignoring characters at end
247-
timestamp=$($DATE -u -j -f "%Y-%m-%dT%H:%M:%S" "$2" "+%s" 2> /dev/null)
248-
fi
249-
if test "x$timestamp" = x; then
250-
# Perhaps the time was missing.
251-
timestamp=$($DATE -u -j -f "%FT%TZ" "$2""T00:00:00Z" "+%s" 2> /dev/null)
252-
fi
253-
fi
254-
$1=$timestamp
241+
$1=$($JAVA $TOPDIR/make/src/classes/DateUtil.java --date="$2")
255242
])
256243

257244
###############################################################################

make/common/Utils.gmk

+5-9
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
ifeq (,$(_MAKEBASE_GMK))
2731
$(error You must include MakeBase.gmk prior to including Utils.gmk)
2832
endif
@@ -221,15 +225,7 @@ Or = \
221225
# Convert an UNIX epoch based timestamp (as an integer) to an ISO 8601 date
222226
# string.
223227
# Param 1 - timestamp
224-
ifeq ($(IS_GNU_DATE), yes)
225-
EpochToISO8601 = \
226-
$(shell $(DATE) --utc --date="@$(strip $1)" \
227-
+"$(ISO_8601_FORMAT_STRING)" 2> /dev/null)
228-
else
229-
EpochToISO8601 = \
230-
$(shell $(DATE) -u -j -f "%s" "$(strip $1)" \
231-
+"$(ISO_8601_FORMAT_STRING)" 2> /dev/null)
232-
endif
228+
EpochToISO8601 = $(shell $(JAVA) $(TOPDIR)/make/src/classes/DateUtil.java --date="$(strip $1)" --format="yyyy-MM-dd'T'HH:mm:ss'Z'")
233229

234230
################################################################################
235231
# Parse a multiple-keyword variable, like FOO="KEYWORD1=val1;KEYWORD2=val2;..."

make/src/classes/DateUtil.java

+94
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)