Log UTC DateTime using the ISO8601 format.

Hi,

Using TTMSLoggerTextOutputHandler and;
TMSLogger.UtcTimeStamp := true;
TMSLogger.Outputs := [loTimeStamp, loValue];

I'm trying use the ISO8601 format for the UTC datetime being logged (loTimeStamp).

I'm not sure how to achieve this, and I'm unable to find a solution in the documentation. Do you have any information to share on the topic?

You can set the TimestampFormat property and provide a specific date time format, for example:

TMSLogger.OutputFormats.TimeStampFormat := '[{%"yyyy-mm-dd hh:nn:ss"dt}]';

Yes, I have been trying to use the TimeStampFormat, I might be wrong but the
ISO8601 date has a somewhat conditional output, thus I cannot see how I can use a fixed format string.

The way I understand the ISO8601 date is that it will have a "Z" at the end of the date sting if time has zero offset to UTC, and "±hh:mm" will be added (replace the "Z") if time has an offset to UTC.

So I usually call System.DateUtils.DateToISO8601(Now, false); to get the format I need, but I not able to make this call with the TMSLogger I think.

Dates are in local time zone, so you can get the current time one offset of the machine and set TimeStampFormat accordingly, adding the suffix +hh:mm manually to it.

Thank you.

I hope the way I solved it is correct. I'll post it here in case someone else needs it aswell.

const
  PLACEHOLDER = 'PLACEHOLDER';
  Neg: array[Boolean] of string = ('+', '-');
begin
  // Add a placeholder to be replaced with 'Z' or the UTC time offset.
  var lTimeStampFormat := Format('{%%"[yyyy-mm-dd hh:nn:ss.zzz%s]"dt}', [PLACEHOLDER]);
  // I.e: {%"[yyyy-mm-dd hh:nn:ss.zzzPLACEHOLDER]"dt}

  // Slightly modified code from System.DateUtils.DateToISO8601()
  var lTimeZone := TTimeZone.Local;
  var lBias := Trunc(lTimeZone.GetUTCOffset(Now).Negate.TotalMinutes);
  if lBias <> 0 then begin
    var lOffsetTimeStr := Format('%s%.02d:%.02d', [Neg[lBias > 0], Abs(lBias) div MinsPerHour, Abs(lBias) mod MinsPerHour]);
    lTimeStampFormat := lTimeStampFormat.Replace(PLACEHOLDER, lOffsetTimeStr);
    // I.e: {%"[yyyy-mm-dd hh:nn:ss.zzz+02:00]"dt}
  end else begin
    lTimeStampFormat := lTimeStampFormat.Replace(PLACEHOLDER, '''Z''');
    // I.e: {%"[yyyy-mm-dd hh:nn:ss.zzz''Z'']"dt
  end;

  // Assign the UTC ISO8601 format string to the logger
  TMSLogger.OutputFormats.TimeStampFormat := lTimeStampFormat;
  TMSLogger.UtcTimeStamp := true;
  ...
  ...
end;
1 Like