A question about the date Function

Matthew Seaman m.seaman at infracaninophile.co.uk
Thu Sep 17 05:47:02 UTC 2009


Martin McCormick wrote:

 
> date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s" >f0
> date +%s >f1

> 	What does the long form of this command give us that
> date +%s fails to do?

It's a contrived example:

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s" 

   -j says "don't alter the system date" -- this is used if you want
      to read and format a date/time string other than the present time.

   -f says use the following format to read the input date. That's
      %a -- abbreviated weekday name (localized)
      %b -- abbreviated month name (localized)
      %d -- day of month as decimal number, zero padded to two digits
      %T -- equivalent to %H:%M:%S
          %H -- Hour in 24h clock, zero padded to two digits
          %M -- Minute, zero padded
          %S -- Second, zero padded
      %Z -- Time zone name
      %Y -- Year as 4 digits including century.
      (See strftime(3))

     Which looks like this:

     % date +"%a %b %d %T %Z %Y"
     Thu Sep 17 06:31:15 BST 2009

     and that just happens to be the default *output* format date produces
     without any arguments.  Which is appropriate as the next item on the command
     line is

  "`date`"  Rune the date command without arguments and substitute the output
     into the command line here as a single argument

   +%s finally, says output the date that was read in as the number of seconds
     since the epoch.  This is an argument to the initial date command.

so the end result is that the command reads the current date time in the standard
output format, parses all of that then converts it into seconds-since-the-epoch,
using two invocations of the date(1) program to do so. Which is not at all efficient
if all you need to do is generate the current epoch time.  Just use

   date +%s

for that.

On the other hand, it does show you how to convert an arbitrary date/time to 
epoch time. eg.:

   % date -j -f "%a %b %d %T %Z %Y" "Fri Feb 13 23:31:30 GMT 2009" +%s
   1234567890

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20090917/5d6b7e4e/signature.pgp


More information about the freebsd-questions mailing list