Finding the difference between two time stamp with date in it - shell

I would like to get a simple shell script to find the difference in two times.
Example:
Tue May 9 10:38:17 BST 2017
-rw-rw-r-- 1 unikix unikix 1387 Feb 17 11:34 ABC
The first one is the system date and other one, the time extracted from output of list command.
There is a need to check the time format also (12 hour clock and 24 clock) before finding the difference.

Try the following:
DATE1="Tue May 9 10:38:17 BST 2017"
DATE2="Feb 17 11:34"
sec1=$(date --date "${DATE1}" +"%s")
sec2=$(date --date "${DATE2}" +"%s")
diff=$((${sec1} - ${sec2}))
echo "Difference is ${diff} sec"

Related

Adding days to GNU date command with time stamp

When I run this command I get what you'd expect:
date -d "2018-06-07 + 1 days"
Fri Jun 8 00:00:00 CEST 2018
1 day is added to the day provided (using midnight as starting point).
However when I try to work in a time (17:00:00), two things happen.
date -d "2018-06-07 17:00:00 + 28 days"
Up to 25 days, the output is wrong: wrong dates/wrong time (I have run this in a loop).
Above 25 days, it starts spitting out "date: invalid date ‘2018-06-07 17:00:00 +25 days’"
The manpage says about -d /--date that is pretty much free format. But I'm starting to think the plus sign is incorrectly interpreted (maybe as a timezone offset?) when you use the time (hours:minutes:seconds)?
So how can I add days FROM a timestamped date?
For increment on the days with timestamp to work, the timestamp needs to be in the standard format returned by default by the date command. So sanitize the date to a format in which it accepts minute arithmetic and do the processing.
date -d "2018-06-07 17:00:00"
Thu, Jun 07, 2018 5:00:00 PM
Now put it in a variable, e.g. putting your string in the example below
dateStr=$(date -d "2018-06-07 17:00:00")
date -d "$dateStr + 28 days"
returns
Thu, Jul 05, 2018 5:00:00 PM
The example uses timezones from IST.

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"

Calculating time in seconds using bash

I have cron job which processes data every 15 minutes(12:00, 12:15, etc...) I need a bash function/script which determines how many seconds until the next processing cycle relative to the current time. If current time = "15:09:00 2016"
the next processing cycle would be 360 sec. Any ideas? thanks.
Get the current time in seconds since the UNIX epoch
$ now=$(date +%s)
then compute that value mod 900 (900 seconds is 15 minutes) and subtract that from 900.
$ echo $((900 - now % 900))
The date command allows a date to be provided following the -d --date option. date also understands relative dates (e.g. + 6 min, +3 days, etc..). So if you need to know what 6 minutes in the future is you can simply use date -d "+ 6 min" to find the exact time that will be. e.g.
$ date
Fri Jun 10 15:22:45 CDT 2016
$ date -d "+ 6 min"
Fri Jun 10 15:28:47 CDT 2016

Convert UTC time to GMT bash script

I am trying to convert a UTC time to GMT time in my small script, but it doesn't work:
TimestampUTC=$(date +"%s")
echo $TimestampUTC
dates=$(date -d #$TimestampUTC)
echo $dates
## 2 hours difference between UTC and GMT
Hours2=120
TimestampGMT=$((TimestampUTC - Hours2))
echo $TimestampGMT
diff=$((TimestampUTC - TimestampGMT))
echo $diff
dateGMT=$(date -d #$TimestampGMT)
echo $dateGMT
The displayed result for $dateGMT is the same as $dates.
Thanks in advance.
error in script.
Unix timestaps are given in seconds.
Hours2=120 means 120 seconds.
So your 2 timestaps are diverging by 2 minutes, not 2 hours.
This code is correct:
Hours2=7200
Also you claim having 2 hours between GMT and UTC, I'm sure you mean CET (central european time)
Note: there is nothing like a CET timestamp. It's just the normal unix timestamp displayed with a timezone offset. So independently of world location, the unix timestamp is always, worldwide, the same at the same instant.
You can replace all your code by just this
# get the timestamp 2 hours in the future from now
date2h=$(date -d "2 hours" +%s)
Which gives you the unix timestamp from the future. It is NOT the current timestamp in CET. The current CET timestamp is always the same as UTC.
How to get the time from UTC and CET? Set the environment variable TZ before the command.
$ TZ=UTC date
Mon Aug 17 11:44:05 UTC 2015
$ TZ=CET date
Mon Aug 17 13:44:05 CEST 2015
$ TZ=GMT date
Mon Aug 17 11:44:05 GMT 2015
but the timestap is always the same
$ TZ=UTC date +%s
1439812072
$ TZ=CET date +%s
1439812072
$ TZ=GMT date +%s
1439812072
GMT and UTC do not differ by 2 hours. In fact they don't differ at all. So displaying the dates of GMT and UTC will always show exactly the same number.
Also I don't know bash but I find it hard to believe that 2 hours is represented by 120 minutes. Normally when doing math with dates milliseconds are used.
In your favourite terminal use the following sequence
export TZ=GMT; date
date_format='+%d %B %Y %H:%M'
datatest="2021-11-21 12:00:00 UTC"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 CET"
echo $(date -d "$datatest" "$date_format")
datatest="2021-11-21 12:00:00 GMT"
echo $(date -d "$datatest" "$date_format")
Out:
21 November 2021 13:00
21 November 2021 12:00
21 November 2021 13:00

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.

Resources