Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 13 total
Thread DateTime (EWB) <-> TDateTime (Delphi)
Tue, Jul 19 2016 2:21 PMPermanent Link

thomh

Hi,

Is there an easy way to convert DateTime in EWB to Delphi TDateTime and back again?

Thanks.

// Thom
Tue, Jul 19 2016 3:20 PMPermanent Link

Walter Matte

Tactical Business Corporation

Routines I use.....

function UNIXTimeToDateTimeFAST(UnixTime: Int64): TDateTime;
const
 MsecPerDay : int64 = 86400000;
 DayDiff    : int64 = 25569;
var  
 DT         : TDateTime;
begin                  
 DT := (UnixTime / MsecPerDay) + DayDiff;       
 result := DT;              // swap  
//  result := UTCToLocal(DT);    // 1.02 Comment Out WBM
end;      

function DateTimeToUNIXTimeFAST(DelphiTime : TDateTime): Int64;
const
 MsecPerDay : int64 = 86400000;
 DayDiff    : int64 = 25569;
var  
 DT         : TDateTime;
 X          : extended;
begin                                        
//  DT := LocalToUTC(DelphiTime);  // 1.02 Comment Out WBM
 DT := DelphiTime;            // swap

 x := (DT - DayDiff) * MsecPerDay;
 result := round(X);
end;

function UNIXTimeToTimeFAST(UnixTime: Int64): TDateTime;
const
 MsecPerDay : int64 = 86400000;
 DayDiff    : int64 = 25569;
var  
 DT         : TDateTime;
begin                  
 DT := (UnixTime / MsecPerDay);       
 result := DT;              // swap  
//  result := UTCToLocal(DT);    // 1.02 Comment Out WBM
end;      

function TimeToUNIXTimeFAST(DelphiTime : TDateTime): Int64;
const
 MsecPerDay : int64 = 86400000;
 DayDiff    : int64 = 25569;
var  
 DT         : TDateTime;
 X          : extended;
begin                                        
//  DT := LocalToUTC(DelphiTime);  // 1.02 Comment Out WBM
 DT := DelphiTime;            // swap

 x := (DT - Trunc(DT)) * MsecPerDay;
 result := round(X);
end;
Tue, Jul 19 2016 4:11 PMPermanent Link

thomh

Thanks, Walter.

There is something funky going on though.

I have a little class which contains a DateTime property.
I set this property to today's date using User.DateOfBirth = Date.

I use TWriter to write a JSON string of this class and User.DateOfBirth gets converted to 1468879200000.

If I pass this value to your UNIXTimeToDateTime I get the date 2016.07.18 which was yesterday.

// Thom
Tue, Jul 19 2016 4:22 PMPermanent Link

thomh

That's weird.

When I code it in Delphi:

LDate := Date;

LDateInt := DateTimeToUNIXTime(LDate);
returns 1468886400000 compared to 1468879200000 in EWB.

And LDate := UNIXTimeToDateTime(LDateInt)
does return the correct date (2016.07.19)

So EWB seems to subtract one day when writing to a DateTime to JSON.

// Thom
Tue, Jul 19 2016 4:26 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Thom,

<< So EWB seems to subtract one day when writing to a DateTime to JSON. >>

Where is the date originating from ?  You have to remember to localize DateTime values, if so desired.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Jul 19 2016 4:31 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Tue, Jul 19 2016 4:40 PMPermanent Link

thomh

Hi Tim,

What do you mean "localize DateTime values" ?

// Thom
Tue, Jul 19 2016 5:29 PMPermanent Link

thomh

And I am using RemObjects and not the EWB Web Server.

// Thom
Tue, Jul 19 2016 5:37 PMPermanent Link

thomh

And since I using TWriter to convert Date() to JSON, the problem seems to be here:

procedure TWriter.DateTimeValue(Value: DateTime);
begin
  case FDateTimeFormat of
     dtfISO8601:
        Append(QuotedStr(DateTimeToISOStr(Value),DOUBLE_QUOTE));
     else
        Append(IntToStr(Integer(Value)));
     end;
end;


// Thom
Tue, Jul 19 2016 8:52 PMPermanent Link

erickengelke

>thomh wrote:

>And since I using TWriter to convert Date() to JSON, the problem seems to be here:
>
> Append(QuotedStr(DateTimeToISOStr(Value),DOUBLE_QUOTE));

The EWB time functions have an optional  USEUTC parameter except DateTimeToISOStr() which I believe always converts to UTC time, which we old farts call Grenich mean time..
Page 1 of 2Next Page
Jump to Page:  1 2
Image