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?
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.
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 # ...
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 .
Try this, and see whether it works for you:
date +%z
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With