coreutils: Examples of date

1 
1 21.1.7 Examples of ‘date’
1 -------------------------
1 
1 Here are a few examples.  Also see the documentation for the ‘-d’ option
1 in the previous section.
1 
1    • To print the date of the day before yesterday:
1 
1           date --date='2 days ago'
1 
1    • To print the date of the day three months and one day hence:
1 
1           date --date='3 months 1 day'
1 
1    • To print the day of year of Christmas in the current year:
1 
1           date --date='25 Dec' +%j
1 
1    • To print the current full month name and the day of the month:
1 
1           date '+%B %d'
1 
1      But this may not be what you want because for the first nine days
1      of the month, the ‘%d’ expands to a zero-padded two-digit field,
1      for example ‘date -d 1may '+%B %d'’ will print ‘May 01’.
1 
1    • To print a date without the leading zero for one-digit days of the
1      month, you can use the (GNU extension) ‘-’ flag to suppress the
1      padding altogether:
1 
1           date -d 1may '+%B %-d'
1 
1    • To print the current date and time in the format required by many
1      non-GNU versions of ‘date’ when setting the system clock:
1 
1           date +%m%d%H%M%Y.%S
1 
1    • To set the system clock forward by two minutes:
1 
1           date --set='+2 minutes'
1 
1    • To print the date in Internet RFC 5322 format, use ‘date
1      --rfc-email’.  Here is some example output:
1 
1           Fri, 09 Sep 2005 13:51:39 -0700
1 
1    • To convert a date string to the number of seconds since the epoch
1      (which is 1970-01-01 00:00:00 UTC), use the ‘--date’ option with
1      the ‘%s’ format.  That can be useful in sorting and/or graphing
1      and/or comparing data by date.  The following command outputs the
1      number of the seconds since the epoch for the time two minutes
1      after the epoch:
1 
1           date --date='1970-01-01 00:02:00 +0000' +%s
1           120
1 
1      If you do not specify time zone information in the date string,
1      ‘date’ uses your computer’s idea of the time zone when interpreting
1      the string.  For example, if your computer’s time zone is that of
1      Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
1      seconds) behind UTC:
1 
1           # local time zone used
1           date --date='1970-01-01 00:02:00' +%s
1           18120
1 
1    • If you’re sorting or graphing dated data, your raw date values may
1      be represented as seconds since the epoch.  But few people can look
1      at the date ‘946684800’ and casually note “Oh, that’s the first
1      second of the year 2000 in Greenwich, England.”
1 
1           date --date='2000-01-01 UTC' +%s
1           946684800
1 
1      An alternative is to use the ‘--utc’ (‘-u’) option.  Then you may
1      omit ‘UTC’ from the date string.  Although this produces the same
1      result for ‘%s’ and many other format sequences, with a time zone
1      offset different from zero, it would give a different result for
1      zone-dependent formats like ‘%z’.
1 
1           date -u --date=2000-01-01 +%s
1           946684800
1 
1      To convert such an unwieldy number of seconds back to a more
1      readable form, use a command like this:
1 
1           # local time zone used
1           date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
1           1999-12-31 19:00:00 -0500
1 
1      Or if you do not mind depending on the ‘@’ feature present since
1      coreutils 5.3.0, you could shorten this to:
1 
1           date -d @946684800 +"%F %T %z"
1           1999-12-31 19:00:00 -0500
1 
1      Often it is better to output UTC-relative date and time:
1 
1           date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
1           2000-01-01 00:00:00 +0000
1 
1    • Typically the seconds count omits leap seconds, but some systems
1      are exceptions.  Because leap seconds are not predictable, the
1      mapping between the seconds count and a future timestamp is not
1      reliable on the atypical systems that include leap seconds in their
1      counts.
1 
1      Here is how the two kinds of systems handle the leap second at
1      2012-06-30 23:59:60 UTC:
1 
1           # Typical systems ignore leap seconds:
1           date --date='2012-06-30 23:59:59 +0000' +%s
1           1341100799
1           date --date='2012-06-30 23:59:60 +0000' +%s
1           date: invalid date '2012-06-30 23:59:60 +0000'
1           date --date='2012-07-01 00:00:00 +0000' +%s
1           1341100800
1 
1           # Atypical systems count leap seconds:
1           date --date='2012-06-30 23:59:59 +0000' +%s
1           1341100823
1           date --date='2012-06-30 23:59:60 +0000' +%s
1           1341100824
1           date --date='2012-07-01 00:00:00 +0000' +%s
1           1341100825
1