linux的date命令及系统时间设置.doc_第1页
linux的date命令及系统时间设置.doc_第2页
linux的date命令及系统时间设置.doc_第3页
linux的date命令及系统时间设置.doc_第4页
linux的date命令及系统时间设置.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

linux 的date命令及系统时间设置Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。Linux中的所有命令(包括函数)都是采用的系统时钟设置。在Linux中,用于时钟查看和设置的命令主要有date、hwclock。1、date名称 : date使用权限 : 所有使用者使用方式 :date -u -d datestr -s datestr -utc -universal -date=datestr -set=datestr -help -version +FORMAT MMDDhhmmCCYY.ss说明 :date 可以用来显示或设定系统的日期与时间,在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中可用的标记列表如下 :时间方面 :% : 印出 %n : 下一行%t : 跳格%H : 小时(00-23)%I : 小时(01-12)%k : 小时(0-23)%l : 小时(1-12)%M : 分钟(00-59)%p : 显示本地 AM 或 PM%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss APM)%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数%S : 秒(00-60)%T : 直接显示时间 (24 小时制)%X : 相当于 %H:%M:%S%Z : 显示时区日期方面 :%a : 星期几 (Sun-Sat)%A : 星期几 (Sunday-Saturday)%b : 月份 (Jan-Dec)%B : 月份 (January-December)%c : 直接显示日期与时间%d : 日 (01-31)%D : 直接显示日期 (mm/dd/yy)%h : 同 %b%j : 一年中的第几天 (001-366)%m : 月份 (01-12)%U : 一年中的第几周 (00-53) (以 Sunday 为一周的第一天的情形)%w : 一周中的第几天 (0-6)%W : 一年中的第几周 (00-53) (以 Monday 为一周的第一天的情形)%x : 直接显示日期 (mm/dd/yy)%y : 年份的最后两位数字 (00.99)%Y : 完整年份 (0000-9999)若是不以加号作为开头,则表示要设定时间,而时间格式为 MMDDhhmmCCYY.ss,其中 MM 为月份,DD 为日,hh 为小时,mm 为分钟,CC 为年份前两位数字,YY 为年份后两位数字,ss 为秒数参数 :-d datestr : 显示 datestr 中所设定的时间 (非系统时间)-help : 显示辅助讯息-s datestr : 将系统时间设为 datestr 中所设定的时间-u : 显示目前的格林威治时间-version : 显示版本编号例子 :显示时间后跳行,再显示目前日期: #date +%T%n%D显示月份与日数: date +%B %d显示日期与设定时间(12:34:56): #date -date 12:34:56注意 :当你不希望出现无意义的 0 时(比如说 1999/03/07),则可以在标记中插入 - 符号,比如说 date +%-H:%-M:%-S 会把时分秒中无意义的 0 给去掉,像是原本的 08:09:04 会变为 8:9:4。另外,只有取得权限者(比如说 root)才能设定系统时间。当你以 root 身分更改了系统时间之后,请记得以 clock -w 来将系统时间写入 CMOS 中,这样下次重新开机时系统时间才会持续抱持最新的正确值。例子:修改日期时间在命令行输入:Date:显示当前时间 Fri Aug 3 14:15:16 CST 2007date s:按字符串方式修改时间可以只修改日期,不修改时间,输入: date -s 2007-08-03只修改时间,输入:date -s 14:15:00同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:#date -s 2007-08-03 14:15:002、查看硬件时间# hwclock设置硬件时间# hwclock -set -date=07/07/06 10:19 (月/日/年 时:分:秒)3、硬件时间和系统时间的同步按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步,但是在不重新启动的时候,需要用hwclock命令实现同步。硬件时钟与系统时钟同步:# hwclock -hctosys(hc代表硬件时间,sys代表系统时间)系统时钟和硬件时钟同步:(让系统的时间同步到硬件时钟)# hwclock systohc本文出自 “学习,感悟,经历” 博客Question:While playing around with GNU/date, this happened: # dateWed Jul 22 10:24:05 CEST 2009# date -set=1970-01-01date: cannot set date: Invalid argumentThu Jan 1 00:00:00 CET 1970So, date gives an error but still modifies the system time? Lets have a closer look: # strace -ff -F date -set=1970-01-01 2&1 | grep settclock_settime(CLOCK_REALTIME, 18446744073709548016, 0) = -1 EINVAL (Invalid argument)settimeofday(18446744073709548016, 0, NULL) = -1 EINVAL (Invalid argument)Wow, scary stuff. However, after a bit more thinking and searching we try again, a bit different this time: # TZ=GMT date -set=1970-01-01Thu Jan 1 00:00:00 GMT 1970# dateThu Jan 1 01:00:04 CET 1970Oh, and while were at it: it looks like openSolaris comes with GNU/date as its default date(1) command now. Its three years old already (from GNU/coreutils 6.7), but at least we are a bit more flexible when it comes to displaying date & time. On Tue, Sep 9, 2008 at 1:47 PM, Enrique Arizn Benito wrote: Upps, I forgot it. #date -s 1970-01-01 00:00:01 date: cannot set date: Invalid argumentIt looks like date is simply reporting an error that it received fromthe operating system. The strace utility (or some platform-specificreplacement if you are not using Linux) should be able to confirmthis. Thu Jan 1 00:00:01 CET 1970 Curiosly after the Invalid argument error, date properly prints the new hour but doesnt change it.This is almost certainly because the date program understands the dateyou refer to, but failed in its attempt to set the system clock tothat value. Thats what makes me think its really a bug.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论