如何使用ksh(或另一个shell脚本)获得现在和不同日期(分钟)之间的区别?

编程入门 行业动态 更新时间:2024-10-18 14:26:35
本文介绍了如何使用ksh(或另一个shell脚本)获得现在和不同日期(分钟)之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用以下值给出两个变量:

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脚本)获得现在和不同日期(分钟)之间的区别?

本文发布于:2023-11-22 14:47:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   脚本   区别   日期   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!