Device in UNIX to subtract days

Exists any kind of device in Solaris UNIX (so no GNU device readily available) to subtract days? I recognize that in Linux we have gawk that can subtract one date from an additional. Yet in Solaris the maximum we have is nawk (boosted awk) which can not execute date estimations. Additionally I can not make use of perl.

Exists any kind of means to do date estimations like 20100909 - 20001010?

UPGRADE: Is bc able to execute days estimations?

2019-05-04 19:40:36
Source Share
Answers: 6

Here is an awk manuscript I simply wrote, need to collaborate with an POSIX awk. You'll need to attempt the Solaris variation ; bear in mind that there are 2 variations of Awk on Solaris too, one in/ container and also one in/ usr/xpg4/bin/ awk (which is nawk, I think).

    daysofmonth["01"] = 0; daysofmonth["02"] = 31; daysofmonth["03"] = 59;
    daysofmonth["04"] = 90; daysofmonth["05"] = 120; daysofmonth["06"] = 151;
    daysofmonth["07"] = 181; daysofmonth["08"] = 212; daysofmonth["09"] = 243;
    daysofmonth["10"] = 273; daysofmonth["11"] = 304; daysofmonth["12"] = 334;
    fullday = 86400;
/[12][09][0-9][0-9][01][0-9][0123][0-9]/ {
    year = substr($0, 1, 4); month = substr($0, 5, 2); day = substr($0, 7, 2);
    date = ((year - 1970) * 365.25) + daysofmonth[month] + day - 1;
    if ((year % 4) == 0 && month > 2) { date = date + 1; }
    print date * fullday - (25200);

Pass a YYYYmmdd date string via and also it will certainly be transformed to variety of secs given that the Epoch (with a little offer for getting on day borders). After that you will certainly have the ability to subtract both.

today=`echo 20110210 | awk -f convdate.awk`
then=`echo 20001231 | awk -f convdate.awk`
sincethen=`expr $today - $then`
2019-05-23 01:25:12


 from datetime import datetime as dt
 a = dt(2010, 9, 9)
 b = dt(2000, 10, 10)
 print (a - b).days
2019-05-23 01:24:48

Perl is most likely to be mounted, and also it is very easy sufficient to order components from CPAN, install them in your house directory site, and also refer to them in your program. In this instance, the Date::Calc component has a Delta_Days subroutine that will certainly aid.

2019-05-23 00:36:15

I would certainly attempt making use of the date command which becomes part of POSIX so it is nearly almost everywhere. UPGRADE : Unfortunately, it appears that - d is not component of POSIX date and also most likely not there on Solaris. Hence this most likely will not address the OPs inquiry.

d1=`date -d 20100909 +%s`
d2=`date -d 20001010 +%s`

Currently d1 and also d2 are integers that represent secs given that the unix date. Hence to get the distinction in between both, we subtract ($((d1-d2)) in celebration) and also concealed to whatever devices we desire. Days are the most convenient :

echo "$(((d1-d2)/86400)) days"

Just how to do the conversion will likely be various if you do not have celebration. One of the most mobile means might be to make use of expr (expr's posix man page).

2019-05-08 07:31:11

If you have Python :

from time import *
date1 = strptime("20100909","%Y%m%d")
date2 = strptime("20001010","%Y%m%d")
diff = mktime(date1) - mktime(date2)
print repr(d/86400) + " days"

You marked your inquiry "gawk" yet you claim "no GNU devices". Gawk has date math.

2019-05-08 07:27:36

Unfortunately, none of the POSIX command line utilities give math on days. date -d and also date +%s are the means to go if you have them, yet they're GNU expansions.

There's an awkward hack with touch that type of benefit examining that a date goes to the very least n days in the previous :

touch -t 201009090000 stamp
if [ -n "$(find stamp -mtime +42)" ]; then ...

(Note that this code might be off by one if DST began or dropped in the period and also the manuscript runs prior to 1am.)

Numerous individuals have actually wound up applying date adjustment collections in Bourne or POSIX shell. There are a couple of instances and also web links in the FAQ.

Mounting GNU devices might be the means of the very least discomfort.

2019-05-08 04:56:39