datediff man page

datediff — Compute duration from DATE/TIME (the reference date/time) to the other

Synopsis

datediff [OPTION]... DATE/TIME [DATE/TIME]...

Description

Compute duration from DATE/TIME (the reference date/time) to the other DATE/TIMEs given and print the result as duration. If the other DATE/TIMEs are omitted read them from stdin.

DATE/TIME can also be one of the following specials
- `now' interpreted as the current (UTC) time stamp
- `time' the time part of the current (UTC) time stamp
- `today' the current date (according to UTC)
- `tomo[rrow]' tomorrow's date (according to UTC)
- `y[ester]day' yesterday's date (according to UTC)

Note: The output format of durations (specified via -f) takes all format specifiers into account, i.e. specifying %M and %S for example prints the duration in minutes and seconds, whereas specifying %S only prints the duration in seconds.

See section `The refinement rule' in datediff(1).

Recognized OPTIONs:

-h, --help
Print help and exit
-V, --version
Print version and exit
-q, --quiet
Suppress message about date/time and duration parser errors and fix-ups. The default is to print a warning or the fixed up value and return error code 2. Also see -S|--skip-illegal to output an empty line instead of leaving out the line altogether.
-S, --skip-illegal
Output empty lines as placeholder for illegal input, i.e. parser errors or date/times that cannot be subtracted.
-f, --format=STRING
Output format. This can either be a specifier string (similar to strftime()'s FMT) or the name of a calendar.
-i, --input-format=STRING...
Input format, can be used multiple times. Each date/time will be passed to the input format parsers in the order they are given, if a date/time can be read successfully with a given input format specifier string, that value will be used.
-b, --base=DT
For underspecified input use DT as a fallback to fill in missing fields. Also used for ambiguous format specifiers to position their range on the absolute time line. Must be a date/time in ISO8601 format. If omitted defaults to the current date/time.
-e, --backslash-escapes
Enable interpretation of backslash escapes in the output and input format specifier strings.
--from-locale=LOCALE
Interpret dates on stdin or the command line as coming from the locale LOCALE, this would only affect month and weekday names as input formats have to be specified explicitly.
--from-zone=ZONE
Interpret dates on stdin or the command line as coming from the time zone ZONE.

Format Specs

Format specs in dateutils are similar to posix' strftime().

However, due to a broader range of supported calendars dateutils must employ different rules.

Date specs:

%a  The abbreviated weekday name
%A  The full weekday name
%_a The weekday name shortened to a single character (MTWRFAS)
%b  The abbreviated month name
%B  The full month name
%_b The month name shortened to a single character (FGHJKMNQUVXZ)
%c  The count of the weekday within the month (range 00 to 05)
%C  The count of the weekday within the year (range 00 to 53)
%d  The day of the month, 2 digits (range 00 to 31)
%D  The day of the year, 3 digits (range 000 to 366)
%F  Equivalent to %Y-%m-%d (ymd's canonical format)
%g  ISO week date year without the century (range 00 to 99)
%G  ISO week date year including the century
%j  Equivalent to %D
%m  The month in the current calendar (range 00 to 19)
%Q  The quarter of the year (range Q1 to Q4)
%q  The number of the quarter (range 01 to 04)
%s  The number of seconds since the Epoch.
%u  The weekday as number (range 01 to 07, Sunday being 07)
%U  The week count,  day of week is Sun (range 00 to 53)
%V  The ISO week count,  day of week is Mon (range 01 to 53)
%w  The weekday as number (range 00 to 06, Sunday being 00)
%W  The week count,  day of week is Mon (range 00 to 53)
%y  The year without a century (range 00 to 99)
%Y  The year including the century
%_y The year shortened to a single digit
%Z  The zone offset in hours and minutes (HH:MM) with
    a preceding sign (+ for offsets east of UTC, - for offsets
    west of UTC)
%Od The day as roman numerals
%Om The month as roman numerals
%Oy The two digit year as roman numerals
%OY The year including the century as roman numerals
%rs In time systems whose Epoch is different from the unix Epoch, this
    selects the number of seconds since then.
%rY In calendars with years that don't coincide with the Gregorian
    years, this selects the calendar's year.
%dth  The day of the month as an ordinal number, 1st, 2nd, 3rd, etc.
%mth  The month of the year as an ordinal number, 1st, 2nd, 3rd, etc.
%db The business day of the month (since last month's ultimo)
%dB Number of business days until this month's ultimo

Time specs:

%H  The hour of the day using a 24h clock, 2 digits (range 00 to 23)
%I  The hour of the day using a 12h clock, 2 digits (range 01 to 12)
%M  The minute (range 00 to 59)
%N  The nanoseconds (range 000000000 to 999999999)
%p  The string AM or PM, noon is PM and midnight is AM.
%P  Like %p but in lowercase
%S  The  (range 00 to 60, 60 is for leap seconds)
%T  Equivalent to %H:%M:%S

General specs:

%n  A newline character
%t  A tab character
%%  A literal % character

Modifiers:

%O  Modifier to turn decimal numbers into Roman numerals
%r  Modifier to turn units into real units
th  Suffix, read and print ordinal numbers
b   Suffix, treat days as business days

By design dates before 1601-01-01 are not supported.

For conformity here is a list of calendar designators and their corresponding format string:

ymd     %Y-%m-%d
ymcw    %Y-%m-%c-%w
ywd     %rY-W%V-%u
bizda   %Y-%m-%db
lilian     n/a
ldn        n/a
julian     n/a
jdn        n/a

These designators can be used as output format string, moreover, @code{lilian}/@code{ldn} and @code{julian}/@code{jdn} can also be used as input format string.

Format Specs for Durations

Unlike time or absolute instants, durations are reference-free, i.e. the reference instant is not part of the duration. As a result durations cannot be named, i.e. there is no naming scheme that applies to all durations and all references unambiguously.

Consequently, none of the format specifiers for date/times makes sense for durations in the literal sense. However, to aid intuitive usage we reused format specifiers when they represent integral values and a valid unit for duration, as follows:

Date specs:

%c  Equivalent to %w
%d  Duration in days
%F  Equivalent to %dd with no resorting to bigger units
%m  Duration in months
%w  Duration in weeks
%y  Equivalent to %Y
%Y  Duration in years
%db Duration in business days
%dB Equivalent to %db

Time specs:

%H  Duration in hours
%I  Equivalent to %H
%M  Duration in minutes
%S  Duration in seconds
%T  Equivalent to %Ss without resorting to bigger units
%rS Duration in real-life seconds, as in including leap seconds
%rT Equivalent to %rSs without resoring to bigger units

General specs:

%n  A newline character
%t  A tab character
%%  A literal % character

Modifiers:

%r    Modifier to turn units into real units
%0    Modifier to pad refined values with zeroes
%SPC  Modifier to pad refined values with spaces
b     Suffix, treat days as business days

The Refinement Rule

Durations are somewhat ambiguous when it comes to representing them through format specifiers. Unlike format specifiers in point-in-time representations duration specifiers can have an intra-line relationship.

So for instance a duration of 128 seconds might be presented through "%S" as "128" but similarly through "%M:%S" as "02:08" (read two minutes and 8 seconds).

There are several approaches to deal with this ambiguity. The datediff tool will follow, what we call, the refinement rule. That is, regardless of the position of a format specifier, if it is a valid @emph{refinement} of another specifier in the format string, then it will only show the fractional value, i.e. the value in its natural range with respect to the @emph{refined} specifier.

%Y  possible refinements: %m, %w, %d
%m  possible refinements: %w, %d
%w  possible refinements: %d
%d  possible refinements: %H, %M, %S
%H  possible refinements: %M, %S
%M  possible refinements: %S

The refinement alternatives are listed in order of precedence and they are mutually exclusive. I.e. it is not possible to express a duration in months and hours without having a "%d" specifier as well. On the other hand in a chain of refinements inner elements are optional, i.e. you can express a duration in weeks and hours because every day has 24 hours and hence there are 168 hours in a week.

In case of negative durations (the minuend is in the future relative to the subtrahend) @emph{only} the largest unit will carry the minus sign.

Using the refinement rule keeps the format string dead simple, there's no need for operators or a full-blown language to distinguish the range ambiguity, which then would have to be escaped because they could also in theory be part of the literal characters of the format string, resulting more often than not in command lines that are hard to craft and even harder to understand later on (e.g. if used in shell scripts).

The refinement rule ingeniously covers the 99% case but, unlike other approaches, there's @emph{no} way to display two unrefined values in the same format string, e.g. "'%w weeks (which is %d days)'".

Examples

$ datediff 2012-03-02 2012-03-02
0
$
$ datediff 2012-03-02 2012-03-12
10
$
$ datediff 2012-03-02 2012-04-12
41
$
$ datediff 2012-03-12 2012-04-02
21
$
$ datediff 2012-04-02 2012-03-12
-21
$
$ datediff 2012-01-02 2012-02-29 -f '%dd'
58d
$
$ datediff 2012-01-02 2012-02-29 -f '%ww %dd'
8w 2d
$
$ datediff 10:00:00 10:00:00
0s
$
$ datediff 10:01:00 10:06:00
300s
$
$ datediff 10:06:00 10:01:00
-300s
$
$ datediff 10:01:00 11:03:10 -f '%S sec'
3730 sec
$
$ datediff 10:01:00 11:03:10 -f '%Mm %Ss'
62m 10s
$
$ datediff 10:01:00 11:03:10 -f '%H:%M:%S'
1:2:10
$
$ datediff 2012-03-02T10:04:00 2012-03-02T10:14:00
600s
$
$ datediff 2012-03-02T10:04:00 2012-03-02T10:14:00 -f '%M min'
10 min
$
$ datediff 2012-03-01T12:17:00 2012-03-02T14:00:00
92580s
$
$ datediff 2012-03-01T12:17:00 2012-03-02T14:00:00 -f '%d days and %S seconds'
1 days and 6180 seconds
$

Author

Written by Sebastian Freundt <freundt@fresse.org>

Reporting Bugs

Report bugs to: https://github.com/hroptatyr/dateutils/…

Info

May 2016 dateutils 0.4.0 User Commands