Lazarus Pascal
Lazarus Pascal
How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)
Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.CkDateTime,
Chilkat.DtObj;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
strTimestamp: string;
dateTime: TCkDateTime;
bLocalTimezone: Boolean;
dt: TDtObj;
begin
// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
strTimestamp := '2016-11-11T14:32:17.0908971Z';
dateTime := TCkDateTime.Create;
dateTime.SetFromTimestamp(strTimestamp);
// Get a DtObj in the local timezone.
bLocalTimezone := True;
dt := TDtObj.Create;
dateTime.ToDtObj(bLocalTimezone,dt);
// Get the individual date/time components
WriteLn('-- Local Time --');
WriteLn('Year: ' + dt.Year);
WriteLn('Month: ' + dt.Month);
WriteLn('Day: ' + dt.Day);
WriteLn('Hour: ' + dt.Hour);
WriteLn('Minutes: ' + dt.Minute);
WriteLn('Seconds: ' + dt.Second);
// Get a DtObj in the GMT/UTC timezone.
bLocalTimezone := False;
dateTime.ToDtObj(bLocalTimezone,dt);
// Get the individual date/time components
WriteLn('-- UTC Time --');
WriteLn('Year: ' + dt.Year);
WriteLn('Month: ' + dt.Month);
WriteLn('Day: ' + dt.Day);
WriteLn('Hour: ' + dt.Hour);
WriteLn('Minutes: ' + dt.Minute);
WriteLn('Seconds: ' + dt.Second);
dateTime.Free;
dt.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.