#1 2013-02-02 12:36:51

arquivo59
Member
Registered: 2013-01-14
Posts: 24

TModTime to TDateTime

Hi. How can I convert TModTime / TCreateTime into TDatetime to show it to the user?

Offline

#2 2013-02-02 15:27:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: TModTime to TDateTime

In short: PIso8601(@Value)^.ToDateTime or the corresponding methods of Iso8601 wrapper - or even the corresponding methods in mORMoti18n.pas.

In the SAD pdf, search for TModTime keyword in the index, at the beginning of the file:

Date and time fields

For TTimeLog / TModTime / TCreateTime, a proprietary fast Int64 date time format will map the Iso8601 record type, as defined in SynCommons.pas unit:
- 0..5 bits will map seconds,
- 6..11 bits will map minutes,
- 12..16 bits will map hours,
- 17..21 bits will map days (minus one),
- 22..25 bits will map months (minus one),
- 26..38 bits will map years.

This format will be very fast for comparing dates or convert into/from text, and will be stored more efficiently than plain ISO 8601 TEXT as used for TDateTime fields.

Note that since TTimeLog type is bit-oriented, you can't just use add or substract two TTimeLog values when doing such date/time computation: use a TDateTime temporary conversion in such case. See for instance how the TSQLRest.ServerTimeStamp property is computed:

function TSQLRest.GetServerTimeStamp: TTimeLog;
begin
  PIso8601(@result)^.From(Now+fServerTimeStampOffset);
end;


procedure TSQLRest.SetServerTimeStamp(const Value: TTimeLog);
begin
  fServerTimeStampOffset := PIso8601(@Value)^.ToDateTime-Now;
end;

But if you simply want to compare TTimeLog kind of date/time, it is safe to directly compare their Int64 underlying value.

Due to compiler limitation in older versions of Delphi, direct typecast of a TTimeLog or Int64 variable into a Iso8601 record (as with Iso8601(aTimeLog).ToDateTime) could create an internal compiler error. In order to circumvent this bug, you would have to use a pointer typecast, e.g. as in PIso8601(Value)^.ToDateTime above.

Offline

Board footer

Powered by FluxBB