Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499167
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类:

2012-08-16 15:00:46

原文地址:显示不同时区的时间 作者:ubuntuer

通常我们要查询本地区当前的日期和时间,可以使用到最基本的date命令。但是如果你要要穿越不同的时区,这时不就遇到麻烦了?或者,更有说服力点,如果你有朋友和同事在不同的国家地区,而且你很在乎时间,比如在卡萨布兰加,梵谛冈市或者悉尼。
    现在我们在大多数Unix机可以通过date命令来创建一个时区数据库。它通常位于/usr/share/zoneinfo,该数据库列举了多达250个不同的区域,并且为每个区域确定适当的时区。因为date命令可以TZ选项指定时区,而且适合任何的已知区域,下面举例该功能的应用:
$ TZ="Africa/Casablanca" date
Mon Dec 2 16:31:01 WET 2069
    但是我们可以使用一个shell脚本,生成更友好的时区数据库:暂时的环境变量设置可能对大多数系统使用者觉得不大舒服。

脚本源代码

#!/bin/sh


# timein - Shows the current time in the specified time zone or

# geographic zone. Without any argument, shows UTC/GMT. Use

# the word "list" to see a list of known geographic regions.

# Note that it's possible to match zone directories (regions),

# but that only time zone files (cities) are valid specifications.


# Time zone database ref:


zonedir="/usr/share/zoneinfo"

if [ ! -d $zonedir ] ; then
  echo "No time zone database at $zonedir." >&2 ; exit 1
fi

if [ -d "$zonedir/posix" ] ; then
  zonedir=$zonedir/posix # modern Linux systems

fi

if [ $# -eq 0 ] ; then

  timezone="UTC"
  mixedzone="UTC"
elif [ "$1" = "list" ] ; then
  ( echo "All known time zones and regions defined on this system:"
    cd $zonedir
    find * -type f -print | xargs -n 2 | \
      awk '{ printf " %-38s %-38s\n", $1, $2 }'
  ) | more
  exit 0
else

  region="$(dirname $1)"
  zone="$(basename $1)"

  # Is it a direct match? If so, we're good to go. Otherwise we need

  # to dig around a bit to find things. Start by just counting matches.


  matchcnt="$(find $zonedir -name $zone -type f -print |
        wc -l | sed 's/[^[:digit:]]//g')"


  if [ "$matchcnt" -gt 0 ] ; then # at least one file matches

    if [ $matchcnt -gt 1 ] ; then # more than one file matches

      echo "\"$zone\" matches more than one possible time zone record." >&2
      echo "Please use 'list' to see all known regions and time zones" >&2
      exit 1
    fi
    match="$(find $zonedir -name $zone -type f -print)"
    mixedzone="$zone"
  else
    # First letter capitalized, rest of word lowercase for region + zone

    mixedregion="$(echo ${region%${region#?}} | tr '[[:lower:]]' '[[:upper:]]')\
$(echo ${region#?} | tr '[[:upper:]]' '[[:lower:]]')"

    mixedzone="$(echo ${zone%${zone#?}} | tr '[[:lower:]]' '[[:upper:]]')\
$(echo ${zone#?} | tr '[[:upper:]]' '[[:lower:]]')"


    if [ "$mixedregion" != "." ] ; then
      # Only look for specified zone in specified region

      # to let users specify unique matches when there's more than one

      # possibility (e.g., "Atlantic")

      match="$(find $zonedir/$mixedregion -type f -name $mixedzone -print)"
    else
      match="$(find $zonedir -name $mixedzone -type f -print)"
    fi

    if [ -z "$match" ] ; then # no file matches specified pattern

      if [ ! -z $(find $zonedir -name $mixedzone -type d -print) ] ; then
        echo \
    "The region \"$1\" has more than one time zone. Please use 'list'" >&2
      else # just not a match at all

        echo "Can't find an exact match for \"$1\". Please use 'list'" >&2
      fi
      echo "to see all known regions and time zones." >&2
      exit 1
    fi
  fi
  timezone="$match"
fi

nicetz=$(echo $timezone | sed "s|$zonedir/||g") # pretty up the output


echo It\'s $(TZ=$timezone date '+%A, %B %e, %Y, at %l:%M %p') in $nicetz

exit 0

工作原理


运行脚本
指定某个特定的时区或城市进行查询,在命令返回结果就会显示该时区或城市的时间。只要你对相关的时区或城市有所了解,在该脚本就就能用时区或城市查询,比如用Pacific或Yap。如果没附带任何选项,timein脚本就显示通用时间,即格林威治标准时间(UTC)

结果
$ timein
It'
s Friday, March 28, 2069, at 2:58 AM in UTC
$ timein London
It's Friday, March 28, 2069, at 2:58 AM in Europe/London
$ timein Brazil
The region "Brazil" has more than one time zone. Please use '
list'
to see all known regions and time zones.
$ timein Pacific/Honolulu
It'
s Thursday, March 27, 2069, at 4:58 PM in Pacific/Honolulu
$ timein WET
It's Friday, March 28, 2069, at 3:58 AM in WET
$ timein mycloset
Can'
t find an exact match for "mycloset". Please use 'list'
to see all known regions and time zones.

 原文地址 http://www.bsdlover.cn/html/47/n-447.html

阅读(319) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~