Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Trying manual epoch calculations
#!/usr/bin/env bash
#set -x
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
if [[ "$_month" -gt 2 ]]; then
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" \
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.

Copy link
Owner Author

jessebutryn commented Oct 23, 2019

$ ./ '2019-10-23T17:25:23Z'
Input date is: 2019-10-23T17:25:23Z
Your results are as follows
GNU Date: 1571851523
Reinvented wheel: 1571851523

This comment has been minimized.

Copy link

derobert commented Oct 23, 2019

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
You can’t perform that action at this time.