LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Time Calculations (http://www.linuxquestions.org/questions/linux-newbie-8/time-calculations-930906/)

lonesoac0 02-23-2012 01:14 PM

Time Calculations
 
1 Attachment(s)
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. :)

Cedrik 02-23-2012 01:16 PM

What issue ?

catkin 02-23-2012 01:17 PM

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?

suicidaleggroll 02-23-2012 01:50 PM

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.

lonesoac0 02-23-2012 02:26 PM

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. :)

lithos 02-23-2012 02:48 PM

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)


All times are GMT -5. The time now is 12:31 PM.