YEEN
YEEN

Reputation: 87

bash (>4.2) script timezone offset format as hours and minutes

I am using GNU bash, version 4.3. How can I represent timezone as hours and minute?

Here is the code.

./time.sh

printf -v t '%(%s)T' -1
export TZ=UTC


counter=1
while [ "$counter" -le 10 ]
do
    ((t=t+600))
    printf '%(%y-%m-%dT%H:%M:%S%z)T\n' "$t"
    ((counter++))
done

echo All done

Here is the result.

#bash time.sh
18-01-17T07:29:50+0000
18-01-17T07:39:50+0000
18-01-17T07:49:50+0000
18-01-17T07:59:50+0000
18-01-17T08:09:50+0000
18-01-17T08:19:50+0000
18-01-17T08:29:50+0000
18-01-17T08:39:50+0000
18-01-17T08:49:50+0000
18-01-17T08:59:50+0000
All done

But I'd like to show the result format like '18-01-17T08:59:50+00:00'. How can I do that?

Upvotes: 1

Views: 208

Answers (1)

Thomas
Thomas

Reputation: 182028

Use date instead of printf:

date --iso-8601=seconds --date="@$t"

Note that this assumes GNU date; the --iso-8601 argument does not exist in the POSIX specification. POSIX doesn't have %:z either (nor even %z), so if this needs to be portable you'll need a different solution.

Upvotes: 1

Related Questions