Reputation: 292
I'm trying to get the current date/time seconds from epoch in bash. I did it with
date +%s
But this doesn't handle my timezone and daylight saving.
Something like 1) get epoch 2) add 3600 (i'm in rome, so GMT+1) 3) add 3600 only if we are on daylight saving time
It's possible in bash?
Upvotes: 1
Views: 1536
Reputation: 292
I found a way:
#!/bin/sh
OFFSET=$(date +%z)
SIGN=${OFFSET:0:1}
HOURS=${OFFSET:1:2}
MINUTES=${OFFSET:3:2}
EPOCH=$(date +%s)
echo $(( ${EPOCH} ${SIGN} ( ${HOURS} * 3600 + ${MINUTES} * 60 ) ))
Upvotes: 1
Reputation: 461
date [-u|--utc|--universal]
returns the UTC representation whereas date
returns the system's local time with regard to DST (Daylight Saving Time).
Example (assuming 2016-06-01 is within DST):
Get Local time from your timezone (GMT+1 +1 for DST)
$ date -d '2016-06-01 02:00:00+0200' +'%s = %Y-%m-%d %H:%M:%S'
1464739200 = 2016-06-01 02:00:00
Get UTC (epoch) from your timezone (GMT+1 +1 for DST)
$ date -u -d '2016-06-01 02:00:00+0200' +'%s = %Y-%m-%d %H:%M:%S'
1464739200 = 2016-06-01 00:00:00
Upvotes: 0