Calculate date time difference [duplicate] - shell

This question already has answers here:
Difference in date time
(3 answers)
Closed 9 years ago.
I have 2 variables.
GMDCOM variable stores the date time in below format
Tue Oct 1 13:37:38 2013
Tue Oct 1 13:32:40 2013
Tue Oct 1 13:37:53 2013
GMDRRS variable stores the date time in the below format
Tue Oct 1 13:35:33 2013
Tue Oct 1 13:34:33 2013
Tue Oct 1 13:32:33 2013
I want to calculate the datetime difference
e.g Tue Oct 1 13:37:38 2013 - Tue Oct 1 13:35:33 2013 in hh:mm:ss format
and store it in another variable. I dont want to use PERL, AWK or SED. Instead i want to use normal BASH Shell commands to achieve it. Please help.

Try following
#!/bin/bash
GMDCOM='Tue Oct 1 13:37:38 2013'
GMDRRS='Tue Oct 1 13:35:33 2013'
d1=$(date -d "$GMDCOM" +%s)
d2=$(date -d "$GMDRRS" +%s)
dd=$(($d1-$d2))
ss=$(($dd%60))
mm=$((($dd/60)%60))
hh=$((($dd/3600)%60))
printf "%02d:%02d:%02d\n" "$hh" "$mm" "$ss"

Related

convert multiple variable output to a table in bash

I have 3 variables, $commonName, $expiryDate and $DaysRemInUnixEpoch. Each variable has 3 lines as below output. I want to display output of all 3 variables in to 3 different columns. I tried looking for solution using printf but no luck. Can anyone please advise if they have done this in the past using printf and how? Any help will be much appreciated.
Below are 3 variables output together in one column. I want to split in to 3 columns having 3 rows in each column.
bash-4.1$ echo -e "$commonName\n$expiryDate\n$daysRemInUnixEpoch"
mycertificate_mycert.mycomp.net
PSIN0P551
ROOTROOTCA
Feb 6 2022 11:57:32 GMT
Jan 9 2023 18:51:25 GMT
Mar 12 2035 18:24:54 GMT
682
1020
5465
bash-4.1$
desired output I am looking for is something like below
mycertificate_mycert.mycomp.net Feb 6 2022 11:57:32 GMT 682
PSIN0P551 Jan 9 2023 18:51:25 GMT 1020
ROOTROOTCA Mar 12 2035 18:24:54 GMT 5465
With bash (Process Substitution), paste and column:
paste -d ';' <(echo "$commonName") <(echo "$expiryDate") <(echo "$daysRemInUnixEpoch") | column -s ';' -t
Output:
mycertificate_mycert.mycomp.net Feb 6 2022 11:57:32 GMT 682
PSIN0P551 Jan 9 2023 18:51:25 GMT 1020
ROOTROOTCA Mar 12 2035 18:24:54 GMT 5465
I assume that your variables do not contain ;.
see: man paste and man column

Add X days to a particular date in BASH

Totally new to BASH. Apologies in advance.
Problem
I'd like to add X days to a specific date.
Code
I figured out that date in BASH retrieves the current date.
I also figured out that I can add X days to the current date in the following way,
expiration_date=$ date -v +1d
which gives,
Tue Sep 26 20:28:13 CEST 2017
which is indeed the date of writing plus X=1 days.
Question
In stead of date in the command line above, I'd like to insert a particular date to which X days will be added, e.g. 20/09/2017.
Don't care about the format of the particular date.
In other words: How do I make the following work,
expiration_date=$ '20/09/2017' -v +1d
Tried this answer, but doesn't do what I want.
Edit: Did not know things are different for OSX.
You can do this way:
dt='2017-09-20'
date -d "$dt +1 day"
Thu Sep 21 00:00:00 EDT 2017
date -d "$dt +2 day"
Fri Sep 22 00:00:00 EDT 2017
It seems OP is using OSX. You can use date addition this way:
s='20/09/2017'
date -j -v +1d -f "%d/%m/%Y" "$s"
Thu Sep 21 14:49:51 EDT 2017
You can do something like this:
date -d "Sun Sep 6 02:00:00 IST 2012+10 days"

Find and Echo only the date (with format) in String Output on Bash

I am trying to get the date "+%a %b %d %R:%S %Y" in bash.
here's the sample command and output
$ xscreensaver-command --time
XScreenSaver 5.32: screen non-blanked since Thu Oct 29 12:15:05 2015 (hacks: #184, #60)
I am trying to get the the value Thu Oct 29 12:15:05 2015 on the string.
How can I achieve this?
Try to append with GNU grep:
2>&1 | grep -Po 'since \K.*(?= \()'
Output:
Thu Oct 29 12:15:05 2015

Bash convert epoch to date, showing wrong time

How come date is converting to wrong time?
result=$(ls /path/to/file/File.*)
#/path/to/file/File.1361234760790
currentIndexTime=${result##*.}
echo "$currentIndexTime"
#1361234760790
date -d#"$currentIndexTime"
#Tue 24 Oct 45105 10:53:10 PM GMT
This particular timestamp is in milliseconds since the epoch, not the standard seconds since the epoch. Divide by 1000:
$ date -d #1361234760.790
Mon Feb 18 17:46:00 MST 2013
For Mac OS X, it's date -r <timestamp_in_seconds_with_no_fractions>
$ date -r 1553024528
Tue Mar 19 12:42:08 PDT 2019
or
$ date -r `expr 1553024527882 / 1000`
Tue Mar 19 12:42:07 PDT 2019
or
$ date -r $((1553024527882/1000))
Tue Mar 19 12:42:07 PDT 2019
You can use bash arithmetic expansion to perform the division:
date -d #$((value/1000))
Note that "value" is a bash variable with the $ being optional; i.e., $value or value can be used.

sed: convert time(3) seconds in a table into printable date (spamdb)

I get the following from spamdb, where the third field represents the time in seconds since the Epoch.
Cns# spamdb | fgrep TRAPPED
TRAPPED|113.163.117.129|1360836903
TRAPPED|113.171.216.201|1360837481
TRAPPED|122.177.159.61|1360844596
TRAPPED|36.231.9.231|1360865649
TRAPPED|37.146.207.209|1360832096
TRAPPED|212.156.98.210|1360837015
TRAPPED|59.99.160.62|1360839785
TRAPPED|86.127.116.162|1360840492
TRAPPED|92.83.139.194|1360843056
TRAPPED|219.71.12.150|1360844704
I want to sort this table by the time, and print the time field with date -r, such that it's presentable and clear when the event has occurred.
How do I do this in tcsh on OpenBSD?
Sorting with sort is easy, and so is editing with sed; but how do I make sed execute date -r or equivalent?
There are indeed a few obstacles here: first, you basically have to separate the data, and then one part of it is presented as-is, whereas another part has to be passed down to date -r for date formatting, prior to being presented to the user.
Another obstacle is making sure the output is aligned: apparently, it's quite difficult to handle the tab character in the shell, possibly only on the BSDs:
sed replace literal TAB
Replacing / with TAB using sed
Also, as we end up piping this to sh for execution, we have to use a different separator for the fields other than the pipe character, |.
So far, this is the best snippet I could come up with, it seems to work great in my tcsh:
Cns# spamdb | fgrep TRAPPED | sort -n -t '|' -k 3 | sed -E -e 's#\|###g' \
-e 's#^([A-Z]+)#([0-9.]+)#([0-9]+)$#"echo -n \2_"; "date -r \3"#g' | \
xargs -n1 sh -c | awk '{gsub("_","\t",$0); print;}'
37.146.207.209 Thu Feb 14 00:54:56 PST 2013
113.163.117.129 Thu Feb 14 02:15:03 PST 2013
212.156.98.210 Thu Feb 14 02:16:55 PST 2013
113.171.216.201 Thu Feb 14 02:24:41 PST 2013
59.99.160.62 Thu Feb 14 03:03:05 PST 2013
86.127.116.162 Thu Feb 14 03:14:52 PST 2013
92.83.139.194 Thu Feb 14 03:57:36 PST 2013
122.177.159.61 Thu Feb 14 04:23:16 PST 2013
219.71.12.150 Thu Feb 14 04:25:04 PST 2013
36.231.9.231 Thu Feb 14 10:14:09 PST 2013

Resources