0

I need to calculate the time in hours since a specific input.

I tried some different codes and tools but couldn't get any result yet ...

So to summarize the requirement here I just need to time gone from now since specific input?

eg, in a simple way, i need the time consumed since 6/12 4:40 PM?

mshafey
  • 89
  • 1
  • 9
  • try `date "+%m/%d %r"`. it's giving output like `06/13 07:34:10 PM IST` – Tanmaya Meher Jun 13 '20 at 14:05
  • or use `date +"%m/%d %I:%M %p"` – Tanmaya Meher Jun 13 '20 at 14:12
  • [Writing shells script to display time in am or pm notation](https://stackoverflow.com/questions/15056591/writing-shells-script-to-display-time-in-am-or-pm-notation) and [Time / Date Commands, advanced bash scripting guide](http://tldp.org/LDP/abs/html/timedate.html) – Tanmaya Meher Jun 13 '20 at 14:13
  • i don't want to display the time in any format, I need the time "how much hours" since a specific time – mshafey Jun 14 '20 at 20:22
  • Like: now is 22:21 - after 1 hour the time difference will be 1 hour (that's what I need actually) – mshafey Jun 14 '20 at 20:23
  • Another example: if now is 22:21, how many hours since 7:13 same day? and if it was yesterday what will be the hours difference as well – mshafey Jun 14 '20 at 20:24
  • https://stackoverflow.com/questions/3096953/how-to-calculate-the-time-interval-between-two-time-strings – RufusVS Jun 14 '20 at 21:59
  • You just changed the question entirely. Anyways, these might help. https://unix.stackexchange.com/questions/317139/date-difference-calculation https://unix.stackexchange.com/questions/24626/quickly-calculate-date-differences – Tanmaya Meher Jun 15 '20 at 18:58

1 Answers1

1

If your script is running at time t0 and it's still running at time t1 and you want to know how many hours have elapsed between t0 and t1, I think the easiest way to do that is to use the UNIX epoch.

So at time t0 store the epoch time:

t0="$(date '+%s')"

and then at time t1 get the epoch time again:

t1="$(date '+%s')"

then you can just query the difference between the two times to get the seconds, and divide by 3600 if you want that in hours:

(( elapsed = t1 - t0 ))
(( hours = elapsed / 3600 ))

Korn shell isn't the best tool for division though. You might prefer to stick with the number of seconds elapsed.