2

I have to calculate the date epoch time from a GMT timestamp, but I'm locally not in that timezone. My current local time is CEST.

So when I try to calculate the epoch time with

date -d "2017-08-05 10:10:10" "+%s"

I get

1501927810

But the given time is a GMT, and should therefore result in

1501927810

I was trying to manually add the difference of hours between the local time and the GMT with

date -d "2017-08-05 10:10:10 +2 hours" "+%F %T"

But that gives me

2017-08-05 11:10:10

why is there only +1 hour added!?

1
  • Because CEST = GMT + 1 hour? Commented Oct 17, 2021 at 17:55

4 Answers 4

4

You can tell date to use GMT timezone by setting the environment variable TZ:

$ TZ=GMT date -d "2017-08-05 10:10:10" "+%s"
1501927810

Otherwise you would have to specify the date string as GMT (+0 hours, not 2):

$ date -d "2017-08-05 10:10:10 +0" "+%s"
1501927810
1

You can add the known timezone from where the original date was to the date string:

date -d "2017-08-05 10:10:10 GMT" +%s
1501927810
date -d "2017-08-05 10:10:10 PST" +%s
1501956610

And as mentioned in other answers, you also have to take into account the daylight saving time changes and GMT might actually not be the timezone you're looking for.

You can also use the following format:

date --date 'TZ="Europe/London" 2017-08-05 10:10:10' +%s
1501924210

That is different from setting the TZ environment variable and you should be aware that using the TZ environment variable doesn't work when using epoch timestamps:

echo "TODAY: $(TZ=UTC date) : $(TZ=UTC date '+%s') YESTERDAY: $(TZ=UTC+24 date) $(TZ=UTC+24 date '+%s')" 
TODAY: Fri Oct 15 21:57:12 UTC 2021 : 1634335032 YESTERDAY: Thu Oct 14 21:57:12 UTC 2021 1634335032

Note that the dates are correct, but the epoch timestamps are the same.

This does work:

echo "TODAY: $(date) : $(date '+%s') YESTERDAY: $(date -d '24 hours ago') $(date -d '24 hours ago' '+%s')"
TODAY: Fri Oct 15 23:05:03 IST 2021 : 1634335503 YESTERDAY: Thu Oct 14 23:05:03 IST 2021 1634249103

This is because epoch timestamps are not affected by time zones since they are simply the number of seconds since a specific date. That will be the same no matter what timezone you are in.

So if you want the epoch timestamp from some date in a different timezone, you can use something like

date -d "$(TZ=PST date)" +%s

If you have a date string already that you know is from a different timezone and want it's epoch timestamp, using the TZ env var will not work either:

TZ=GMT date -d "2017-08-05 10:10:10" "+%s"
1501927810

produces the same timestamp as:

TZ=PST date -d "2017-08-05 10:10:10" "+%s"
1501927810
1
  • Thanks for posting a new answer! This is indeed a great improvement over the deleted one. Just to clarify one point, your other answer had already received two deletion votes from regular users, I (as a mod) cast the third vote, but the answer would have been deleted in any case. And it is often better to delete incorrect answers and post new ones, in cases such as this where the new answer is significantly different, to clear any existing votes.
    – terdon
    Commented Oct 22, 2021 at 13:58
0

If your timestamp is in UTC, then you can tell date to use UTC by using the -u option:

-u, --utc, --universal

print or set Coordinated Universal Time (UTC)

0

GMT is often used to describe a timezone that switches by an hour between summer and winter (UK, Portugal, etc.). In August the UK would have been on GMT+1 but in many situations the timezone is still described as GMT despite the label for that part of the year being BST. For date the timezone would be written "Europe/London". (Notice the origin timezone is declared in the date definition. You can choose your target timezone for display by setting TZ outside the date command, as an environment variable, in the usual way.)

date --date 'TZ="Europe/London" 2017-08-05 10:10:10'

In other contexts GMT is used to describe an invariant broadly equivalent to UTC. Here the timezone would be given simply as "GMT".

date --date 'TZ="GMT" 2017-08-05 10:10:10'

It depends on the data source as to which of these your timestamp corresponds, and it matters because in August they would differ by an hour.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .