1

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]
Walter A
  • 19,067
  • 2
  • 23
  • 43
user3753693
  • 225
  • 1
  • 5
  • 13
  • 1
    **edit** : given you're stuck with hp-ux (and you're probably in a corporate environment where you **can't** install GNU tools), you'll have to code your own functions to convert dates to manipuable values. My answer at stackoverflow.com/questions/8606520/converting-dates-in-shel‌​l/… is part of the solution. Search for `[linux] awk date math` (and other similar search targets) to find more complete solutions. Good luck. – shellter Feb 23 '17 at 04:11

3 Answers3

3

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.

bishop
  • 37,830
  • 11
  • 104
  • 139
1

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] $
pynexj
  • 19,215
  • 5
  • 38
  • 56
1

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.

Walter A
  • 19,067
  • 2
  • 23
  • 43
  • When I use this it says "ksh: str: parameter not set" when i remove the -v string=$"{str}". It gives an error on the mktime function saying it is not defined. – user3753693 Feb 27 '17 at 19:09
  • I removed the unused string. The mktime is a gawk extension, your awk version is not supporting it. When you can't get gawk, consider making a lookup.csv file from excel witjh one date on each line. With something like `grep -n` (or a second excel column) you can get linenumbers and look up the difference. – Walter A Feb 27 '17 at 23:06