天天看點

date指令解析_學習筆記

時間:2017.11.14

作者:李強

參考:man,info,magedu講義

聲明:以下英文純屬個人翻譯,英文B級,歡迎糾正,盜版不糾,才能有限,希望不誤人子弟為好。

1、使用目的與場景

  列印或者修改系統日期時間system time。

2、官方說明

  Display or manipulate the history list.

3、寫在前面

  首先這裡有兩個概念System time 和 Hardware Clock。date最重要的就是對時間格式的了解,畢竟這裡不是在shell不是在X windows下,不能點點滑鼠就行。時間管理以後就使用ntp統一管理。等我學習後我會來這裡再補充。

4、修改檔案及涉及的環境變量

       /etc/localtime

      /usr/share/zoneinfo/*

      TZ

5、用法

Usage:

date [OPTION]... [+FORMAT]

date [-u|--utc|--universal] [ MMDDhhmm[[CC]YY][.ss] ]

直接執行顯示目前system time

預設顯示格式是 '+%a %b %e %H:%M:%S %Z %Y' 就是'Mon Nov 13 17:59:49 CST 2017'這樣子的格式。

 -d, --date=STRING 顯示時間描述的字元串,但不是now這種。有具體格式下面會說     

 -f, --file=DATEFILE 從datefile檔案中的每一行去顯示date,像--date 每次從檔案取一行

 -r, --reference=FILE 顯示檔案最後一次的修改時間

 -R, --rfc-2822 輸出時間以RFC 2822格式.  Example: Mon, 07 Aug 2006 12:34:56 -0600

 --rfc-3339=TIMESPEC 輸出時間以RFC 3339格式.時間和日期之間用空格隔開,

  TIMESPEC=‘date’, ‘seconds’, or ‘ns’ : 2006-08-07 12:34:56-06:00           

 -s, --set=STRING  用字元串單詞的方式來設定時間和-d的格式一樣

 -u, --utc, --universal 列印和設定UTC時間

  我們來說下-d 和-s中的STRING的格式,因為太多,自己可以參考下info資訊:

* General date syntax::             Common rules.

* Calendar date items::             19 Dec 1994.

* Time of day items::               9:20pm.

* Time zone items::                EST, PDT, GMT.

* Day of week items::               Monday and others.

* Relative items in date strings::     next tuesday, 2 years ago.

* Pure numbers in date strings::       19931219, 1440.

* Seconds since the Epoch::          @1078100502.

* Specifying time zone rules::        TZ="America/New_York", TZ="UTC0".

* Authors of get_date::             Bellovin, Eggert, Salz, Berets, et al.

ps .因為second代表的是s的機關,是以最好用數字表示表較好,+-表示往前往後,

比如:+2day -1day 這裡的day可以替換為year month day  hour minute second  am pm 

 數字前不加+,-  對于monday-sunday January-December 大小寫,簡寫都可以。

this 表示0 last表示-1,first,next表示1,因為second是機關是以就是2表示2,數字可以往後排,last和next還是挺直覺,其他我覺得阿拉伯數字還是挺直覺的。this 和first在當天時間都是saturday和sunday的時候使用都有問題。都是和next一個意思。這裡有待進一步考證。

date -d '+2day +1month' +%F/%T 時間往後,一個月2天後的時間。

date -d '+2hour +1minute +5second' 時間往後,設定為目前時間加上2小時1分鐘5秒。

date -d 'TZ="CET|Europe/Paris"'時區還是挺複雜的,還是用tzselect來檢視和設定,

這裡有一點要注意的是,比如今天是星期天sunday,那麼我想看下這個星期一的時間

因為我們一直在用date操作,是以我們可以用clock -s 來幫助我們快速回複我們修改的時間和硬體時間對齊。

關于時間同步的問題我們後面會學習ntp。

centos7中用timedatectl ,個人感覺7上的ctl功能還是不錯的,各個都是子產品化設定。內建一點比單一各有千秋吧。

   控制輸出的格式如下,常用就2個,其他就是查幫助吧:

       %%     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., 20)

       %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    +hh:mm numeric timezone (e.g., -04:00)

       %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)

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

       預設情況下,日期用0填充數字字段,下面的可以跟在‘%’後面:

       -      (hyphen)連字元,跟在%号後,就不會去填充0了

       _      (underscore)下劃線,跟在%号後加了個空格

       0      (zero) 0

       ^      upper 大寫字母,跟在%号後,變成大寫的格式顯示了

       #      opposite相反的格式,變成相反的格式顯示了

             本文轉自 lajifeiwomoshu 51CTO部落格,原文連結:http://blog.51cto.com/lajifeiwomoshu/1981829

上一篇: FTP服務

繼續閱讀