# This function is to help us analyse timestamp difference given a file with
# a single column where each row has GStreamer PTS/DTS/Duration or timestamp
# of the form HH:MM:SS:sssssssss in each row.
# Adapted from https://stackoverflow.com/questions/8903239/how-to-calculate-time-elapsed-in-bash-script
function tsd --description "Get timestamp difference between rows" -a file
    if test (count $argv) -lt 1
        echo "Usage: tsd <file_name>"
        echo "Eg: tsd gstreamer-ts.log"
    else
        set -l RESULT_FILE /tmp/gst-ts-analysis.csv

        if test -e $RESULT_FILE
            echo "Result file $RESULT_FILE exists, removing..."
            rm $RESULT_FILE
        end
        while read -la first_line; and read -la second_line
            set -l fist_date $(date -u -d "$first_line" +"%s.%N")
            set -l second_date $(date -u -d "$second_line" +"%s.%N")
            date -u -d "0 $second_date sec - $fist_date sec" +"%H:%M:%S.%N" >>$RESULT_FILE
        end <$file
        nvim -R $RESULT_FILE
    end
end
