Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get UTC offset from Unix command line?

Tags:

I'm writing an autoconf script that needs the current UTC offset. There's no obvious way to get this out of the date program. Is there any straightforward way to get this from a command-line utility, or should I write a test that gets the information and somehow captures it?

like image 260
vy32 Avatar asked May 02 '11 16:05

vy32


People also ask

What is the command to check the time in Unix?

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.

How do I get the current date and time in Unix?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

What is Linux system time?

It is calculated by the Linux kernel as the number of seconds since midnight January 1st 1970, UTC. The initial value of the system clock is calculated from the hardware clock, dependent on the contents of /etc/adjtime .


2 Answers

Try this, and see whether it works for you:

date +%z
like image 74
Till Avatar answered Sep 22 '22 14:09

Till


For others doing ISO8601, you might pick some variant of:

date +%Y%m%dT%H%M%S%z     # 20140809T092143-0700
date -u +%Y%m%dT%H%M%S%z  # 20140809T162143+0000
date -u +%Y%m%dT%H%M%SZ   # 20140809T162143Z

I like those because the lack of punctuation supports universal use. Note that the capital Z is 'hard-coded' for UTC - using %Z will put UTC or the other named timezone. If you prefer punctuation:

date +%Y-%m-%dT%H:%M:%S%z      # 2014-08-09T09:21:43-0700
date +%Y-%m-%dT%H:%M:%S%:z     # 2014-08-09T09:21:43-07:00 - NOT ALL SYSTEMS
date -u +%Y-%m-%dT%H:%M:%S%z   # 2014-08-09T16:21:43+0000
date -u +%Y-%m-%dT%H:%M:%S%:z  # 2014-08-09T16:21:43+00:00 - NOT ALL SYSTEMS
date -u +%Y-%m-%dT%H:%M:%SZ    # 2014-08-09T16:21:43Z

Consult man strftime as supported formats vary. For instance, some systems support inserting colons into the offset using %:z, %::z, or %:::z - only two of my five systems do (Debian, Ubuntu do, but Mac, BusyBox, QNX do not).

And I often go back to https://en.wikipedia.org/wiki/ISO_8601 for reference.

like image 37
sage Avatar answered Sep 24 '22 14:09

sage