日期:非法选项-d,查找两个日期之间的差异 [英] date: illegal option -- d, Find difference between two dates

查看:98
本文介绍了日期:非法选项-d,查找两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将从文件读取的时间戳从字符串转换为日期格式,以便可以找到2个日期/时间戳之间的差异. Web上的大多数线程/讨论都显示了使用日期参数'-d'来将字符串转换为历元或查找两个时间戳之间的差异

I am trying to convert timestamps read from a file from string to date format so that I can find the difference of 2 dates/timestamps. most of the threads/discussions on web show usage of date argument '-d' to convert the string to epoch or to find the difference of two timestamps Find difference between two dates in bash

但是看来我的环境/操作系统不支持-d date参数.以下是我的环境的详细信息:

But it looks like my environment/OS doesn't support -d date argument. Below are the details of my env:

bash --version
GNU bash, version 3.2.52(1)-release (i386-pc-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

uname -a
SunOS s01***** 5.10 Generic_147148-26 i86pc i386 i86pc

从文件中读取的采样日期:

Sample dates read from file:

START_TIME="09/03/16 - 01:04:56"
END_TIME="09/03/16 - 05:10:44"

我尝试过的代码 我试图模仿在bash中找到两个日期之间的区别的以下代码

!# /usr/bin/sh

date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
date -d @$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) -u +'%H:%M:%S'

bash test.sh
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
test.sh: line 5: -  : syntax error: operand expected (error token is " ")

我不认为第5行的语法错误是导致我在日期手册页中未找到选项-d的主要原因.

I don't think syntax error on line 5 is the main culprit cause I didnot find option -d in my date's man page.

回应评论:

>>> date --version
    date: illegal option -- version
    usage:  date [-u] mmddHHMM[[cc]yy][.SS]
            date [-u] [+format]
            date -a [-]sss[.fff]
    >>> date --help
    date: illegal option -- help
    usage:  date [-u] mmddHHMM[[cc]yy][.SS]
            date [-u] [+format]
            date -a [-]sss[.fff]
    >>> echo $0
    bash

即使不支持这些参数.抱歉,如果我犯了任何愚蠢的错误.

Even these arguments are not supported. Apologies if I am committing any silly mistake.

有人可以给我等同于-d的信息来共享上面的env细节,还是一种无需使用-d就能找到两个日期之间的差异的方法.

Could someone please give me the equivalent of -d for the env details shared above or a way to find the difference between two dates without using -d.

预先感谢

推荐答案

awk mktime有相当大的机会在您的系统上存在:

awk mktime has a decent chance of existing on your system:

#!/bin/bash

START_TIME="09/03/16 - 01:04:56"
END_TIME="09/03/16 - 05:10:44"

echo -e "$START_TIME\n$END_TIME" |
  tr '/:-' ' ' |
  awk '{print "20"$3" "$2" "$1" "$4" "$5" "$6}' |
  awk '{printf "%s ", mktime($0)}' |
  awk '{print $2 - $1}'

说明:

  1. echo两个时间字符串
  2. tr09/03/16 - 01:04:56转换为09 03 16 01:04:56
  3. 第一个awk09 03 16 01 04 56更改为2016 03 09 01 04 56
  4. second awk2016 03 09 01 04 56转换为纪元时间:1457514296并在一行上同时打印这两个时间:1457514296 1457529044
  5. 第三个awk从第二个中减去第一个,以秒为单位:14748
  1. echo both time strings
  2. tr converts 09/03/16 - 01:04:56 to 09 03 16 01:04:56
  3. first awk changes 09 03 16 01 04 56 to 2016 03 09 01 04 56
  4. second awk converts 2016 03 09 01 04 56 to epoch time: 1457514296 and prints both on one line: 1457514296 1457529044
  5. third awk subtracts first from second, giving difference in seconds: 14748

awk也可以很容易地合并,但是在这里我为了清楚起见将它们分开.

the awks could also easily be merged, but here i kept each separate for clarity.

这篇关于日期:非法选项-d,查找两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆