📄 timedate.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Time / Date Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="Complex Commands"HREF="moreadv.html"><LINKREL="NEXT"TITLE="Text Processing Commands"HREF="textproc.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="moreadv.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="textproc.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="TIMEDATE"></A>15.3. Time / Date Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="TDLISTING1"></A>Time/date and timing</B></P><DL><DT><ANAME="DATEREF"></A><BCLASS="COMMAND">date</B></DT><DD><P>Simply invoked, <BCLASS="COMMAND">date</B> prints the date and time to <TTCLASS="FILENAME">stdout</TT>. Where this command gets interesting is in its formatting and parsing options.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX51"></A><P><B>Example 15-10. Using <ICLASS="FIRSTTERM">date</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Exercising the 'date' command 3 4 echo "The number of days since the year's beginning is `date +%j`." 5 # Needs a leading '+' to invoke formatting. 6 # %j gives day of year. 7 8 echo "The number of seconds elapsed since 01/01/1970 is `date +%s`." 9 # %s yields number of seconds since "UNIX epoch" began, 10 #+ but how is this useful? 11 12 prefix=temp 13 suffix=$(date +%s) # The "+%s" option to 'date' is GNU-specific. 14 filename=$prefix.$suffix 15 echo $filename 16 # It's great for creating "unique" temp filenames, 17 #+ even better than using $$. 18 19 # Read the 'date' man page for more formatting options. 20 21 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <TTCLASS="OPTION">-u</TT> option gives the UTC (Universal Coordinated Time).</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>date</B></TT> <TTCLASS="COMPUTEROUTPUT">Fri Mar 29 21:07:39 MST 2002</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>date -u</B></TT> <TTCLASS="COMPUTEROUTPUT">Sat Mar 30 04:07:42 UTC 2002</TT> </PRE></TD></TR></TABLE> </P><P>This option facilitates calculating the time between different dates.</P><DIVCLASS="EXAMPLE"><HR><ANAME="DATECALC"></A><P><B>Example 15-11. <ICLASS="FIRSTTERM">Date</I> calculations</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # date-calc.sh 3 # Author: Nathan Coulter 4 # Used in ABS Guide with permission (thanks!). 5 6 MPHR=60 # Minutes per hour. 7 HPD=24 # Hours per day. 8 9 diff () { 10 printf '%s' $(( $(date -u -d"$TARGET" +%s) - 11 $(date -u -d"$CURRENT" +%s))) 12 # %d = day of month. 13 } 14 15 16 CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z') 17 TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z') 18 # %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone. 19 20 printf '\nIn 2007, %s ' \ 21 "$(date -d"$CURRENT + 22 $(( $(diff) /$MPHR /$MPHR /$HPD / 2 )) days" '+%d %B')" 23 # %B = name of month ^ halfway 24 printf 'was halfway between %s ' "$(date -d"$CURRENT" '+%d %B')" 25 printf 'and %s\n' "$(date -d"$TARGET" '+%d %B')" 26 27 printf '\nOn %s at %s, there were\n' \ 28 $(date -u -d"$CURRENT" +%F) $(date -u -d"$CURRENT" +%T) 29 DAYS=$(( $(diff) / $MPHR / $MPHR / $HPD )) 30 CURRENT=$(date -d"$CURRENT +$DAYS days" '+%F %T.%N %Z') 31 HOURS=$(( $(diff) / $MPHR / $MPHR )) 32 CURRENT=$(date -d"$CURRENT +$HOURS hours" '+%F %T.%N %Z') 33 MINUTES=$(( $(diff) / $MPHR )) 34 CURRENT=$(date -d"$CURRENT +$MINUTES minutes" '+%F %T.%N %Z') 35 printf '%s days, %s hours, ' "$DAYS" "$HOURS" 36 printf '%s minutes, and %s seconds ' "$MINUTES" "$(diff)" 37 printf 'until Christmas Dinner!\n\n' 38 39 # Exercise: 40 # -------- 41 # Rewrite the diff () function to accept passed parameters, 42 #+ rather than using global variables.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="DATERANDREF"></A></P><P>The <ICLASS="FIRSTTERM">date</I> command has quite a number of <ICLASS="FIRSTTERM">output</I> options. For example <TTCLASS="OPTION">%N</TT> gives the nanosecond portion of the current time. One interesting use for this is to generate six-digit random integers. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 date +%N | sed -e 's/000$//' -e 's/^0//' 2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3 # Strip off leading and trailing zeroes, if present.</PRE></TD></TR></TABLE> </P><P>There are many more options (try <BCLASS="COMMAND">man date</B>).</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 date +%j 2 # Echoes day of the year (days elapsed since January 1). 3 4 date +%k%M 5 # Echoes hour and minute in 24-hour format, as a single digit string. 6 7 8 9 # The 'TZ' parameter permits overriding the default time zone. 10 date # Mon Mar 28 21:42:16 MST 2005 11 TZ=EST date # Mon Mar 28 23:42:16 EST 2005 12 # Thanks, Frank Kannemann and Pete Sjoberg, for the tip. 13 14 15 SixDaysAgo=$(date --date='6 days ago') 16 OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a month). 17 OneYearAgo=$(date --date='1 year ago')</PRE></TD></TR></TABLE></P><P>See also <AHREF="special-chars.html#EX58">Example 3-4</A>.</P></DD><DT><ANAME="ZDUMPREF"></A><BCLASS="COMMAND">zdump</B></DT><DD><P>Time zone dump: echoes the time in a specified time zone.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>zdump EST</B></TT> <TTCLASS="COMPUTEROUTPUT">EST Tue Sep 18 22:09:22 2001 EST</TT> </PRE></TD></TR></TABLE> </P></DD><DT><ANAME="TIMREF"></A><BCLASS="COMMAND">time</B></DT><DD><P>Outputs very verbose timing statistics for executing a command.</P><P><TTCLASS="USERINPUT"><B>time ls -l /</B></TT> gives something like this:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">0.00user 0.01system 0:00.05elapsed 16%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (149major+27minor)pagefaults 0swaps</TT></PRE></TD></TR></TABLE> </P><P>See also the very similar <AHREF="internal.html#TIMESREF">times</A> command in the previous section.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>As of <AHREF="bash2.html#BASH2REF">version 2.0</A> of Bash, <BCLASS="COMMAND">time</B> became a shell reserved word, with slightly altered behavior in a pipeline.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="TOUCHREF"></A><BCLASS="COMMAND">touch</B></DT><DD><P>Utility for updating access/modification times of a file to current system time or other specified time, but also useful for creating a new file. The command <TTCLASS="USERINPUT"><B>touch zzz</B></TT> will create a new file of zero length, named <TTCLASS="FILENAME">zzz</TT>, assuming that <TTCLASS="FILENAME">zzz</TT> did not previously exist. Time-stamping empty files in this way is useful for storing date information, for example in keeping track of modification times on a project. </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <BCLASS="COMMAND">touch</B> command is equivalent to <TTCLASS="USERINPUT"><B>: >> newfile</B></TT> or <TT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -