Linux学习之十-Linux系统时间

时间:2023-12-28 15:53:32

Linux系统时间

1、date命令用于查看以及修改Linux系统的时间,关于date命令的详细帮助文档如下

[root@localhost ~]# 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.

 

-d, --date=STRING display time described by STRING, not `now'

-f, --file=DATEFILE like --date once for each line of DATEFILE

-r, --reference=FILE display the last modification time of FILE

-R, --rfc-2822 output date and time in RFC 2822 format.

Example: Mon, 07 Aug 2006 12:34:56 -0600

--rfc-3339=TIMESPEC output date and time in RFC 3339 format.

TIMESPEC=`date', `seconds', or `ns' for

date and time to the indicated precision.

Date and time components are separated by

a single space: 2006-08-07 12:34:56-06:00

-s, --set=STRING set time described by STRING

-u, --utc, --universal print or set Coordinated Universal Time

--help display this help and exit

--version output version information and exit

 

FORMAT controls the output. 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., 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 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

 

After any flags comes an optional field width, as a decimal number;

then an optional modifier, which is either

E to use the locale's alternate representations if available, or

O to use the locale's alternate numeric symbols if available.

 

Report date bugs to bug-coreutils@gnu.org

GNU coreutils home page: <http://www.gnu.org/software/coreutils/>

General help using GNU software: <http://www.gnu.org/gethelp/>

For complete documentation, run: info coreutils 'date invocation'

date常用格式及参数说明

date +%F === date +%Y-%m-%d    显示年月日

[root@localhost ~]# date +%F

2018-04-13

[root@localhost ~]# date +%Y-%m-%d

2018-04-13

date +%T === date +%H-%M-%S    显示时分秒

[root@localhost ~]# date +%T

21:39:42

[root@localhost ~]# date +%H-%M-%S

21-39-49

date +%F\ %T    显示年月日时分秒

[root@localhost ~]# date +%F\ %T

2018-04-13 21:41:23

date +%y    显示两位年份

[root@localhost ~]# date +%y

18

date +%Y    显示四位年份

[root@localhost ~]# date +%Y

2018

date +%m     显示月份

[root@localhost ~]# date +%m

04

date+%M     显示分钟

[root@localhost ~]# date +%M

42

date +%d     显示日

[root@localhost ~]# date +%d

13

date+%p或%P     显示出AM(am)或PM(pm)

[root@localhost ~]# date +%P

pm

[root@localhost ~]# date +%p

PM

date +%u     显示周几

[root@localhost ~]# date +%u

5

date +%H     小时24小时制(00~23)

[root@localhost ~]# date +%H

21

date +%I     小时12小时制(01~12)

[root@localhost ~]# date +%l

9

date+ %j     显示一年的第几天

[root@localhost ~]# date +%j

103

date+%W    显示一年的第几个星期

[root@localhost ~]# date +%W

15

date -d '-3day'    显示前三天的日期

[root@localhost ~]# date -d '-3day'

Tue Apr 10 21:45:46 CST 2018

date -s '20180401 08:20:00'    设置时间

[root@localhost ~]# date -s '20180401 08:20:00'

Sun Apr 1 08:20:00 CST 2018

 

2、同步时间命令ntpdate

ntp === network time protocol网络时间协议

利用时间服务器(阿里云等),也可以将此命令添加到定时任务中,让Linux系统定时同步时间

ntpdate ntp1.aliyun.com

ntpdate ntp2.aliyun.com

ntpdate ntp3.aliyun.com

ntpdate ntp4.aliyun.com

ntpdate ntp5.aliyun.com

ntpdate ntp6.aliyun.com

 

3、cal 显示日历

-1 显示一个月的月历

-3 显示系统前一个月,当前月,下一个月的月历

-s 显示星期天为一个星期的第一天,默认的格式

-m 显示星期一为一个星期的第一天

-j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)

-y 显示当前年份的日历的所有年月份

[root@localhost ~]# cal -y

2018

 

January February March

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 1 2 3 1 2 3

7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10

14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17

21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24

28 29 30 31 25 26 27 28 25 26 27 28 29 30 31

 

April May June

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7 1 2 3 4 5 1 2

8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9

15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16

22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23

29 30 27 28 29 30 31 24 25 26 27 28 29 30

 

July August September

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 7 1 2 3 4 1

8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8

15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15

22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22

29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29

30

October November December

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 6 1 2 3 1

7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8

14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15

21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22

28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29

30 31

 

4、hwclock命令是一个硬件时钟访问工具,它可以显示当前时间、设置硬件时钟的时间和设置硬件时钟为系统时间,也可设置系统时间为硬件时钟的时间。

在Linux中有硬件时钟与系统时钟等两种时钟。

硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟。

系统时钟(软件时钟)则是指kernel中的时钟。当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作。所有Linux相关指令与函数都是读取系统时钟的设定。

参数说明:

--adjust:hwclock每次更改硬件时钟时,都会记录在/etc/adjtime文件中。使用--adjust参数,可使hwclock根据先前的记录来估算硬件时钟的偏差,并用来校正目前的硬件时钟;

--debug:显示hwclock执行时详细的信息

--directisa:hwclock预设从/dev/rtc设备来存取硬件时钟。若无法存取时,可用此参数直接以I/O指令来存取硬件时钟

--hctosys:将系统时钟调整为与目前的硬件时钟一致

--systohc:将硬件时钟调整为与目前的系统时钟一致

--set --date=<日期与时间>:设定硬件时钟

--show:显示硬件时钟的时间与日期

--test:仅测试程序,而不会实际更改硬件时钟

--utc:若要使用格林威治时间,请加入此参数,hwclock会执行转换的工作

--version:显示版本信息

 

参考来源:http://man.linuxde.net/hwclock