On previous version of CentOS, I used the following command to get the current timezone in my bash script:
timezone=$(sed 's/ZONE=//g' /etc/sysconfig/clock) Output is "America/New_York"
How can I achieve an exact output on CentOS 7?
CentOS 7 specific:
timedatectl | gawk -F': ' ' $1 ~ /Timezone/ {print $2}' And displaying just the timezone:
timedatectl | gawk -F'[: ]+' ' $2 ~ /Timezone/ {print $3}'
more generic:
date +%Z America/New_York (EST, -0500) as ouput.Could you modify your first command to make it display only America/New_York timedatectl | awk '/Timezone:/ {print $2}' If you don't mind using perl, you can use DateTime::TimeZone module from CPAN:
$ perl -MDateTime::TimeZone -E ' say DateTime::TimeZone->new(name => "local")->name(); ' Asia/Ho_Chi_Minh POSIXly, you can use date with %Z format:
$ date +%Z ICT For the time zone, you can use geolocation:
$ curl https://ipapi.co/timezone America/Chicago Or:
$ curl http://ip-api.com/line?fields=timezone America/Chicago