You are not logged in.
Pages: 1
How I can store milliseconds TDateTime in any kind datetime property?
Offline
Code from SynCommons.pas
procedure TimeToIso8601PChar(P: PUTF8Char; Expanded: boolean; H,M,S: cardinal;
FirstChar: AnsiChar = 'T'); overload;
// we use Thhmmss format //---------------------> but not THH:MM:SS.SSS
begin
P^ := FirstChar;
inc(P);
pWord(P)^ := TwoDigitLookupW[h];
inc(P,2);
if Expanded then begin
P^ := ':';
inc(P);
end;
pWord(P)^ := TwoDigitLookupW[M];
inc(P,2);
if Expanded then begin
P^ := ':';
inc(P);
end;
pWord(P)^ := TwoDigitLookupW[s];
// -------------->here no code with milliseconds
end;
//-------------->here convert date - it's ok
procedure DateToIso8601PChar(Date: TDateTime; P: PUTF8Char; Expanded: boolean); overload;
// we use YYYYMMDD date format
var Y,M,D: word;
begin
DecodeDate(Date,Y,M,D);
DateToIso8601PChar(P,Expanded,Y,M,D);
end;
/// convert a date into 'YYYY-MM-DD' date format
function DateToIso8601Text(Date: TDateTime): RawUTF8;
begin
SetLength(Result,10);
DateToIso8601PChar(Date,pointer(Result),True);
end;
procedure TimeToIso8601PChar(Time: TDateTime; P: PUTF8Char; Expanded: boolean;
FirstChar: AnsiChar = 'T'); overload;
// we use Thhmmss format
var H,M,S,MS: word;
begin
DecodeTime(Time,H,M,S,MS); //-------------->here we have MS, but don't use it after
TimeToIso8601PChar(P,Expanded,H,M,S,FirstChar);
end;
function DateTimeToIso8601(D: TDateTime; Expanded: boolean;
FirstChar: AnsiChar='T'): RawUTF8;
// we use YYYYMMDDThhmmss format
var tmp: array[0..31] of AnsiChar;
begin
if Expanded then begin //-------------->here Expanded version TDatetime
DateToIso8601PChar(D,tmp,true);
TimeToIso8601PChar(D,@tmp[10],true,FirstChar);
SetString(result,PAnsiChar(@tmp),19);
end else begin
DateToIso8601PChar(D,tmp,false);
TimeToIso8601PChar(D,@tmp[8],false,FirstChar);
SetString(result,PAnsiChar(@tmp),15);
end;
end;
Offline
So you want JSON storage?
The easiest in your case is to define a plain double property or parameter, which will be stored as floating point in JSON and in the DB.
Then you can use this double value as TDateTime.
And you will get the ms resolution.
Offline
@ab, since we are on this, may I take this chance to ask, do you have any info on the comparison of performance between storing the TDateTime value as Iso8601 string (by default in mORMot) vs storing as Double?
By performance I mean querying performance, currently I use TDateTime fields (thus stored as Iso8601 strings in the sqlite3 db by default), and my query uses 'ORDER BY myDateField DESC' or something like that. I have noticed something the querying is slow, I'm wondering how many performance I will gain if I change my date time fields from TDateTime(Iso8601) to Double...
PS, I do have an index on the date time fields currently,.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Pages: 1