Display time in different time zones

Source: Internet
Author: User
Tags time zones
The most basic requirement for a date command to display time in different time zones is to display the date and time in your time zone. But what if you have users across time zones? Or if you have friends or colleagues in different regions? You will always be confused about what day it is ,... info & nbsp display time of different time zones the most basic requirement of a date command is to display the date and time of your time zone. But what if you have users across time zones? Or if you have friends or colleagues in different regions? You will never know what day it is, such as Casablanca, the Vatican, or Sydney. Currently, most Unix systems have a date command, whose background is a time zone database. Its location is generally in the directory/usr/share/zoneinfo. it lists more than 250 different regions and can clearly indicate the time of each region. Because the date command focuses on the time variable TZ, which can be set to different known regions, the core functions can be expressed as follows: TZ = "Africa/Casablanca" date through a shell script, we can create a user-friendly time zone database: You know, most system users do not like the temporary environment variable setting code: 01 #! /Bin/sh02 03 # timein. sh -- display the time of a specific time zone or region. 04 # If no parameters are provided, display UTC/GMT.05 # use the word "list" to view the list of known regions 06 # Note: The region may be matched, however, only the time zone is valid. 07 08 #09 10 zonedir = "/usr/share/zoneinfo" 11 12 if [! -D $ zonedir]; then13 echo "No time zone database at $ zonedir. "> & 214 exit 115 fi16 17 if [-d" $ zonedir/posix "]; then18 zonedir = $ zonedir/posix # Modern Linux system 19 fi20 21 if [$ #-eq 0]; then22 timezone = "UTC" 23 mixedzone = "UTC" 24 elif ["$1" = "list"]; then25 (echo "All known time zones and regions defined on this system: "26 cd $ zonedir27 find *-type f-print | xargs-n 2 | \ 28 awk '{printf" %-38 s % -38s \ n ", $1, $2} '29) | more30 exit 031 else32 region =" $ (dirname $1) "# if the parameter format is Asia/Beijing, then region is Asia33 zone = "$ (basename $1)" # if the parameter format is Beijing, then region is a point 34 35 # exactly matches? Very good. Otherwise, we need to explore it. 36 # start from calculating the number of matches. 37 38 matchcnt = "$ (find $ zonedir/$ region-name $ zone-type f-print | 39 wc-l | sed's/[^ [: digit:] // g') "# Add $ region to the above search path; otherwise, the search is inaccurate. No. 40 # for example, a parameter like Asia/Beijing is provided. if the path does not contain Asia, Beijing 41 42 if ["$ matchcnt"-gt 0] will never be found. then # at least one file matches 43 if ["$ matchcnt"-gt 1]; then # more than one file matches 44 echo "\" $ zone \ "matches more than one possible time zone record. "> & 245 echo" Please use 'list' to see all known regions and time zones. "> & 246 exit 147 fi48 match =" $ (find $ zonedir-name $ zone-type f-print) "49 mixedzone =" $ zone "50 else51 # The first letter is in upper case, and the remaining words are in lower case: r Egion + zone52 mixedregion = "$ (echo $ {region % $ {region #?}} | Tr' [[: lower:] '[[: upper:]') \ 53 $ (echo $ {region #?} | Tr' [[: upper:] '[[: lower:]') "# Do not leave spaces or tabs in the line header 54 mixedzone =" $ (echo $ {zone % $ {zone #?}} | Tr' [[: lower:] '[[: upper:]') \ 55 $ (echo $ {zone #?} | Tr' [[: upper:] ''[[: lower:] ')" # Do not leave spaces or tabs in the line header 56 57 if ["$ mixedregion "! = ". "]; Then58 # Only query specific places 59 # when there are multiple possibilities, ask the user to specify a unique match (such as" Atlantic ") 60 match = "$ (find $ zonedir/$ mixedregion-name $ mixedzone-type f-print) "61 else62 match =" $ (find $ zonedir-name $ mixedzone-type f-print) "63 fi64 65 if [-z" $ match "]; then66 if [! -Z "$ (find $ zonedir-name $ mixedzone-type d-print)"]; then67 echo \ 68 "The region \" $1 \ "has more than one time zone. please use 'list' "> & 269 else # none match 70 echo" Can't find an exact match for \ "$1 \". please use 'list' "> & 271 fi72 echo" to see all known regions and time zones. "> & 273 exit 174 fi75 fi76 timezone =" $ match "77 fi78 79 nicetz = $ (echo $ timezone | sed" s | $ zonedir/| g ") 80 81 echo It \'s $ (TZ = $ timezone date '+ % A, % B % e, % Y, at % l: % M % p') in $ nicetz82 83 exit 0 how the script works: this script uses the date command to display the date and time. In fact, the whole script is concerned with confirming the time zone name you have given so that it can run as scheduled at the end of the script. The most complex part of this script is the region name that is predicted to be input by the user and does not match any name in the time zone database. The time zone database will be sorted by region according to the time zone name. when a typical input problem occurs, the script will also try to report useful error information. For example, although TZ = "Casablanca" cannot find a matching region, the date command will demonstrate GMT, and the city Casablanca does exist in the regional database. The possible name, Africa/Casablanca, will be provided to this script. The script will find Casablanca in the African Directory and confirm the region correctly. Specify "Africa". on the other hand, the script knows that there are partitions and only provides information that is insufficient to specify a unique time zone. Finally, you can use a time zone name (such as UTC and WET) as the script parameter to view the defined time zone subset. Note: UTC is the abbreviation of Universal Time Coordinated. Beijing Time is 8 hours earlier than UTC Time. GMT (Greenwich Mean Time) is Greenwich Mean Time. Run the script: 01. /timein. sh02 It's Monday, July 22, 2012, at AM in UTC03 04. /timein. sh London05 It's Monday, May 25, 2012, at AM in Europe/London06 07. /timein. sh route il08 The region "Brazil" has more than one time zone. please use 'list' 09 to see all known regions and time zones.10 11. /timein. sh Pacific/Honolulu12 It's Sunday, July 22, 2012, at PM in Pacific/Honolulu13 14. /timein. sh WET15 It's Monday, March 13, 2012, at AM in WET16 17. /timein. sh mycloset18 Can't find an exact match for "mycloset ". please use 'list' 19 to see all known regions and time zones.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.