none
Converting UNIX timestamp to DateTime

    Question

  • I would like to seek your advice or assistance in resolving my problem with parsing a text log -- one of my message type has a UNIX timestamp (number of seconds since 1-Jan-1970 00:00AM) and I want to convert it to a DateTime field. My config code cannot do the required conversion. ToDateTime function seems not able to convert (ticks as long), only string representations of a date/time value.

    The example log file line is:

    2016-08-01 07:10:51 SYNC: System Clock out of sync with GPS! - System: 1470035451, Board: 1470035440

    My current config code to parse the "System" timestamp (value=1470035451 above) is as follows:

    message OutOfSyncWithGPS with
     EntryInfo { Tag = "SYNC:"
     , Regex = @"^(?<msgts>[-0-9]+\s[:0-9]+)\s(?<msgtype>SYNC:)\s(?<msgtext>.*)?\s(?<textts>System:\s\d+\,\sBoard:\s\d+)$"
     , Priority = 1
     }
     : TextLogMsg
    {
    }

    syntax OutOfSyncWithGPSSystemDateTime = system:"System: " systemticks:regex{\d+} board:"Board: " boardticks:regex{\d+}
     => DateTime((systemticks as long) * 10000000 + 255690000000);

    DateTime get texttsSystemDateTime(this OutOfSyncWithGPS msg) with EntryFieldInfo {IsTimestamp = false, IsLocalTime = false }
    {
     optional DateTime SystemDateTime = OutOfSyncWithGPSSystemDateTime(msg.textts);
     if(SystemDateTime == nothing)
     {
      return ToDateTime("1970-01-01 00:00:00.0000000") as DateTime;
     }
     else
     {
      return SystemDateTime as DateTime;
     }
    }

    Thanks in advance.

    Monday, August 8, 2016 6:31 PM

Answers

  • Found this gem, grepping for "TimeSpan" in the sub-directory AppData\Local\Microsoft\MessageAnalyzer\OPNAndConfiguration:

    NTP.opn contains the code:

    DateTime get T2(this Message m)
    {
            ulong interv = (m.TransmitTimestamp as ulong);
            double sec = ((interv & 0xFFFFFFFF00000000) >> 32) + ((interv & 0x00000000FFFFFFFF) as double) / 4294967296; // 4294967296 = power(2, 32)
            DateTime ts = CreateDateTime(1900, 1, 1).AddSeconds(sec);

     return ts;
    }

    For my case, I used the adjusted line below (myticks being string):

            DateTime ts = CreateDateTime(1970, 1, 1).AddSeconds(myticks as long);

    Hope this helps.

    • Marked as answer by juandering Saturday, August 13, 2016 4:21 PM
    Saturday, August 13, 2016 4:21 PM

All replies

  • Just wanted to re-phrase the problem:

    Given the text log line below, how do I enable my custom config parser to extract and convert the "System:" date/time in seconds since 01-Jan-1970 00:00AM, "1470035451" to DateTime? I will later compare this DateTime value to the timestamp DateTime value of the line, "2016-08-01 07:10:51", which I am able to extract already.

    2016-08-01 07:10:51 SYNC: System Clock out of sync with GPS! - System: 1470035451, Board: 1470035440

    If I am able to extract "System:" date/time then I will be able to extract "Board:" date/time also.

    Thanks in advance.

    Wednesday, August 10, 2016 5:28 AM
  • Found this gem, grepping for "TimeSpan" in the sub-directory AppData\Local\Microsoft\MessageAnalyzer\OPNAndConfiguration:

    NTP.opn contains the code:

    DateTime get T2(this Message m)
    {
            ulong interv = (m.TransmitTimestamp as ulong);
            double sec = ((interv & 0xFFFFFFFF00000000) >> 32) + ((interv & 0x00000000FFFFFFFF) as double) / 4294967296; // 4294967296 = power(2, 32)
            DateTime ts = CreateDateTime(1900, 1, 1).AddSeconds(sec);

     return ts;
    }

    For my case, I used the adjusted line below (myticks being string):

            DateTime ts = CreateDateTime(1970, 1, 1).AddSeconds(myticks as long);

    Hope this helps.

    • Marked as answer by juandering Saturday, August 13, 2016 4:21 PM
    Saturday, August 13, 2016 4:21 PM