Skip to content

Commit 627e6b4

Browse files
committed
fix(assert): handle timezone offsets in date parsing for BusyBox
1 parent a122c56 commit 627e6b4

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/assert_dates.sh

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@ function bashunit::date::to_epoch() {
1212
;;
1313
esac
1414

15-
# Normalize ISO 8601: replace T with space, strip Z suffix, strip tz offset
15+
# Handle Z (UTC) suffix explicitly: BusyBox needs TZ=UTC, BSD needs +0000
16+
case "$input" in
17+
*Z)
18+
local utc_input="${input%Z}"
19+
local utc_norm="${utc_input/T/ }"
20+
local epoch
21+
# GNU/BusyBox: parse in explicit UTC
22+
epoch=$(TZ=UTC date -d "$utc_input" +%s 2>/dev/null) && { echo "$epoch"; return 0; }
23+
epoch=$(TZ=UTC date -d "$utc_norm" +%s 2>/dev/null) && { echo "$epoch"; return 0; }
24+
# BSD: use +0000 offset which %z understands
25+
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%S%z" "${utc_input}+0000" +%s 2>/dev/null) && { echo "$epoch"; return 0; }
26+
echo "$input"
27+
return 1
28+
;;
29+
esac
30+
31+
# Normalize ISO 8601: replace T with space, strip tz offset
1632
local normalized="$input"
1733
normalized="${normalized/T/ }"
18-
normalized="${normalized%Z}"
1934
# Strip timezone offset (+HHMM or -HHMM) at end for initial parsing
2035
case "$normalized" in
2136
*[+-][0-9][0-9][0-9][0-9])
@@ -30,6 +45,24 @@ function bashunit::date::to_epoch() {
3045
echo "$epoch"
3146
return 0
3247
}
48+
# If input has timezone offset, parse in UTC and adjust manually (BusyBox)
49+
case "$input" in
50+
*[+-][0-9][0-9][0-9][0-9])
51+
epoch=$(TZ=UTC date -d "$normalized" +%s 2>/dev/null) && {
52+
local ilen=${#input}
53+
local ostart=$((ilen - 5))
54+
local osign="${input:$ostart:1}"
55+
local ohh="${input:$((ostart + 1)):2}"
56+
local omm="${input:$((ostart + 3)):2}"
57+
local osecs=$(( (10#$ohh * 3600) + (10#$omm * 60) ))
58+
if [ "$osign" = "+" ]; then
59+
osecs=$(( -osecs ))
60+
fi
61+
echo $(( epoch + osecs ))
62+
return 0
63+
}
64+
;;
65+
esac
3366
# Try GNU date with normalized (space-separated) input
3467
if [ "$normalized" != "$input" ]; then
3568
epoch=$(date -d "$normalized" +%s 2>/dev/null) && {

0 commit comments

Comments
 (0)