如何使用ksh(或另一个shell脚本)获得现在和不同日期(分钟)之间的区别? [英] How to get the difference between now and a different date (in minutes) using ksh (or another shell script)?
问题描述
使用以下值给出两个变量:
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
如何使差异分钟)使用ksh?
How can I make the difference (in minutes) between them using ksh?
我正在使用Solaris 10。
我做了你所说的,这是错误:
$ 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
$
推荐答案
好的,这是一个C程序,可能会工作。它是(轻轻地)在solaris上测试。
well, here's a C program that might work. it's (lightly) tested on solaris.
它使用POSIX标准 strptime(3)功能来解析给定的字符串,并在epoch秒内打印结果。
it uses the POSIX standard strptime(3) function to parse a given string and prints the result in epoch seconds.
在理论上应该是是
$ ./strptime "Mon Feb 27 16:21:34 WET 2012" "%a %b %e %H:%M:%S %Z %Y"
但我还没有得到识别WET时区,所以你可能需要把它放在一个字面上:
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
这里是代码:
#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;
}
按照
$ CFLAGS='-W -Wall -Wshadow -std=gnu99' make strptime
(可能需要gcc,不知道sun cc是否在这里工作)
(probably requires gcc, no idea if sun cc will work here)
这篇关于如何使用ksh(或另一个shell脚本)获得现在和不同日期(分钟)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!