fix(date): improve cross-platform date command compatibility

Fixes date command errors on macOS with Homebrew GNU coreutils:
- 'date: invalid option -- v' when GNU date runs BSD syntax
- 'syntax error in expression' when stat returns filesystem metadata

Changes:
- Replace uname-based detection with capability detection (try/fallback)
- Try GNU commands first, fall back to BSD, then ultimate fallbacks
- Add date -r fallback for file mtime (most portable)

This handles mixed environments where uname returns "Darwin" but
GNU coreutils are in PATH from Homebrew.

Credit: @farce1 (PR #119)
This commit is contained in:
Test User 2026-02-02 09:39:39 -07:00
parent c05499c176
commit 3ed0a9d9ff
2 changed files with 32 additions and 27 deletions

View file

@ -5,33 +5,33 @@
# Get current timestamp in ISO 8601 format with seconds precision
# Returns: YYYY-MM-DDTHH:MM:SS+00:00 format
# Uses capability detection instead of uname to handle macOS with Homebrew coreutils
get_iso_timestamp() {
local os_type
os_type=$(uname)
if [[ "$os_type" == "Darwin" ]]; then
# macOS (BSD date)
# Use manual formatting and add colon to timezone offset
date -u +"%Y-%m-%dT%H:%M:%S%z" | sed 's/\(..\)$/:\1/'
else
# Linux (GNU date) - use -u flag for UTC
date -u -Iseconds
# Try GNU date first (works on Linux and macOS with Homebrew coreutils)
local result
if result=$(date -u -Iseconds 2>/dev/null) && [[ -n "$result" ]]; then
echo "$result"
return
fi
# Fallback to BSD date (native macOS) - add colon to timezone offset
date -u +"%Y-%m-%dT%H:%M:%S%z" | sed 's/\(..\)$/:\1/'
}
# Get time component (HH:MM:SS) for one hour from now
# Returns: HH:MM:SS format
# Uses capability detection instead of uname to handle macOS with Homebrew coreutils
get_next_hour_time() {
local os_type
os_type=$(uname)
if [[ "$os_type" == "Darwin" ]]; then
# macOS (BSD date) - use -v flag for date arithmetic
date -v+1H '+%H:%M:%S'
else
# Linux (GNU date) - use -d flag for date arithmetic
date -d '+1 hour' '+%H:%M:%S'
# Try GNU date first (works on Linux and macOS with Homebrew coreutils)
if date -d '+1 hour' '+%H:%M:%S' 2>/dev/null; then
return
fi
# Fallback to BSD date (native macOS)
if date -v+1H '+%H:%M:%S' 2>/dev/null; then
return
fi
# Ultimate fallback - compute using epoch arithmetic
local future_epoch=$(($(date +%s) + 3600))
date -r "$future_epoch" '+%H:%M:%S' 2>/dev/null || date '+%H:%M:%S'
}
# Get current timestamp in a basic format (fallback)