Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-23-2012, 01:14 PM   #1
Registered: Jan 2010
Distribution: Ubuntu
Posts: 95

Rep: Reputation: 4
Time Calculations

Hello all,

I have written a Bash script that kind-of works. My end goal is to calculate the time difference between the two times that I input. The reason that I say that my script kind-of works is that it sometimes calculates the correct values that I want. If I input 22, 0, 6, 0 then the output will be the correct value of 8.

Any help on this issue would be greatly appreciated.
Attached Files
File Type: txt script2.txt (762 Bytes, 17 views)

Last edited by lonesoac0; 02-23-2012 at 01:15 PM. Reason: No attachment
Old 02-23-2012, 01:16 PM   #2
Senior Member
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
What issue ?
Old 02-23-2012, 01:17 PM   #3
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Here's the script
read -p "Insert sleep Hour Now: " sleephour
read -p "Insert sleep Minute Now: " sleepminute
sleep_time=$(date +%r --date=$sleephour:$sleepminute:00)

echo $new_sleeptime_hour:$new_sleeptime_minute $new_sleeptime_AM_PM  

read -p "Insert wake Hour Now: " wakehour
read -p "Insert wake Minute Now: " wakeminute
wake_time=$(date +%r --date=$wakehour:$wakeminute:00)

echo $new_waketime_hour:$new_waketime_minute $new_waketime_AM_PM  

total_time=$((new_sleeptime_hour + new_waketime_hour)) 

echo $((24 - total_time)) 

exit 0
Can you give us an example of when it doesn't work?
Old 02-23-2012, 01:50 PM   #4
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Why not use the %s argument to date instead? It will remove the 0/24 ambiguity.


sleep_time=$(date +%s --date=$sleephour:$sleepminute:00)


wake_time=$(date +%s --date=$wakehour:$wakeminute:00)

total_time=$(echo "($wake_time - $sleep_time)/3600" | bc -l)
The problem with the way you're calculating delta time is it can't handle a sleep time after 24/0, or a wake time before 24/0. To do it your way you're going to need a check like:
if [[ $new_waketime_hour -lt $new_sleeptime_hour ]]; then
   new_waketime_hour=$((new_waketime_hour + 24))
total_time=$((new_waketime_hour - new_sleeptime_hour))
But even then it still doesn't take into account minutes.

%s will be simpler and more robust.

Last edited by suicidaleggroll; 02-23-2012 at 01:52 PM.
Old 02-23-2012, 02:26 PM   #5
Registered: Jan 2010
Distribution: Ubuntu
Posts: 95

Original Poster
Rep: Reputation: 4
Thank you all for your posts! I am trying to learn Bash and I tried my best at it before I submited my issue to this forum. Anywho, I will definately try out all of your sugestions tonight.
Old 02-23-2012, 02:48 PM   #6
Senior Member
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217

I'd like to present you a little more complex Bash time operations like here

with the essential part of doing it:
The basic algorithm uses the fact that the ‘date’ command can output the “seconds since epoch�? as well as be provided a date other than “now�? with the –d option (see above disclaimer). So, if we give it a specific date, and request the output in %s we get the “seconds since the epoch�? for the date we’ve supplied. Now, we apply our offset (+ or -) and resubmit the date command with the modified epoch time.

----- Start of Script -----


# The date we want to convert
# can be any valid date format - I like this one myself

# provide the original date instead of "now" and ask for "seconds since epoch"
ORIGINAL_EPOCH=$(date -d "${ORIGINAL_DATE}" "+%s")

# Now apply our offset, in this example we subtract 1 day
# Remember that $((.)) does arithmetic in bash (aka ksh)
# and can be nested. *hug gnu*
NEW_EPOCH=$((${ORIGINAL_EPOCH} - $((60 * 60 * 24))))

# We now have our target date in EPOCH format, we need to get it back to our
# original format.
NEW_DATE=$(date -d "1970-01-01 ${NEW_EPOCH} sec" "+%Y%m%d")

# And there we have it! $NEW_DATE contains "20060331"

# For those that like a tidyier script (like me)
# here's a sample of the above in one line.
NEW_DATE=$(date -d "1970-01-01 $(($(date -d "${ORIGINAL_DATE}" "+%s") - $((60 * 60 * 24)))) sec" "+%Y%m%d")


----- End of Script -----

prints out:

ORIGINAL_DATE = 20060401
NEW_DATE = 20060331

There you have it. Enjoy!
The essential is in converting a date/time to EPOCH format and do the math, then displaying it in the format you like
a quick example displaying time:
# echo "Time needed: " $(date -d "1970-01-01 $thetime sec" +"%H:%M:%S") / $thetime "secs"
## prints out the time ( like: 00:01:29 / 89 secs )
(from my time calculating notes)

Last edited by lithos; 02-23-2012 at 02:53 PM.


bash, math, time

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Date Calculations using script!! anishkumarv Linux - Newbie 9 09-24-2011 04:36 PM
[SOLVED] top calculations sbauer72 Linux - Distributions 2 04-01-2011 09:54 PM
threads for mathematical calculations? skywalker27182 Programming 3 02-13-2005 11:25 AM
need help with pi calculations! daven1 Programming 2 01-06-2005 08:21 PM
rough MySQL size calculations? benbroad Linux - Software 2 12-02-2004 07:01 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration