Compute estimated time left from specific string, bash scripting

Viewed 33

I'm looking for an elegant way of computing estimated time left for a program to finish. I have a log.txt file where all kind of parameters are written and which steps are finished so far. When the photon launching phase starts, date, time and percentage reached so far is written e.g.

10/11/2020 02:48:29.079   (12.3 GB) Launched primary emission photon packets: 43.0%

The idea is to use the first and latest date and percentage and then to simply apply a linear scaling taking correctly taking into account the change of date (that could be arbitrary). Time is given in day/month/year hh:mm:ss format.

10/11/2020 02:30:24.669   (12.3 GB) Launched primary emission photon packets: 0.2%
10/11/2020 03:01:12.047   (12.3 GB) Launched primary emission photon packets: 73.1%

p2 = 73.1%

p1 = 0.2%

t2 = 10/11/2020 03:01:12.047

t1 = 10/11/2020 02:30:24.669

When solved, one obtains:

t_finish = t(100%) = (t2 - t1)/(p2 - p1)100% + (p2t1 - p1*t2)/(p2 - p1)

Finally, the time left is t_left = t_finish - t2

The output should be in seconds. Below is a part of the log.txt file that contains time and percentage reached. A solution to this problem could be very useful for future time and date calculations.

......
......
10/11/2020 02:30:18.006   (12.3 GB) Starting primary emission...
10/11/2020 02:30:18.006   (12.3 GB) Launching 3e7 primary emission photon packets
10/11/2020 02:30:24.669   (12.3 GB) Launched primary emission photon packets: 0.2%
10/11/2020 02:30:31.385   (12.3 GB) Launched primary emission photon packets: 0.4%
......
......
10/11/2020 03:01:12.047   (12.3 GB) Launched primary emission photon packets: 73.1%
......
......
1 Answers

What about using (GNU) date and bc?

#!/usr/bin/env bash

p2='73.1'
p1='0.2'
t2='10/11/2020 03:01:12.047'
t1='10/11/2020 02:30:24.669'


epoch2="$(date -d "$t2" +%s)"
epoch1="$(date -d "$t1" +%s)"

end_epoch="$(printf '( %s - %s ) / ( %s - %s ) + ( %s * %s - %s * %s ) / ( %s - %s )\n' "$epoch2" "$epoch1" "$p2" "$p1" "$p2" "$epoch1" "$p1" "$epoch2" "$p2" "$p1" | bc)"

printf "Estimated end: %s (%s)\n" "$(date -d "@$end_epoch")" "$end_epoch"
Related