datemath

Index Home MAE > MAE Architecture > MAE Utilities > datemath

Manipulate a UNIX time by adding and subtracting seconds, minutes, hours, days, and weeks allowing flexible control of output format (like the date command, but able to output different times than the present).

Synopsis

datemath [time] [date] [hour] [noon] [midnight] [now] [Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec] [firstday | lastday | Sun | Mon | Tue | Wed | Thu | Fri | Sat] [+|-ns] [+|-nm] [+|-nh] [+|-nd] [+|-nw] [+|-nM] [+|-nQ] [+|-nY] [-f format] [-F filename position]

Description

The datemath command performs math on the date and time and allows you to output the result with most of the options of the date command. This becomes particularly useful when you need to know yesterday's date, tomorrow's date, or some other relative date or time. By default, datemath starts with the current date and time, however the first parameter may be a fixed date and time (specified as seconds since Jan 1, 1970). From there, specify as many manipulations as needed, for example +1d to move forward 1 day or -1h to move back 1 hour.

The resulting time value is output as seconds since Jan 1, 1970 unless another format is specified. The format follows the -f command line option an generally follows the options available in the date command. For example,

datemath -1d -f "%D"

will output yesterday's date. See the formatting substitutions below.

Beware that the order of arguments matters. For example,

datemath Mar lastday -f "%D"

will output the date for the last day of March (03/31/2000), whereas:

datemath lastday Mar -f "%D"

will output the day in March corresponding to the last day of the current month (03/30/2000 if run in April).

Options

Option

Description

time

The time as seconds since Jan 1, 1970 to specify as the base time to manipulate.

date

The date (month/day/year) of the base time to manipulate, e.g. 10/2/96. Hours, minutes, and seconds are untouched.

hour

The hour (hour:minute:seconds or hour:minutes) of the base time to manipulate, e.g. 12:40.  Month, day, and year are untouched.

noon

Set the hour of the base time to manipulate to 12:00 noon.  Month, day, and year are untouched.

midnight

Set the hour of the base time to manipulate to 12:00 midnight.  Month, day, and year are untouched.

now

Set the hour of the base time to manipulate to right now.  This serves as a place holder since datemath by default defaults to right now.

Jan

Set the month to January.  If the month is not January, then the previous January is used.  Time of day remains untouched.

Feb

Set the month to February.  If the month is not February, then the previous February is used.  Time of day remains untouched.

Mar

Set the month to March.  If the month is not March, then the previous March is used.  Time of day remains untouched.

Apr

Set the month to April.  If the month is not April, then the previous April is used.  Time of day remains untouched.

May

Set the month to May.  If the month is not May, then the previous May is used.  Time of day remains untouched.

Jun

Set the month to June.  If the month is not June, then the previous June is used.  Time of day remains untouched.

Jul

Set the month to July.  If the month is not July, then the previous July is used.  Time of day remains untouched.

Aug

Set the month to August.  If the month is not August, then the previous August is used.  Time of day remains untouched.

Sep

Set the month to September.  If the month is not September, then the previous September is used.  Time of day remains untouched.

Oct

Set the month to October.  If the month is not October, then the previous October is used.  Time of day remains untouched.

Nov

Set the month to November.  If the month is not November, then the previous November is used.  Time of day remains untouched.

Dec

Set the month to December.  If the month is not December, then the previous December is used.  Time of day remains untouched.

firstday

Set the day of the month to the first of the month.  Month, year, and time untouched.

lastday

Set the day of the month to the last of the month.  Month, year, and time untouched.

Sun

Set the day of the week to Sunday.  If the day is not Sunday, then the previous Sunday is used.  Time of day remains untouched.

Mon

Set the day of the week to Monday.  If the day is not Monday, then the previous Monday is used.  Time of day remains untouched.

Tue

Set the day of the week to Tuesday.  If the day is not Tuesday, then the previous Tuesday is used.  Time of day remains untouched.

Wed

Set the day of the week to Wednesday.  If the day is not Wednesday, then the previous Wednesday is used.  Time of day remains untouched.

Thu

Set the day of the week to Thursday.  If the day is not Thursday, then the previous Thursday is used.  Time of day remains untouched.

Fri

Set the day of the week to Friday.  If the day is not Friday, then the previous Friday is used.  Time of day remains untouched.

Sat

Set the day of the week to Saturday.  If the day is not Saturday, then the previous Saturday is used.  Time of day remains untouched.

[+|-]n s

Add (+) or subtract - n seconds of time.

[+|-]n m

Add (+) or subtract - n minutes of time.

[+|-]n h

Add (+) or subtract - n hours of time.

[+|-]n d

Add (+) or subtract - n days of time.

[+|-]n w

Add (+) or subtract - n weeks of time.

[+|-]n M

Add (+) or subtract - n months of time.  Be careful adding time to the end of a month.  For example, adding 1 month to Aug 31 does not produce Sep 31, but Oct 1.

[+|-]n Q

Add (+) or subtract - n quarters (3 months) of time.

[+|-]n Y

Add (+) or subtract - n years of time.

-f format

Specify format of output.  The format can include any text as well as specifal substitions that start with %.  For example, "Date: %D" will output

Date: 05/06/96

if the resulting date was May 6, 1996.  The following table shows you what substitutions are allowed:

%s

Output seconds since Jan 1, 1970

%T

Output the time as HH:MM:SS

%D

Output the date in format MM/DD/YY

%S

Output the second of the minute, 00 to 59

%M

Output the minute of the hour, 00 to 59

%H

Output the hour of the day, 00 to 23

%I

Output the hour of the day, 01 to 12

%a

Output the day of the week, Sun to Sat

%d

Output the day of the month, 01 to 31

%m

Output the month of the year, 01 to 12

%q

Output the quarter of the year, 1 to 4

%Q

Output the quarter and the year, 1Q03 to 4Q03

%y

Output the year of the century, 00 to 99

%Y

Output the year with the century, 1999...

%b

Output the name of the month, Jan to Dec

%Z

Output the name of the timezone, EST


-F filename position

Use this command line option to translate dates in a file (use - for stdin) from the Unix format (seconds since Jan 1, 1970) to the format specified.  The position of the Unix formatted time must be specified as either a token on the line or as byte positions.  Tokens on a line range from 1 upward.  Byte positions are specified as a range:  lower-upper where byte positions start at 1 for the first character on the line.