hroptatyr / dateutils

nifty command line date and time utilities; fast date calculations and conversion in the shell
http://www.fresse.org/dateutils/
Other
613 stars 42 forks source link

[RFE] Floating point diff from datediff #86

Open woky opened 6 years ago

woky commented 6 years ago

I couldn't figure out from the documentation how to get floating point result from datediff. I'd like to get decimal duration in hours (? stands for modifier I'd like to use but am not aware of):

% datediff 10:30 11:00 -f '%?'
1.5

Currently I can only get duration in hours truncated to integer:

% datediff 10:30 12:00 -f '%H'
1

Of course this is possible:

% echo "$(datediff 10:30 12:00 -f '%S')/3600" | bc -l
1.50000000000000000000

But that's a little bit verbose and output is too... scientific. I realize that this would pull in complexity of floating point formatting so please disregard if out of scope. And if this is already possible, then please point me to the relevant section of manual, close this issue, and accept my apology for not reading the manual properly.

hroptatyr commented 6 years ago

Hey there, you are correct, datediff does not provide format specifiers that result in fractional units. Your proposed work-around is probably what I'd do as well. However, essentially you'd have to hard-code details that you have no way of knowing, or, if you prefer, that dateutils have been designed to abstract away from (the number of seconds in an hour isn't always 3600).

I do agree it would be useful but I see the added complexity in specifying the exact result concisely plus I'm unsure about supporting "strange" fractions, like 6.7 weeks, or 3.22333 months.

The best way to go forward about this (and maintain backward compatibility) is using an optional qualifier something like %.H. Precision could be hard-limited to 3 (full minutes) or 5 (resolution to the second) digits but it's just a matter of time until someone requests full precision control. Ah well.