Add cross-platform date utility library to handle differences between GNU date (Linux) and BSD date (macOS). Fixes issues with: - ISO 8601 timestamp formatting (-Iseconds flag) - Date arithmetic operations (-d vs -v flags) Changes: - Created lib/date_utils.sh with get_iso_timestamp() and get_next_hour_time() - Updated ralph_loop.sh to use date utilities (2 instances) - Updated lib/circuit_breaker.sh to use date utilities (4 instances) - Updated lib/response_analyzer.sh to use date utilities (1 instance) All date operations now work consistently across both platforms without modification. The utility automatically detects the OS and uses the appropriate date command syntax. Tested on Linux with GNU date - all syntax checks and integration tests pass.
41 lines
1.1 KiB
Bash
41 lines
1.1 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# date_utils.sh - Cross-platform date utility functions
|
|
# Provides consistent date formatting and arithmetic across GNU (Linux) and BSD (macOS) systems
|
|
|
|
# Get current timestamp in ISO 8601 format with seconds precision
|
|
# Returns: YYYY-MM-DDTHH:MM:SS+00:00 format
|
|
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)
|
|
date -Iseconds
|
|
fi
|
|
}
|
|
|
|
# Get time component (HH:MM:SS) for one hour from now
|
|
# Returns: HH:MM:SS format
|
|
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'
|
|
fi
|
|
}
|
|
|
|
# Get current timestamp in a basic format (fallback)
|
|
# Returns: YYYY-MM-DD HH:MM:SS format
|
|
get_basic_timestamp() {
|
|
date '+%Y-%m-%d %H:%M:%S'
|
|
}
|