Trying manual epoch calculations
#!/usr/bin/env bash | |
# | |
#set -x | |
# | |
_date=$1 | |
wheelv2 () { | |
local d=$1 | |
local _year _month _day _hour _minute _second epoch today days_since_epoch seconds_since_epoch | |
local pattern='[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z' | |
if ! [[ "$d" =~ $pattern ]]; then | |
printf '%s\n' 'Error: Unknown input date format' >&2 | |
exit 1 | |
fi | |
_year=${d:0:4} | |
_month=${d:5:2} | |
_day=${d:8:2} | |
_hour=${d:11:2} | |
_minute=${d:14:2} | |
_second=${d:17:2} | |
if [[ "$_month" -gt 2 ]]; then | |
_month=$((_month+1)) | |
else | |
_month=$((_month+13)) | |
_year=$((_year-1)) | |
fi | |
today=$(( (_year*365) + (_year/4) - (_year/100) + (_year/400) + (_month*306001/10000) + _day )) | |
days_since_epoch=$(( today-719591 )) | |
seconds_since_epoch=$(( (days_since_epoch*86400) + (_hour*3600) + (_minute*60) + _second )) | |
printf '%s\n' "$seconds_since_epoch" | |
} | |
wheel_date=$(wheelv2 "$_date") | |
gnu_date=$(gdate -d "$_date" '+%s') | |
# | |
printf '%s: %s\n' \ | |
"Input date is" \ | |
"$_date" | |
# | |
printf '%s\n%s: %s\n%s: %s\n' \ | |
"Your results are as follows" \ | |
"GNU Date" "$gnu_date" \ | |
"Reinvented wheel" "$wheel_date" |
This comment has been minimized.
This comment has been minimized.
Line 21 and 24 should be +1 and -1 not ++ and -- ... after that, appears to work. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.