LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 02-23-2012, 12:14 PM   #1
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 88

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, 16 views)

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

Rep: Reputation: 242Reputation: 242Reputation: 242
What issue ?
 
Old 02-23-2012, 12:17 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Here's the script
Code:
#!/bin/bash
read -p "Insert sleep Hour Now: " sleephour
read -p "Insert sleep Minute Now: " sleepminute
sleep_time=$(date +%r --date=$sleephour:$sleepminute:00)
new_sleeptime_hour=${sleep_time:0:2} 
new_sleeptime_minute=${sleep_time:3:2}
new_sleeptime_AM_PM=${sleep_time:9:2}

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)
new_waketime_hour=${wake_time:0:2} 
new_waketime_minute=${wake_time:3:2}
new_waketime_AM_PM=${wake_time:9:2} 

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, 12:50 PM   #4
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,721

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

Code:
blah

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

blah

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:
Code:
if [[ $new_waketime_hour -lt $new_sleeptime_hour ]]; then
   new_waketime_hour=$((new_waketime_hour + 24))
fi
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 12:52 PM.
 
Old 02-23-2012, 01:26 PM   #5
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 88

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, 01:48 PM   #6
lithos
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
Hi,

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

with the essential part of doing it:
Quote:
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 -----

#!/bin/bash

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

# 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")

echo "ORIGINAL_DATE = $ORIGINAL_DATE"
echo "NEW_DATE = $NEW_DATE"

----- 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:
Quote:
# 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 01:53 PM.
 
  


Reply

Tags
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 03:36 PM
[SOLVED] top calculations sbauer72 Linux - Distributions 2 04-01-2011 08:54 PM
threads for mathematical calculations? skywalker27182 Programming 3 02-13-2005 10:25 AM
need help with pi calculations! daven1 Programming 2 01-06-2005 07:21 PM
rough MySQL size calculations? benbroad Linux - Software 2 12-02-2004 06:01 AM


All times are GMT -5. The time now is 03:51 PM.

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