Calculate time passed since $STARDATE and current date [closed] - bash

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to establish the amount of time that has passed since 24th February 2008 - time 17:28 UTC
The output would be something like:
Its been x years x months x days x hours x minute x seconds since Tottenham Won a Trophy

You can use python:
python -c "from datetime import datetime as d; print(d.today() - d(2008, 2, 24, 17, 28, 0))"
Or you have to build something like:
starttime=$(date -d "2008-02-24 17:28 UTC" +%s)
current=$(date +%s)
timediff=$((current - starttime))
echo $timediff
days=$((timediff/86400))
hours=$((timediff%86400/3600))
min=$((timediff%3600/60))
sec=$((timediff%60))
echo "$days days $hours hours $min minutes $sec seconds since start"
Years and month would be more difficult

Related

Shell: How to calculate current MMYY to previous date (-15 months)

I am trying to calculate the current date MMYY - 15 months ago.
I get current date in the format I want with shell command: date +"%m%y"
I am trying to use this current date and calculate what the MMYY is 15 months ago, what command would show me this?
Not sure why you want to get the current MMYY and calculate "15 months ago" afterwards. You could just do it in one command:
date --date='15 months ago' +"%m%y"
man date and info date are your friend.

Get the days between two dates which are in Unix Timestamp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Have a file with pull-requests details and has CreatedDate (ex: 1613698170) is in Unix timestamp format.
I want to notify to stake holders, when pull-request is open more than x days.
How to get the no of days between current date and pull-request created date in bash / groovy. So that I will execute this script in jenkins and send out notifications.
The following code:
def epochMillis = 1598098239000
def now = new Date()
def then = new Date(epochMillis)
def days = now - then
println "then: $then"
println "now: $now"
println "days between now and then: ${days}"
calculates the number of days between now and epoch millis. When executed, the above prints:
─➤ groovy solution.groovy
then: Sat Aug 22 14:10:39 CEST 2020
now: Fri Feb 19 19:01:54 CET 2021
days between now and file creation: 181

Best way to compare Timestamps in Linux shell/bash script? [duplicate]

This question already has answers here:
Bash script compare two date variables [duplicate]
(5 answers)
Closed 1 year ago.
I have to read a epoch timestamp (in seconds) from a directory /usr/local/healthcheck.txt on my Red Hate Enterprise Linux machine every ~10 minutes (polling). I need perform a comparison on the time to check if the timestamp in the healthcheck.txt file is OLDER than 50 minutes from the current time/timestamp OR if the healthcheck.txt is non-existent, to throw an error. The timestamp in the healthcheck.txt file generally looks like this (its in seconds, as stated above) :
1591783065
I was using date -d #1591783065 to convert the timestamp to Human Readable and get something like this:
Tue Jun 9 16:22:57 UTC 2020
What would be the best approach to compare the current timestamp to this timestamp in the file and check if its older than 50 minutes?
In Java , we have a Date package , and can just use compareTo to compare the times/dates, is there a simple way to do this with shell/bash scripts?
Why don't you stick with epoch-time? You can get the current time as seconds since epoch by
date +%s, so you just have to compare
if (( (healthcheck_time + 50*60) < $(date +%s) ))
then
# .... healthcheck older than 50 minutes
fi

How may I pipe a timestamp to date and convert it to datetime? [duplicate]

This question already has answers here:
Pipe string to GNU Date for conversion - how to make it read from stdin?
(5 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Assume we have a timestamp like below:
[xxxxxx ~]$ date -d#1530586185
Tue Jul 3 05:49:45 EEST 2018
and I want to pipe it to date like:
echo 1530586185 | date -d#
date: invalid date ‘#’ <-------- error message
How can I do it?
Suggestion 1:
date -d"#$(echo 1530586185)"
Suggestion 2:
DATE=$(echo 1530586185); date -d"#$DATE"

Will this code work over a new year?

Basically, I have a series of commands I want to run every other sunday. I set a cron task to run the script every sunday, then this script only allows the script to run on even weeks, thus it only runs every other sunday. My question is, will this script still work going from year to year.
if [ $(($(date +'%U') % 2)) -eq 0 ]
then
some command
fi
You have what's known as the XY problem here.
You have a problem with this part of your shell script, and you want to solve the problem by fixing the script. In reality, fixing the root cause of the problem is easier.
Simply alter your cron job to run every other Sunday:
#----+-----+-----+-----+-----+-------------------------------------------------
#min |hour |day |month|day |command
# | |of mn| |of wk|
#----+-----+-----+-----+-----+-------------------------------------------------
03 04 * * 7 expr `date +%W` % 2 >/dev/null || fortnightly.sh
See How to instruct cron to execute a job every second week? for more info.
If you don't want to specify this with cron syntax, you can use the %s format instead of %U. This will give you the number of seconds since 1st Jan 1970 UTC. You can divide this to get a week number:
$(($(date +'%s') / 604800))
Then you can do your modulo test on that.
Note the number 604800 = 7 * 86400 = 7 * 24 * 60 * 60 ie the number of seconds in one week.
If you're running this every day, you'll want to know that it's actually a Sunday. So in this case, you would divide by 86400 to get a day number. Then, armed with the knowledge that day zero was a Thursday, you can check that the result (modulo 14) is either 3 or 10, depending on which Sunday you started at.

Resources