1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5
>Time / Date Commands</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
11
TITLE="Advanced Bash-Scripting Guide"
12
HREF="index.html"><LINK
14
TITLE="External Filters, Programs and Commands"
15
HREF="external.html"><LINK
17
TITLE="Complex Commands"
18
HREF="moreadv.html"><LINK
20
TITLE="Text Processing Commands"
21
HREF="textproc.html"><META
22
HTTP-EQUIV="Content-Style-Type"
23
CONTENT="text/css"><LINK
25
HREF="common/kde-common.css"
27
HTTP-EQUIV="Content-Type"
28
CONTENT="text/html; charset=iso-8859-1"><META
29
HTTP-EQUIV="Content-Language"
32
HREF="common/kde-localised.css"
34
TITLE="KDE-English"><LINK
36
HREF="common/kde-default.css"
38
TITLE="KDE-Default"></HEAD
46
STYLE="font-family: sans-serif;"
50
SUMMARY="Header navigation table"
59
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
75
>Chapter 16. External Filters, Programs and Commands</TD
97
>16.3. Time / Date Commands</H1
105
>Time/date and timing</B
121
> prints the date and
125
>. Where this command gets
126
interesting is in its formatting and parsing options.</P
134
>Example 16-10. Using <I
146
CLASS="PROGRAMLISTING"
148
2 # Exercising the 'date' command
150
4 echo "The number of days since the year's beginning is `date +%j`."
151
5 # Needs a leading '+' to invoke formatting.
152
6 # %j gives day of year.
154
8 echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
155
9 # %s yields number of seconds since "UNIX epoch" began,
156
10 #+ but how is this useful?
159
13 suffix=$(date +%s) # The "+%s" option to 'date' is GNU-specific.
160
14 filename=$prefix.$suffix
161
15 echo "Temporary filename = $filename"
162
16 # It's great for creating "unique and random" temp filenames,
163
17 #+ even better than using $$.
165
19 # Read the 'date' man page for more formatting options.
176
> option gives the UTC (Universal
177
Coordinated Time).</P
197
CLASS="COMPUTEROUTPUT"
198
>Fri Mar 29 21:07:39 MST 2002</TT
213
CLASS="COMPUTEROUTPUT"
214
>Sat Mar 30 04:07:42 UTC 2002</TT
223
>This option facilitates calculating the time between
244
CLASS="PROGRAMLISTING"
246
2 # date-calc.sh
247
3 # Author: Nathan Coulter
248
4 # Used in ABS Guide with permission (thanks!).
250
6 MPHR=60 # Minutes per hour.
251
7 HPD=24 # Hours per day.
254
10 printf '%s' $(( $(date -u -d"$TARGET" +%s) -
255
11 $(date -u -d"$CURRENT" +%s)))
256
12 # %d = day of month.
260
16 CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z')
261
17 TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z')
262
18 # %F = full date, %T = %H:%M:%S, %N = nanoseconds, %Z = time zone.
264
20 printf '\nIn 2007, %s ' \
265
21 "$(date -d"$CURRENT +
266
22 $(( $(diff) /$MPHR /$MPHR /$HPD / 2 )) days" '+%d %B')"
267
23 # %B = name of month ^ halfway
268
24 printf 'was halfway between %s ' "$(date -d"$CURRENT" '+%d %B')"
269
25 printf 'and %s\n' "$(date -d"$TARGET" '+%d %B')"
271
27 printf '\nOn %s at %s, there were\n' \
272
28 $(date -u -d"$CURRENT" +%F) $(date -u -d"$CURRENT" +%T)
273
29 DAYS=$(( $(diff) / $MPHR / $MPHR / $HPD ))
274
30 CURRENT=$(date -d"$CURRENT +$DAYS days" '+%F %T.%N %Z')
275
31 HOURS=$(( $(diff) / $MPHR / $MPHR ))
276
32 CURRENT=$(date -d"$CURRENT +$HOURS hours" '+%F %T.%N %Z')
277
33 MINUTES=$(( $(diff) / $MPHR ))
278
34 CURRENT=$(date -d"$CURRENT +$MINUTES minutes" '+%F %T.%N %Z')
279
35 printf '%s days, %s hours, ' "$DAYS" "$HOURS"
280
36 printf '%s minutes, and %s seconds ' "$MINUTES" "$(diff)"
281
37 printf 'until Christmas Dinner!\n\n'
285
41 # Rewrite the diff () function to accept passed parameters,
286
42 #+ rather than using global variables.</PRE
300
> command has quite a
308
> gives the nanosecond portion
309
of the current time. One interesting use for this is to
310
generate random integers.
319
CLASS="PROGRAMLISTING"
320
> 1 date +%N | sed -e 's/000$//' -e 's/^0//'
321
2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322
3 # Strip off leading and trailing zeroes, if present.
323
4 # Length of generated integer depends on
324
5 #+ how many zeroes stripped off.
328
9 # 394504284</PRE
335
>There are many more options (try <B
348
CLASS="PROGRAMLISTING"
350
2 # Echoes day of the year (days elapsed since January 1).
353
5 # Echoes hour and minute in 24-hour format, as a single digit string.
357
9 # The 'TZ' parameter permits overriding the default time zone.
358
10 date # Mon Mar 28 21:42:16 MST 2005
359
11 TZ=EST date # Mon Mar 28 23:42:16 EST 2005
360
12 # Thanks, Frank Kannemann and Pete Sjoberg, for the tip.
363
15 SixDaysAgo=$(date --date='6 days ago')
364
16 OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a month!)
365
17 OneYearAgo=$(date --date='1 year ago')</PRE
372
HREF="special-chars.html#EX58"
375
HREF="contributed-scripts.html#STOPWATCH"
389
>Time zone dump: echoes the time in a specified time zone.</P
409
CLASS="COMPUTEROUTPUT"
410
>EST Tue Sep 18 22:09:22 2001 EST</TT
429
>Outputs verbose timing statistics for executing a command.</P
448
CLASS="COMPUTEROUTPUT"
459
>See also the very similar <A
460
HREF="internal.html#TIMESREF"
462
> command in the previous
476
SRC="common/note.png"
484
HREF="bash2.html#BASH2REF"
490
> became a shell reserved word,
491
with slightly altered behavior in a pipeline.</P
507
>Utility for updating access/modification times of a
508
file to current system time or other specified time,
509
but also useful for creating a new file. The command
515
> will create a new file
516
of zero length, named <TT
523
> did not previously exist.
524
Time-stamping empty files in this way is useful for
525
storing date information, for example in keeping track of
526
modification times on a project.
540
SRC="common/note.png"
554
>: >> newfile</B
560
>>> newfile</B
588
HREF="external.html#CPREF"
598
> to update the time stamp of files
599
you don't wish overwritten.</P
601
>As an example, if the directory <TT
603
>/home/bozo/tax_audit</TT
607
>spreadsheet-051606.data</TT
611
>spreadsheet-051706.data</TT
615
>spreadsheet-051806.data</TT
619
>touch spreadsheet*.data</B
621
will protect these files from being overwritten
622
by files with the same names during a
625
>cp -u /home/bozo/financial_info/spreadsheet*data
626
/home/bozo/tax_audit</B
646
> job control command executes
647
a given set of commands at a specified time. Superficially,
649
HREF="system.html#CRONREF"
655
> is chiefly useful for one-time execution
661
>at 2pm January 15</B
663
> prompts for a set of
664
commands to execute at that time. These commands should be
665
shell-script compatible, since, for all practical
666
purposes, the user is typing in an executable shell
667
script a line at a time. Input terminates with a <A
668
HREF="special-chars.html#CTLDREF"
672
>Using either the <TT
683
reads a command list from a file. This file is an
684
executable shell script, though it should, of course,
685
be non-interactive. Particularly clever is including the
687
HREF="extmisc.html#RUNPARTSREF"
690
the file to execute a different set of scripts.</P
706
>at 2:30 am Friday < at-jobs.list</B
710
CLASS="COMPUTEROUTPUT"
711
>job 2 at 2000-10-27 02:30</TT
733
> job control command is similar to
737
>, but it runs a command list when the system
745
>, it can read commands from a file with the
771
dates back to the era of mainframe computers. It means
772
running a set of commands without user intervention.</P
788
>Prints a neatly formatted monthly calendar to
792
>. Will do current year or a large
793
range of past and future years.</P
805
>This is the shell equivalent of a <I
809
>. It pauses for a specified number of
810
seconds, doing nothing. It can be useful for timing or
811
in processes running in the background, checking for
812
a specific event every so often (polling), as in <A
813
HREF="debugging.html#ONLINE"
822
CLASS="PROGRAMLISTING"
823
> 1 sleep 3 # Pauses 3 seconds.</PRE
841
SRC="common/note.png"
851
> command defaults to
852
seconds, but minute, hours, or days may also be specified.
860
CLASS="PROGRAMLISTING"
861
> 1 sleep 3 h # Pauses 3 hours!</PRE
883
SRC="common/note.png"
891
HREF="system.html#WATCHREF"
894
be a better choice than <B
898
commands at timed intervals.</P
921
> may be read as the Greek
929
prefix). This is the same as <B
937
intervals. It can be used for fine-grained timing,
938
or for polling an ongoing process at very frequent
949
CLASS="PROGRAMLISTING"
950
> 1 usleep 30 # Pauses 30 microseconds.</PRE
957
>This command is part of the Red Hat
960
>initscripts / rc-scripts</I
974
SRC="common/caution.png"
985
provide particularly accurate timing, and is therefore
986
unsuitable for critical timing loops.</P
1011
> command accesses or
1012
adjusts the machine's hardware clock. Some options
1019
>/etc/rc.d/rc.sysinit</TT
1024
> to set the system time
1025
from the hardware clock at bootup.</P
1030
> command is a synonym for
1044
SUMMARY="Footer navigation table"
1073
HREF="textproc.html"
1083
>Complex Commands</TD
1089
HREF="external.html"
1097
>Text Processing Commands</TD
b'\\ No newline at end of file'