linux有兩個時間:
系統時間和硬件時間
系統時間:
最簡單的使用date命令顯示當前系統的時間,命令行輸入date。
# date
Thu Jun 2 17:11:48 CST 2016
date的具體使用方式:
1. date格式顯示:date [OPTION]… [+FORMAT]
參考如下參數,使用方式例如:
# date +%y
16
# date +%y%m%d
160602
# date +%y%m%d%H%M%S
160602163503
如上例子一般用在自動備份時,使備份文件的名字上加上日期和時間以便查看。其他格式使用方法類似
格式中和可以加入其他字符和符號,例如:
# date +%y-%m-%d
16-06-02
# date +%yY-%mM-%dD
16Y-06M-02D
%% 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, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%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 time zone (e.g., -0400)
%:z +hh:mm numeric time zone (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)
2. 時間設置:date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]
先解釋一下后面部分
[MMDDhhmm[[CC]YY][.ss]]
月日時分 年 .秒
其中年和秒可以省略
例如:
date 06021712
設定時間為6月2日17點12分
date 0602171216.30 等價于date 060217122016.30
設定時間為16年6月2日17點12分30秒
硬件時間:
首先思考一個問題,為什么linux關機后時間依然在走?電腦的主板上有一塊電池為主板的晶體振蕩器可保證主板上的時間在系統關閉之后依然再走,下次開機時系統時間就讀取硬件時鐘作為系統的時間。
這里還需要考慮一個問題,而硬件時間和系統時間是單獨走的。上面提到的date修改的是系統時間,但不能同步到硬件時間,所以關機之后系統時間失效,此時如何讓硬件時間同步系統時間,這個就需要一個命令hwclock
hwlcock -w
如果需要將硬件時間同步到系統時間也是可以的:
hwlcock -s
原創文章,作者:Net20_天意,如若轉載,請注明出處:http://www.www58058.com/17378