Calculating elapsed time from existing text

Dear all

I have two text waves containing date/time in the following format:

YYYY-MM-DD HH:MM:SS.XXXXXX (a real life example: 2022-11-18 09:06:55.370769)

I would like to calculate the time between the two entries. In a simple approach I can ignore the YYYY-MM-DD, but there are times when measurements starts one day and end the other, so I need to take this into account.

Does anyone have a function that calculates the amount of time between two entries of the above format (preferably in seconds)? I guess it is possible to write myself, but with leap years etc. etc. it would be great if there is something built-in (I tried to look for it but could not find it) or written by someone else...

Best,

Johan S

I solved it myself, not so beautiful but anyway - here is the solution...

    wave/T w_Start_Time = $s_Current_DF + "start_time"
    wave/T w_End_Time = $s_Current_DF + "end_time"

    string s_Start_Time = w_Start_Time[0]
    string s_Start_Year = s_Start_Time[0,3]
    string s_Start_Month = s_Start_Time[5,6]
    string s_Start_Day = s_Start_Time[8,9]
    string s_Start_Hour = s_Start_Time[11,12]
    string s_Start_Minute = s_Start_Time[14,15]
    string s_Start_Second = s_Start_Time[17,25]

    string s_End_Time = w_End_Time[0]
    string s_End_Year = s_End_Time[0,3]
    string s_End_Month = s_End_Time[5,6]
    string s_End_Day = s_End_Time[8,9]
    string s_End_Hour = s_End_Time[11,12]
    string s_End_Minute = s_End_Time[14,15]
    string s_End_Second = s_End_Time[17,25]
   
    variable v_Measurement_Time_Start = date2secs(str2num(s_Start_Year), str2num(s_Start_Month), str2num(s_Start_Day)) + str2num(s_Start_Hour)*60*60 + str2num(s_Start_Minute)*60 + str2num(s_Start_Second)
    variable v_Measurement_Time_End = date2secs(str2num(s_End_Year), str2num(s_End_Month), str2num(s_End_Day)) + str2num(s_End_Hour)*60*60 + str2num(s_End_Minute)*60 + str2num(s_End_Second)
    variable v_Measurement_Time = v_Measurement_Time_End - v_Measurement_Time_Start

 

Edit: The  string "s_Current_DF" must be set by you in your code in case you want to use this  - this is just an example of how I solved the problem with text-waves...

The question that comes immediately to my mind is- why are the times in text waves? Seems like that times are in a reasonable format, and it should be possible to load the time/date data into a numeric wave, which would make the computation trivial.

You could alternatively use sscanf:

function ISO8601datetime2secs(string str)
    variable year, month, day, hh, mm, ss
    sscanf str, "%d-%d-%d%*[Tt ]%d:%d:%f", year, month, day, hh, mm, ss
    return v_flag == 6 ? date2secs(year, month, day) + ss + 60*mm + 60*60*hh : NaN
end

 

I don't have any real experience with HDF5, but our documentation doesn't mention date/time data. Surely there is a better way to load that data? All you HDF5 folks out there?