Reputation: 245
I am trying to find the day difference from the last time a password was rest to the current. So i have this so far. I am trying to just convert that date to days so i can subtract the current date in days - the last reset date in days and get a integer value.
$ LASTRESETDATE=$(echo $(passwd -s) | cut -d' ' -f3)
$ echo $LASTRESETDATE
12/15/16
Looking the the date verion i have there is no option for -d
$ date -h
date: illegal option -- h
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]
Upvotes: 1
Views: 759
Reputation: 19982
When you have awk
, you can calculate the difference with
LASTRESETDATE="12/15/16"
endd="$(date '+%m/%d/%y')"
awk -v startdate="${LASTRESETDATE}" -v enddate="${endd}" 'BEGIN {
split(startdate,A,"[/]");
T1=mktime(A[3] " " A[1] " " A[2] " 12 0 0");
split(enddate,B,"[/]");
T2=mktime(B[3] " " B[1] " " B[2] " 12 0 0");
diffdays=(T2-T1)/(3600*24)
printf("%s\n",diffdays);
}'
When you need this often, and you do not have (the right version of) awk, you can make a lookup-table on another system.
With awk
:
startd="12/15/16"
endd="$(date '+%m/%d/%y')"
awk -v startdate="${startd}" -v enddate="${endd}" 'BEGIN {
split(startdate,A,"[/]");
T1=mktime(A[3] " " A[1] " " A[2] " 12 0 0");
split(enddate,B,"[/]");
T2=mktime(B[3] " " B[1] " " B[2] " 12 0 0");
linenr=1;
while (T1 < T2) {
printf("%d %s\n",linenr++, strftime("%m/%d/%y",T1));
T1+=3600*24;
}
}'
Of course you can make a list with Excel or another tool.
EDIT: removed var that I only used prototyping the solution.
Upvotes: 1
Reputation: 39354
When I was unfortunate enough to have to slog through HP-UX idiosyncrasies, I'd often write my own little programs to do exactly what I wanted. Provided your hokey-pux machine is POSIXy, then you could do:
$ cat fromnow.c
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
struct tm parsed;
strptime(argv[1], argv[2], &parsed);
printf("%.0f\n", difftime(time(NULL), mktime(&parsed)));
return 0;
}
Compile it:
$ cc -o fromnow fromnow.c
Run it:
$ ./fromnow 5/13/13 %D
119451988
which will then calculate the number of seconds between the current time and the date "5/13/13" formatted in American style "%D". The first argument is the date, the second argument is the format specifier for parsing that date. See man strptime for options.
This could be made more general, or less, depending upon how much you need to do date calculations.
Upvotes: 3
Reputation: 20688
Not sure which version of ksh
you're using.
[STEP 101] $ echo ${.sh.version}
Version AJM 93u+ 2012-08-01
[STEP 102] $ date=12/15/16
[STEP 103] $ [[ $date =~ (..)/(..)/(..) ]]
[STEP 104] $ date=20${.sh.match[3]}-${.sh.match[1]}-${.sh.match[2]}
[STEP 105] $ echo $date
2016-12-15
[STEP 106] $ old_seconds=$( printf '%(%s)T' $date )
[STEP 107] $ echo $old_seconds
1481797007
[STEP 108] $ now_seconds=$( printf '%(%s)T' )
[STEP 109] $ echo $now_seconds
1487845024
[STEP 110] $ (( diff = now_seconds - old_seconds ))
[STEP 111] $ echo $diff
6048017
[STEP 112] $ echo $(( diff / 86400 )) days
70 days
[STEP 113] $
Upvotes: 1