在Linux的shell裡面,觀看目前時間的指令是date,如:

$ date
Sat Jan  8 15:34:16 CST 2011
$

 

由結果可以看出來,他並不只限於『日期』,連時間一併都顯示出來。這與DOS將日期,時間分別用date, time 兩個指令是不一樣的。不過date能做的事情遠多於此,先就用法來看:

標準版:

$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
...[略]

busybox 版:

# date --help
BusyBox v1.11.1 (2010-12-23 19:34:38 CST) multi-call binary

Usage: date [OPTION]... [+FMT] [TIME]

Display time (using +FMT), or set time
...[略]

 

busybox 在功能上比標準版略少,不過以下介紹的常用功能是二者都支援的。

    以特定格式顯示

當date後面加上一個加號(+),緊接著date特定的格式時,就可以用來決定日期時間的輸出格式。把date所支援的格式

FORMAT controls the output.  The only valid option for the second form
specifies Coordinated Universal Time.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 21)
  %d   day of month (e.g, 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)

  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

 

看起來落落長的一張表格,是為了提供充分的功能以應付各種場合,其中紅色的部分是busybox版不支援的。

話不多說,直接來看幾個例子:

$ date
Sat Jan  8 16:32:05 CST 2011
$ date +%x
01/08/2011
$ date %X
date: invalid date `%X' #忘記寫加號就會得到這樣的提示
$ date +%X
04:32:22 PM
$ date +%a
Sat
$ date +%A
Saturday
$ date +%D
01/08/11
$ date +%F
2011-01-08
$

 

如果你要寫得稍微複雜一點的話,就要加上雙引號(")把你的格式包住,如下:

$ date +"Hello world. It's: %Y/%m/%d %H:%M:%S"
Hello world. It's: 2011/01/08 16:37:34
$


有一個很特別的格式,通常在寫shell script 時會用到,那就是%s,它會顯示從1970/1/1至今所經過的秒數,如果把兩個不同時間點的秒數相減,就可以得到時間差(下面會有詳細介紹):

$ date +%s
1294476056
$

 

題外話,這個輸出似乎與說明不符(不應該輸出PM,有可能是我手上標準版的bug(

GNU coreutils 6.9                 March 2007                           DATE(1)

),busybox反而沒這個問題)

$ date +%X
04:32:22 PM
$

 

    設定系統時間

首先,你要知道只有root才能設定系統時間,否則你會得到”Operation not permitted”的錯誤訊息,系統時間還是沒變:

$ date 01020101
date: cannot set date: Operation not permitted
Sun Jan  2 01:01:00 CST 2011
$

 

通常,我們不會在PC Linux上面做這件事情,這只會在Embedded  板子上面做。所以讓我們來看看busybox上面的命令格式:

# date --help
    ...[略]
Recognized formats for TIME:
        hh:mm[:ss]
        [YYYY.]MM.DD-hh:mm[:ss]
        YYYY-MM-DD hh:mm[:ss]
        MMDDhhmm[[YY]YY][.ss]

#

 

busybox 提供了四種主要格式,每種格式都還可以省略某些設定。老實說,算是蠻囉唆的。所以呢,只要記得如何叫出說明,到時按圖索驥就好啦。以下幾個設定實例:

1. 設定時間到 5:35

# date
Mon Jan 10 07:03:27 UTC 2011
# date 5:35
Mon Jan 10 05:35:00 UTC 2011
#

 

2. 設定日期時間到 5月8日 0:00

# date
Mon Jan 10 05:36:32 UTC 2011

# date 05080000
Sun May  8 00:00:00 UTC 2011
#

 

3. 設定日期時間到 2011年1月11日晚上8:00

# date
Mon Jan 10 05:36:32 UTC 2011
# date 2011.01.11-20:00
Tue Jan 11 20:00:00 UTC 2011
#

 

 

    計算花費時間

有時候我們會利用shell script來做測試,或者監控系統,這時候如果能知道從測試開始到目前為止花了多少時間,是一件非常方便的事情。

利用剛剛提到的 『date +%s』,加上一點點計算,可以用來計算shell script 所花費的時間,例如:

$ cat elap.bash
#!/bin/bash
function elapsed_time()
{
    s_time=$1
    e_time=$2

    elap_s=$((e_time-s_time))
    ss=$((elap_s%60))
    mm=$(((elap_s/60)%60))
    hh=$((elaps/3600))
    printf "%i:%02i:%02i" $hh $mm $ss
}

begin_time=`date "+%s"`

for i in {1..3};
do
    now_time=`date "+%s"`
    elap_time=`elapsed_time $begin_time $now_time`
    echo "elapsed time=$elap_time"
    sleep 1
done


『elapsed_time』這個function,讀入兩個由『date +%s』所產生的秒數,經過簡單的運算之後 hh, mm, ss 分別代表所經過的小時,分鐘與秒數。最後再利用printf的格式化輸出,轉成 h:mm:ss 的格式。因為有時候我們所做的測試可能會超過24小時,而這邊我並沒有處理『天』這個概念,所以『小時』的部分並沒有轉換成 hh 格式。

這個簡單的script,每隔一秒鐘呼叫『elapsed_time』一次,並印出所花費的時間。

執行結果如下:

$ source elap.bash
elapsed time=0:00:00
elapsed time=0:00:01
elapsed time=0:00:02
$

arrow
arrow
    全站熱搜

    dragonspring 發表在 痞客邦 留言(0) 人氣()