- 論壇徽章:
- 0
|
本帖最后由 chinaux 于 2011-11-25 08:24 編輯
Environment: RHEL 6.1
How to set a correct time zone in RHEL could be a headache issue, it was for me a few days ago. For system administrators in China, most of them have no such an issue, since they may do a simple "ln -sf /usr/share/zoneinfo/PRC /etc/localtime" or "cp -a /usr/share/zoneinfo/PRC /etc/localtime", and then a correct timezone is on the output of "date" or "date +%Z". However, what to do if I'd like to see "GMT+8"?
"ln -sf /usr/share/zoneinfo/Etc/GMT+8 /etc/localtime" does give the correct time zone "GMT+8", however, the time is completely wrong, it's the time of US/Pacific. "ln -sf /usr/share/zoneinfo/Etc/GMT-8 /etc/localtime" gives me the right time, however, the time zone "GMT-8" is not what I expect.
"export TZ=GMT-8" in bash gives the correct time, but, the time zone "GMT" is still incorrect.
The right solution is .........
export TZ="<GMT+8>-8"
then, both the time and the time zone are correct!
I've written functions of setting a time zone in system-wide in bash for your reference:- backup_file()
- {
- file=$1
- tmpbackup=${file}.tmp
- if [ ! -f $file ]; then
- # echo "File $file doesn't exist." >&2
- # return 1
- touch $tmpbackup
- restorecon $tmpbackup
- return 0
- fi
-
- cp -a $file $tmpbackup
- if [ "X${backup}" == "no" ]; then return 0 ; fi
- if [ ! -f ${file}.${stamp} ]; then
- cp -a $file ${file}.${stamp}
- fi
- return $?
- }
- # compare a configuration file against its backup ended with "$stamp"
- compare_files()
- {
- file=$1
- tstamp=$2
- if [ ! -f $file -o ! -f ${file}.${tstamp} ]; then
- echo "Error: either $file or ${file}.${tstamp} doesn't exist." >&2
- echo "Error: fail to compare files." >&2
- return 99
- fi
- if [ "X$(diff $file ${file}.${tstamp})" == "X" ]; then
- echo " no changes made to $file."
- rm -f ${file}.${tstamp}
- return 0
- else
- echo " $file is now regenerated."
- return 1
- fi
- }
- set_timezone()
- {
- timezone=$1
- if [ "X${timezone}" == "X" ]; then
- echo "Error: timezone must be a valid value." >&2
- return 1
- fi
- echo -e " new timezone \"${timezone}\" is to be set. "
- echo -e " current timezone is \"$(date +%Z)\" according to the output of \"# date +%Z\". "
- backup_file /etc/profile
- backup_file /etc/csh.login
-
- # bash/ksh
- if grep -q "^export[[:space:]]\{1,\}TZ=" /etc/profile; then
- echo " current TZ is $(grep '^export[[:space:]]\{1,\}TZ=' /etc/profile | sed -e 's/.*TZ=//' -e 's/#.*//' -e 's/[[:space:]].*//g') on /etc/profile."
- grep -v "^export[[:space:]]\{1,\}TZ=" /etc/profile > /etc/profile.tmp
- fi
- # csh
- if grep -q "^setenv[[:space:]]\{1,\}TZ\{1,\}" /etc/csh.login; then
- echo " current TZ is $(grep '^setenv[[:space:]]\{1,\}TZ[[:space:]]\{1,\}' /etc/csh.login | sed -e 's/.*TZ[[:space:]]\{1,\}//' -e 's/#.*//' -e 's/[[:space:]].*//g') on /etc/csh.login."
- grep -v "^setenv[[:space:]]\{1,\}TZ[[:space:]]\{1,\}" /etc/csh.login > /etc/csh.login.tmp
- fi
- echo $timezone | grep -q -E "\+|\-"
- if [ $? -ne 0 ]; then
- if [ -f /usr/share/zoneinfo/${timezone} ]; then
- rm -f /etc/localtime
- cp -a /usr/share/zoneinfo/${timezone} /etc/localtime
- echo " timezone is now set to \"$timezone\"."
- else
- echo "Error: timezone \"$timezone\" is not supported." >&2
- fi
- else
- if echo $timezone | grep -q '+'; then
- offsethour=$(echo $timezone | sed -e "s/.*+/\-/")
- else
- offsethour=$(echo $timezone | sed -e "s/.*-/\+/")
- fi
- sed -e "/^unset i/i\
- export TZ=\"\<${timezone}\>${offsethour}\"
- " /etc/profile.tmp > /etc/profile
- sed -e "/^set history/i\
- setenv TZ \"\<${timezone}\>${offsethour}\"
- " /etc/csh.login.tmp > /etc/csh.login
- fi
- compare_files /etc/profile $stamp
- compare_files /etc/csh.login $stamp
- rm -f /etc/profile.tmp /etc/csh.login.tmp
- }
復(fù)制代碼 |
|