How to get the difference between now and a differ

2019-06-04 12:18发布

Giving two variables with the followind values:

date1 = Mon Feb 27 16:21:34 WET 2012
date2 = Mon Feb 27 16:29:34 WET 2012

How can I make the difference (in minutes) between them using ksh?

I'm using Solaris 10.


I made what you said and this was the error:

$ function d { echo $((($(date -d"$2" +%s)-$(date -d"$1" +%s))/60)); }
$ d "Mon Feb 27 16:21:34 WET 2012" "Mon Feb 27 16:29:34 WET 2012"
date: illegal option -- d
date: illegal option -- M
date: illegal option -- o
date: illegal option -- n
date: illegal option --
date: illegal option -- F
date: illegal option -- e
date: illegal option -- b
date: illegal option --
date: illegal option -- 2
date: illegal option -- 7
date: illegal option --
date: illegal option -- 1
date: illegal option -- 6
date: illegal option -- :
date: illegal option -- 2
date: illegal option -- 9
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 4
date: illegal option --
date: illegal option -- W
date: illegal option -- E
date: illegal option -- T
date: illegal option --
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 2
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- d
date: illegal option -- M
date: illegal option -- o
date: illegal option -- n
date: illegal option --
date: illegal option -- F
date: illegal option -- e
date: illegal option -- b
date: illegal option --
date: illegal option -- 2
date: illegal option -- 7
date: illegal option --
date: illegal option -- 1
date: illegal option -- 6
date: illegal option -- :
date: illegal option -- 2
date: illegal option -- 1
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 4
date: illegal option --
date: illegal option -- W
date: illegal option -- E
date: illegal option -- T
date: illegal option --
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 2
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
0
$

标签: shell date ksh
3条回答
看我几分像从前
2楼-- · 2019-06-04 12:46

using a date which supports -d,

$ function d { echo $((($(date -d "$2" +%s)-$(date -d "$1" +%s))/60)); }
$ d "Mon Feb 27 16:21:34 WET 2012" "Mon Feb 27 16:29:34 WET 2012"
8
查看更多
放我归山
3楼-- · 2019-06-04 12:47

You don't have GNU date. You might have Tcl installed:

$ printf 'puts [expr {abs([clock scan "%s"]-[clock scan "%s"])/60}]' "$date1" "$date2" | tclsh
8
查看更多
仙女界的扛把子
4楼-- · 2019-06-04 12:57

well, here's a C program that might work. it's (lightly) tested on solaris.

it uses the POSIX standard strptime(3) function to parse a given string and prints the result in epoch seconds.

in theory invocation should be

$ ./strptime "Mon Feb 27 16:21:34 WET 2012" "%a %b %e %H:%M:%S %Z %Y"

but i haven't been able to get it to recognize the "WET" timezone, so you may need to just put that in as a literal:

$ ./strptime "Mon Feb 27 16:21:34 WET 2012" "%a %b %e %H:%M:%S WET %Y"
1330377694

here's the code:

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#define ARGS 3
#define USAGE printf("Usage: %s string format\n",argv[0]);
#define EARGS { printf("%s: %s arguments (%d)\n",argv[0],argc<ARGS?"Insufficient":"Too many",argc-1); USAGE; exit(2); }

#define err(_s,_p,_f) {fprintf(stderr,"%s: %s: %s\n",(_p),(_f),strerror(errno));exit(_s);}

int main(int argc,char**argv){
        struct tm tm;
        time_t t;

        if(argc!=ARGS)EARGS;

        strptime(argv[1],argv[2],&tm);
        if(-1==(t=mktime(&tm)))err(1,argv[0],"mktime");
        if(0>printf("%lld\n",(long long)t))err(1,argv[0],"printf");
        return 0;
}

compile with something along the lines of

$ CFLAGS='-W -Wall -Wshadow -std=gnu99' make strptime

(probably requires gcc, no idea if sun cc will work here)

查看更多
登录 后发表回答