Parse Time String

Use the function below to parse a string like one of the following into seconds:
3:20:05 AM 3:45:00 PM 13:15:44

If you have a date-time string and wish to parse it into seconds, use the Parse Date,Time and Date/Time Strings snippet.

To represent midnight (the beginning of a day), use 00:00:00 or 12:00:00 AM. Using 24:00:00 to represent midnight will not produce any kind of error, however the function will return 86400 seconds, which is probably not the result you expect.

//**
// Convert a formatted time string into a number
// of seconds.  The function returns -1 if there
// was an error.
//*
Function Time2Secs(timeString)
    String timeString
 
    // NOTE:  timeString is assumed to be a colon-separated
    // string as hours:minutes:seconds PM where
    // the PM may also be AM or may be omitted.
    // Leading zeros are allowed but not required for each
    // of the three digit groups.
    // The space between the seconds digits and the
    // AM or PM is optional.  If neither AM nor PM is
    // found, the time will be assumed to already be in
    // 24-hour format.  The case of AM and PM does not matter.
    // The range of the digits is not checked.
    Variable hours, minutes, seconds
    String ampmStr = ""
    sscanf  timeString, "%u:%u:%u%s", hours, minutes, seconds, ampmStr
    if (V_flag < 3)
        print "Error in Time2Secs:  could not successfully parse the time."
        return -1
    elseif (V_flag == 4)
        // Determine whether AM or PM or something bogus
        // was used.
        if (cmpstr(" ", ampmStr[0]) == 0)
            // Get rid of the leading space.
            ampmStr = ampmStr[1, strlen(ampmStr) - 1]
        endif
        // Test that ampmStr is now either "AM" or "PM"
        // (case insensitive).  If not, then it's a bogus string.
        ampmStr = UpperStr(ampmStr)
        StrSwitch (ampmStr)
            Case "AM":
                // Compensate for the fact that, eg., 12:30:30 AM is
                // only 30 minutes and 30 seconds past midnight (the
                // beginning of the day), and not 12 hours 30 minutes
                // and 30 seconds.
                if (hours == 12)
                    hours -= 12
                endif
                break
            Case "PM":
                // Compensate for the fact that, eg., 12:30:30 PM is
                // only 30 minutes and 30 seconds past noon, which
                // is 12 hours past midnight (the beginning of the day),
                // and not 24 hours 30 minutes and 30 seconds.
                if (hours == 12)
                    // Don't need to do anything.
                else
                    hours += 12            
                endif
                break
            default:
                // It's bogus, report error to user.
                print "Error in Time2Secs:  Could not parse AM/PM string."
                return -1
        EndSwitch
    endif
 
    // Do the conversion into seconds.
    seconds += (minutes * 60) + (hours * 60 * 60)
    return seconds
End


Note: Edited on June 29, 2010 to give correct results with time strings such as "12:30:30 AM" and "12:30:30 PM".

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More