date命令解析_学习笔记

简介: 时间:2017.11.14作者:李强参考:man,info,magedu讲义声明:以下英文纯属个人翻译,英文B级,欢迎纠正,盗版不纠,才能有限,希望不误人子弟为好。1、使用目的与场景  打印或者修改系统日期时间system time。

时间: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   +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)


       默认情况下,日期用0填充数字字段,下面的可以跟在‘%’后面:


       -      (hyphen)连字符,跟在%号后,就不会去填充0了

       _      (underscore)下划线,跟在%号后加了个空格

       0      (zero) 0

       ^      upper 大写字母,跟在%号后,变成大写的格式显示了

       #      opposite相反的格式,变成相反的格式显示了




             



 

相关文章
|
22天前
|
Unix Linux Shell
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
【Shell 编程指南 日期命令】Date命令:显示与设置系统时间和日期
48 0
|
2月前
|
运维 大数据 Shell
时间命令date,你真的会用了吗?
时间命令date,你真的会用了吗?
41 0
|
4月前
|
Linux
Linux命令(104)之date
Linux命令(104)之date
33 1
|
1月前
|
SQL HIVE
Hive中日期处理函数的使用(date_format、date_add、date_sub、next_day)
Hive中日期处理函数的使用(date_format、date_add、date_sub、next_day)
65 3
|
2月前
date命令的使用
date -d "-1 day" +%F
41 6
|
12月前
|
机器学习/深度学习 测试技术 Python
Python date,datetime,time等相关操作总结
Python date,datetime,time等相关操作总结
88 0
|
Oracle 关系型数据库 MySQL
Mysql date_to_str()函数 日期格式化详细解析
Mysql date_to_str()函数 日期格式化详细解析