Comparing timestamps in object array

This topic contains 2 replies, has 2 voices, and was last updated by  Isaac Garvin 2 years, 1 month ago.

  • Author
  • #28616

    Isaac Garvin

    Hi, probably an easy solution but it's eluding me.

    I have an object array that is essentially lines in a log filtered where one property is the timestamp of the log event and the other is the actual log event message. The timestamps are cast as DateTime. I want to do a TimeSpan of each pair of objects in the array, and have that TimeSpan stored as an additional property in the array.

    Take example object array:

    PS C:\Users\igarvin> $data

    Timestamp             Timespan Message                                                                                                          
    ---------             -------- -------                                                                                                          
    8/1/2015 5:09:34 PM            Starting to sync...                                                                                              
    8/1/2015 5:11:41 PM            Synchronization complete.                                                                                        
    8/2/2015 12:23:43 AM           Starting to sync...                                                                                              
    8/2/2015 12:25:15 AM           Synchronization complete.                                                                                                                                                                 

    I want a timespan property to record the timespan between the sync start and sync complete events. I'm just confused how to grab the results of two objects in an array, compare, and then stuff results back into one of the objects, and to iterate this process. Much googling doesn't show up anything which is odd as I would assume implementing something that would get timespans for log events would be desirable. Any ideas on this?

  • #28618


    Are the log contents in this array consistent? I.e., is it always a 'Starting to sync' log followed by a 'Synchronization complete. ' log? Are there other logs that might also show up? Assuming the logs are consistent, can the problem be described like this:

    'For each synchronization complete message, find the difference between this log's Timestamp and the Timestamp property of the 'sync start' log that came before it, and store this difference in the Timespan property of the synchronization complete log' ?

    This is a quick stab in the dark and is pretty much pseudo-code.

    for ($i = 0; $i -lt $data.Length; $i++)
            if($i.Message -like "Synchronization complete.")
                $previousLog = $data.Get($i-1)
                $y = New-TimeSpan –Start $i.TimeStamp –End $previousLog.TimeStamp
                $i.TimeSpan = $y
  • #28663

    Isaac Garvin

    Gorgeous. I just needed to do a little tinkering for my object array (e.g. use $data[$i] and $data[$-1]) and it worked perfectly. Thank you so much!

You must be logged in to reply to this topic.