LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Do you know a command or shell script to sum hours:minutes:seconds? (https://www.linuxquestions.org/questions/linux-software-2/do-you-know-a-command-or-shell-script-to-sum-hours-minutes-seconds-4175610209/)

dedec0 07-19-2017 03:02 PM

Do you know a command or shell script to sum hours:minutes:seconds?
 
I have a scientific calculator that has a [NICE] button which deals with time. "Time", for it, is a unlimited number of hours (or degrees, if you prefer), number of minutes and number of seconds. It works like this:

We type number of hours (not limited to 24 or 360). Nothing new here.

After that, we may type "[NICE], number of minutes, [NICE]". This will make the number of hours (a real) be increased with the right fractional part. For example:

"8 [NICE] 30 [NICE]" gives the result 8.5, which means 8 and half hours. Good?

After that, we may follow with "number of seconds, [NICE]", which will work similarly to what was done for the minutes we typed. For example:

"8 [NICE] 30 [NICE] 900 [NICE]" gives the result of 8.75. Observe that 900 seconds is 0.25 hours, which was added to our previous result.

For any real number, we may check what it represents in [HOURS/DEGREES]:MINUTES:SECONDS. For example:

Make 8.75 be the current number, in any way. Then we type "[shift] [NICE]" to get "8 o 45 o 0". My "o" here is the closest I could type to what my calc shows, which is an "o" at half line height. But you got it:

8 degrees 45' 0"

So, do you know a script or something ready to use that works in a shell? Something like this would happen in a terminal:

Code:

$ time 8 45 00  # separators maybe ":" too
8.75
$ time 8 30 0 + 0 15 0  # "1" for min. or sec. mean "01", *not* "10"
8.75
$ time -r 8.75  # "-r" or "--reverse" means... do the reverse! haha
8:45:00

I found this, but it is not really what I wanted.

So, do you have something to make that?

Laserbeak 07-19-2017 03:20 PM

I'm sure complex programs like Mathematica do that, but I haven't run across something free that just does that (not to say such thing doesn't exist), but it should be quite easy to program in C or something if you are so inclined.

Laserbeak 07-19-2017 03:24 PM

This would be actually a cool iPhone program to write... hmmm

dedec0 07-19-2017 03:55 PM

I agree with you, Laserbeak. It is actually not that hard to make, so I imagined that it would exist. Quite useful! I do not know how to use everything my sci calc has, but what I called the "[NICE]" button has been useful several times! (:

I will suggest it to gcalctool, which does not have it - seems - among all its tastes: basic, advanced, financial, scientific and programmable.

If I make the script, or if needing a help with some details, I post here.

dc.martin 07-19-2017 06:26 PM

Try something like this:

Quote:

#!/bin/bash
## script name: hours
## invocation: hours WHOLE_NUMBER_OF_HOURS [WHOLE_NUMBER_OF_MINUTES] [WHOLE_NUMBER_OF_SECONDS]
## Expresses hours in decimal based on input of hours, minutes, seconds
## minutes input can exceed 1 hour, ie, m>60 is ok
## seconds input can exceed 1 minute or 1 hour, ie, s>60 is ok, as is s>3600
h=$1 ## hours
m=$2 ## minutes
s=$3 ## seconds
#
## set hours, minutes, and second to 0 if variables are unset
#
echo ${h:-0} hours ${m:-0} minutes ${s:-0} seconds
bc <<< "scale=4;(${h:-0}*60^2 + ${m:-0}*60 + ${s:-0})/60^2"
Here are examples:

Quote:

$ hours 1
1 hours 0 minutes 0 seconds
1.0000

$ hours 1 45
1 hours 45 minutes 0 seconds
1.7500

$ hours 1 45 1800
1 hours 45 minutes 1800 seconds
2.2500


All times are GMT -5. The time now is 10:17 AM.