Mm: ss calculator from shell command line?

From the command line prompts, what is the least keystroke method for calculating the value of mm: ss expressions such as 4:33 + 0:20 - 2:45 = 2:08 ?

This is for interactive use, not for use in a script, or for measuring elapsed time, or something similar. No mouse. There is no graphical interface.

There are thousands of implementations of mm+60*ss and (mmss/60, mmss%60) , in hundreds of languages. I can write a script in bash or ruby ​​or C for this to add another implementation. But it seems likely that this wheel does not need to be rethought when it is probably buried somewhere in bc , dc , irb or perhaps even in bash .

+6
source share
2 answers

Although this is not ideal:

 s="4:33 + 0:20 - 2:45" n=$(sed 's/\([0-9]*\):\([0-9]*\)/(\1 * 60 + \2)/g' <<< "$s" | bc) printf "%d:%02d\n" $(( $n / 60 )) $(( $n % 60 )) 

The sed command outputs (4 * 60 + 33) + (0 * 60 + 20) - (2 * 60 + 45) in bc .

Conclusion: 2:08 .

+3
source

It is not possible for several characters on a tooltip to calculate a time like this. You can do, for example, something like:

date -d "4:33 + 2:22" +%H:%M

but you need to turn on the time zone (and how time is calculated), take a conclusion, calculate again and! Do not exceed 24 hours. So this is ridiculous. One possible solution would be GNU / Units. It contains abbreviations for some common combinations:

hms -> hours, minutes, seconds time -> years, days, hours, minutes and seconds

You have: 2 hours + 23 minutes + 32 seconds You want: seconds * 8612 / 0.00011611705

Homepage: http://www.gnu.org/software/units/

0
source

Source: https://habr.com/ru/post/980041/


All Articles