0

I have a date string that is supplied in the following format:

Timezone-Year.Month.Day-Hour.Minute.Second
GMT-08-2021.08.31-18.29.05

and I need to figure out how to get the date command to recognize it so I can convert it to epoch time. This will be done in a bash script in case that matters.

0

1 Answer 1

1

Assuming the timezone is always in the format GMT-nn, something like this could work to shuffle the fields around for the GNU version of date and date -d:

a="GMT-08-2021.08.31-18.29.05"
b=$(echo "$a" | sed -E -e 's/GMT([-+][0-9]+)-([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9]+)\.([0-9]+)\.([0-9]+)/\2-\3-\4 \5:\6:\7 \100/')
date -u -d "$b"

That's basically just a bunch of capture groups ((...)) and putting them back in the right order (\2 for the second group, etc.).

With that example value, b would contain 2021-08-31 18:29:05 -0800, and I get Wed Sep 1 02:29:05 UTC 2021 from date.

For other versions of date, you'd need some other output format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.