Linux系统的时间、日期和月历命令的使用方法

一、开场白


Linux(Unix)系统、Window系统和Mac系统是当今世界上流程的三大电脑系统,各有特色,但也相互学习、进化和发展,对于编程人员可能更喜欢命令行式的Linux系统。
我也是其中一个,痴迷于手指在键盘上飞驰的感觉,更喜欢Linux系统上那些优秀的编程工具、语言和精巧的程序功能,感觉这是人类的一大智慧财富,不得不学!
Linux系统上的命令表面上是一个单词或者一行代码,背后其实是由很多代码、脚本和程序构成的,千万不要简单的就认为它是一个单词。

你可能会问,学这些干啥呢?当然用来解决现实的问题喽(男性的思维方式是解决问题)!

下面的内容如果对你有帮助,我将非常开心!

二、测试系统


1.测试的Linux系统信息:CentOS Linux release 7.7.1908 (Core)

2.内核版本:
Linux version 3.10.0-862.9.1.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Mon Jul 16 16:29:36 UTC 2018

 

三、Linux命令


今天的主要讲解三个Linux系统中直接与时间相关的命令:time、date、cal
1.先看第一个命令:time
(1)命令帮助信息
time命令-用于评估测量指令、脚本运行所消耗的时间
我们先看一下time命令的帮助信息:执行命令man time,退出的话:执行q或Q即可,是单词quit简写。下面是关于time命令的man帮助信息,还可以用help和info的方式查询,都有关于time的帮助信息,但是man比较精炼,容易理解。
后面我会更新关于linux命令帮助信息查询、获取的专题文章的,敬请期待!

time(n)                                  Tcl Built-In Commands                                  time(n)

_______________________________________________________________________________________________________

NAME
       time - Time the execution of a script
翻译:时间-脚本执行的时间

SYNOPSIS
       time script ?count?
_________________________________________________________________

DESCRIPTION
       This  command  will call the Tcl interpreter count times to evaluate script (or once if count is not specified).  It will then return a string of the form 503 microseconds per iteration which indicates the average amount of time required per iteration,  in  microseconds.   Time  is measured in elapsed time, not CPU time.

说明:这个命令是调用Tcl 解释器评估命令、脚本运行的耗时。然后,它将返回每迭代503微秒形式的字符串,该字符串指示每次迭代所需的平均时间量,以微秒为单位。时间以经过时间而不是CPU时间为单位进行度量。

TCL,称为“工具命令语言”"Tool Command Language",是一种很通用的脚本语言,功能强大,发音为"tickle”。TCL经常被用于快速原型开发,脚本编程,GUI和测试等方面。

EXAMPLE
       Estimate how long it takes for a simple Tcl for loop to count to a thousand:
翻译:估计一个简单的tcl for循环需要多长时间才能计数到1000。
              time {
                  for {set i 0} {$i<1000} {incr i} {
                      # empty body
                  }
              }
SEE ALSO
       clock(n)

KEYWORDS
       script, time

Tcl                                                                                             time(n)

(2)使用方法:根据例子很简单,直接time command即可,例如,我想评估一下命令sleep 3的执行耗时:time sleep 3

real    0m3.001s
user    0m0.000s
sys    0m0.001s
截屏如下:


还比如,我想测量一下脚本运行的时间,先创建一个测试脚本,如下:
[root@hlm-0001 20191207]# cat test.sh
#!/bin/bash
var1=10
while [ $var1 -gt 0 ] # whlie条件循环,-gt是大于的意思
do
    echo $var1
    var1=$[ $var1 -1 ]
done

然后保存脚本文件,执行:time sh test.sh
[root@hlm-0001 20191207]# time sh test.sh
10
9
8
7
6
5
4
3
2
1

real    0m0.002s
user    0m0.002s
sys    0m0.000s

截屏如下:

耗时:real为0.002s,也即是2ms
秒的单位尺度有:秒、毫秒、微秒和纳秒,进制是1000(10^3)
1秒(s)=1000毫秒(ms)
1秒(s)=1000000微秒(μs)
1秒(s)=1000000000纳秒(ns)

2.第二个命令:date
date命令-显示系统日期与时间,可以用来显示或设定系统的日期与时间,可以个性化定制欲显示的时间和日期的显示格式

(1)命令帮助信息(客官,您做好心理准备,很长很长~~~)
DATE(1)                                      User Commands                                      DATE(1)

NAME
       date - print or set the system date and time
翻译:日期-打印或设置系统日期和时间

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

       Mandatory arguments to long options are mandatory for short options too.

翻译:以给定格式显示当前时间,或设置系统日期。强制参数的强制性参数对于长短选项option是一样的

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

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

       -I[TIMESPEC], --iso-8601[=TIMESPEC]
              output  date/time  in  ISO  8601  format.   TIMESPEC='date'  for date only (the default),
              'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision.

       -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 (UTC)
打印输出或者设置通用国际时间(显示目前的格林威治时间)
       --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, 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)

       %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.

ENVIRONMENT
       TZ     Specifies the timezone, unless overridden by command  line  parameters.   If  neither  is specified, the setting from /etc/localtime is used.

翻译:指定时区,除非被命令行参数覆盖。如果没有指定,则使用/etc/localtime中的设置。

EXAMPLES
       Convert seconds since the epoch (1970-01-01 UTC) to a date

              $ date --date='@2147483647'

       Show the time on the west coast of the US (use tzselect(1) to find TZ)

              $ TZ='America/Los_Angeles' date

       Show the local time for 9AM next Friday on the west coast of the US

              $ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

       GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Report date translation bugs
       to <http://translationproject.org/team/>

DATE STRING
       The --date=STRING is a mostly free format human readable date string such as "Sun, 29  Feb  2004
       16:21:42  -0800"  or  "2004-02-29  16:21:42" or even "next Thursday".  A date string may contain items indicating calendar date, time of day, time zone, day of  week,  relative  time,  relative date,  and numbers.  An empty string indicates the beginning of the day.  The date string format is more complex than is easily documented here but is fully described in the info documentation.
翻译:
日期字符串可能包含指示日历日期、日时间、时区、周中日、相对时间、相对日期和数字的项。空字符串表示一天的开始。日期字符串格式比这里容易记录的要复杂,但是在INFO文档中有完整的描述。

AUTHOR
       Written by David MacKenzie.

COPYRIGHT
       Copyright © 2013 Free Software Foundation, Inc.  License GPLv3+: GNU  GPL  version  3  or  later
       <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the
       extent permitted by law.

SEE ALSO
       The full documentation for date is maintained as a Texinfo manual.  If the info  and  date  programs are properly installed at your site, the command 
info coreutils 'date invocation' 
should give you access to the complete manual.


GNU coreutils 8.22                            August 2019                                       DATE(1)

(2)使用方法,分为三个部分:显示日期、显示时间和设置日期
①显示日期:
%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)

常用方式举例:
[root@hlm-0001 20191207]# date +%F
2019-12-07
[root@hlm-0001 20191207]# date +%a
Sat
[root@hlm-0001 20191207]# date +%A
Saturday
[root@hlm-0001 20191207]# date +%b
Dec
[root@hlm-0001 20191207]# date +%B
December
[root@hlm-0001 20191207]# date +%c
Sat 07 Dec 2019 08:32:53 PM CST
[root@hlm-0001 20191207]# date +%d
07
[root@hlm-0001 20191207]# date +%D
12/07/19
[root@hlm-0001 20191207]# date +%h
Dec
[root@hlm-0001 20191207]# date +%j
341
[root@hlm-0001 20191207]# date +%m
12
[root@hlm-0001 20191207]# date +%U
48
[root@hlm-0001 20191207]# date +%w
6
[root@hlm-0001 20191207]# date +%W
48
[root@hlm-0001 20191207]# date +%x
12/07/2019
[root@hlm-0001 20191207]# date +%y
19
[root@hlm-0001 20191207]# date +%Y
2019

②显示时间:
% : 印出 %
%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 [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00..61)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区

常用方式举例:
[root@hlm-0001 20191207]# date +%n


[root@hlm-0001 20191207]# date +%t

[root@hlm-0001 20191207]# date +%tn
    n
[root@hlm-0001 20191207]# date +%tjingxinxing
    jingxinxing
[root@hlm-0001 20191207]# date +%H
20
[root@hlm-0001 20191207]# date +%I
08
[root@hlm-0001 20191207]# date +%k
20
[root@hlm-0001 20191207]# date +%l
 8
[root@hlm-0001 20191207]# date +%M
37
[root@hlm-0001 20191207]# date +%p
PM
[root@hlm-0001 20191207]# date +%r
08:37:36 PM
[root@hlm-0001 20191207]# date +%s
1575722289
[root@hlm-0001 20191207]# date +%S
19
[root@hlm-0001 20191207]# date +%T
20:38:25
[root@hlm-0001 20191207]# date +%X
08:38:33 PM
[root@hlm-0001 20191207]# date +%Z
CST

③设置日期
若是date命令后不以加号作为开头(+),则表示要设定时间,而时间格式为 MMDDhhmm[[CC]YY][.ss],其中 MM 为月份,DD 为日,hh 为小时,mm 为分钟,CC 为年份前两位数字,YY 为年份后两位数字,ss 为秒数。
只有取得权限者(比如说 root)才能设定系统时间。
注意,当你以 root 身分更改了系统时间之后,请记得以 clock -w 来将系统时间写入 CMOS 中,这样下次重新开机时系统时间才会持续保持最新的正确日期时间值。

3.第三个命令:cal
(1)帮助信息
[root@hlm-0001 20191207]# cal --help

Usage:
 cal [options] [[[day] month] year]

Options:
 -1, --one        show only current month (default) 仅显示当前月份的日历
 -3, --three      show previous, current and next month 显示上个月、本月和下个月的日历
 -s, --sunday     Sunday as first day of week 周日为一周的第一天
 -m, --monday     Monday as first day of week 周一为一周的第一天
 -j, --julian     output Julian dates 输出Julian日,即月历中每年在一年中的日数
 -y, --year       show whole current year 显示当前整年的月历
 -V, --version    display version information and exit 显示版本信息及退出
 -h, --help       display this help text and exit 显示帮助信息及退出

(2)使用方法
①显示当前月份的日历,执行命令:cal -1

②显示上个月、本月和下个月的日历,执行命令:cal -3

③周日为一周的第一天,执行命令:cal -s

④周一为一周的第一天,执行命令:cal -m

⑤输出Julian日,即月历中每年在一年中的日数,执行命令:cal -j

⑥显示当前整年的月历,执行命令:cal -y


⑦显示版本信息及退出,执行cal -V
⑧显示帮助信息及退出,执行cal -h


最后,祝大家能从以上的内容学到一些东西,有所收获!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值