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.178linux.com/17378