Skip to content

Commit 5210e36

Browse files
committed
fix(assert): handle ISO 8601 dates on BusyBox date (Alpine)
Normalize T separator to space before calling date -d, so BusyBox date on Alpine can parse ISO 8601 datetime strings like 2023-11-14T12:00:00.
1 parent aa90ada commit 5210e36

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/assert_dates.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,34 @@ function bashunit::date::to_epoch() {
1212
;;
1313
esac
1414

15+
# Normalize ISO 8601: replace T with space, strip Z suffix, strip tz offset
16+
local normalized="$input"
17+
normalized="${normalized/T/ }"
18+
normalized="${normalized%Z}"
19+
# Strip timezone offset (+HHMM or -HHMM) at end for initial parsing
20+
# shellcheck disable=SC2034
21+
local tz_offset=""
22+
case "$normalized" in
23+
*[+-][0-9][0-9][0-9][0-9])
24+
tz_offset="${normalized: -5}"
25+
normalized="${normalized%[+-][0-9][0-9][0-9][0-9]}"
26+
;;
27+
esac
28+
1529
# Format conversion (GNU vs BSD date)
1630
local epoch
17-
# Try GNU date first (-d flag)
31+
# Try GNU date first (-d flag) with original input
1832
epoch=$(date -d "$input" +%s 2>/dev/null) && {
1933
echo "$epoch"
2034
return 0
2135
}
36+
# Try GNU date with normalized (space-separated) input
37+
if [[ "$normalized" != "$input" ]]; then
38+
epoch=$(date -d "$normalized" +%s 2>/dev/null) && {
39+
echo "$epoch"
40+
return 0
41+
}
42+
fi
2243
# Try BSD date (-j -f flag) with ISO 8601 datetime + timezone offset
2344
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "$input" +%s 2>/dev/null) && {
2445
echo "$epoch"

0 commit comments

Comments
 (0)