Jump to content


Photo

Different Dates on v2.2

datetime Iso8601ToDateTime

  • Please log in to reply
3 replies to this topic

#1 Laksekjønn

Laksekjønn
  • Members
  • 418 posts

Posted 19 December 2015 - 04:04 PM

When I click at W3Button2, depending on the SmartMS version, I get different results:

 

WriteLn(D); // using ver 2.1.2.3592

 D= 41818.493310185186 --> 6/28/14 11:50:22

 

WriteLn(D); // using  ver 2.2.0.4093

D= 41818.305810185186 --> 6/28/14 7:20:22  --> this is incorrect

function TryEncodeDate(Y,M,D: integer; var Value: TDateTime): boolean;
begin
  try
    Value := EncodeDate(Y,M,D);
    result := true
  except
    result := false;
  end;
end;

function TryEncodeTime(HH,MM,SS,MS: integer; var Value: TDateTime): boolean;
begin
  try
    Value := EncodeTime(HH,MM,SS,MS);
    result := true
  except
    result := false;
  end;
end;

function Iso8601ToDateTime(const Value: string): TDateTime;
var Y,M,D, HH,MI,SS: integer;
begin //  YYYY-MM-DD   Thh:mm:ss  or  YYYY-MM-DDThh:mm:ss
      //  1234567890   123456789      1234567890123456789
  result := 0;
  case Length(Value) of
  9: if (Value[1]='T') and (Value[4]=':') and (Value[7]=':') then begin
    HH := ord(Value[2])*10+ord(Value[3])-(48+480);
    MI := ord(Value[5])*10+ord(Value[6])-(48+480);
    SS := ord(Value[8])*10+ord(Value[9])-(48+480);
    TryEncodeTime(HH,MI,SS,0,result);
  end;
  10: if (Value[5]=Value[8]) and (ord(Value[8]) in [ord('-'),ord('/')]) then begin
    Y := ord(Value[1])*1000+ord(Value[2])*100+
         ord(Value[3])*10+ord(Value[4])-(48+480+4800+48000);
    M := ord(Value[6])*10+ord(Value[7])-(48+480);
    D := ord(Value[9])*10+ord(Value[10])-(48+480);
    TryEncodeDate(Y,M,D,result);
  end;
  19: if (Value[5]=Value[8]) and (ord(Value[8]) in [ord('-'),ord('/')]) and
         (ord(Value[11]) in [ord(' '),ord('T')]) and (Value[14]=':') and (Value[17]=':') then begin
    Y := ord(Value[1])*1000+ord(Value[2])*100+
         ord(Value[3])*10+ord(Value[4])-(48+480+4800+48000);
    M := ord(Value[6])*10+ord(Value[7])-(48+480);
    D := ord(Value[9])*10+ord(Value[10])-(48+480);
    HH := ord(Value[12])*10+ord(Value[13])-(48+480);
    MI := ord(Value[15])*10+ord(Value[16])-(48+480);
    SS := ord(Value[18])*10+ord(Value[19])-(48+480);
    if (Y<=9999) and ((M-1)<12) and ((D-1)<31) and
       (HH<24) and (MI<60) and (SS<60) then
      result := EncodeDate(Y,M,D)+EncodeTime(HH,MI,SS,0);
  end;
  end;
end;

procedure TLoginForm.W3Button2Click(Sender: TObject);
var D: TDateTime;
    i: integer;
    s,x: string;
    T: TTimeLog;
begin
  s := '2014-06-28T11:50:22';
  D := Iso8601ToDateTime(s);
  WriteLn(D);
  // ver2.1.2.3592 D= 41818.493310185186 --> 6/28/14 11:50:22
  // ver2.2.0.4093 D= 41818.305810185186 --> 6/28/14 7:20:22
end;


#2 Laksekjønn

Laksekjønn
  • Members
  • 418 posts

Posted 21 December 2015 - 12:51 AM

I'm awfuly sorry, in version 2.2, you have to add the "System.DateUtils" unit to work like previous version.

------------------------------------------------------------------------------

 

Another minor issue is related to FormatDateTime function.

I successfully can compile functions that uses FormatDateTime in ver2.1 and v2.2 but always get different results.

function DateTimeToIso8601(Value: TDateTime): string;
begin // e.g. YYYY-MM-DD Thh:mm:ss or YYYY-MM-DDThh:mm:ss
  if Value<=0 then
    result := '' else
  if frac(Value)=0 then
    result := FormatDateTime('yyyy-mm-dd',Value) else
  if trunc(Value)=0 then
    result := FormatDateTime('Thh:nn:ss',Value) else
    result := FormatDateTime('yyyy-mm-ddThh:nn:ss',Value);
end;

I've discovered that smart team have changed this function since 2.1

FormatDateTime(frm: String; dt:Float) : String;  --> ver 2.1

FormatDateTime(frm: String; dt:Float; utc:DateTimeZone=0)  : String;  --> ver 2.2

 

Suggestion: is overloading this function

I think would be better if the 3rd parameter default were set to DateTimeZone.UTC instead of 0.

 

FormatDateTime(frm: String; dt:Float; utc:DateTimeZone=DateTimeZone.UTC)  : String;

 

 



#3 ielite

ielite
  • Members
  • 691 posts

Posted 01 January 2016 - 04:42 PM

JS DateTime Under Delphi (and SMS)

 

https://jonlennartaa...ss.com/2016/01/



#4 EvaF

EvaF
  • Members
  • 6 posts

Posted 23 June 2017 - 04:48 PM

procedure TLoginForm.W3Button2Click(Sender: TObject);
var D: TDateTime;
i: integer;
s,x: string;
T: TTimeLog;
begin
s := '2014-06-28T11:50:22';
D := Iso8601ToDateTime(s);
WriteLn(D);
// ver2.1.2.3592 D= 41818.493310185186 --> 6/28/14 11:50:22
// ver2.2.0.4093 D= 41818.305810185186 --> 6/28/14 7:20:22
end;

I met the similiar troubles ( when I use TimeStampOffset).

 

There is option  to override the function TryEncodeDate and FormatDateTime

 

 or

I noticed in generated code the variable $TZ (=timezone?)

function EncodeDate(y,m,d,u) { return ( (u||$TZ)===2 ? Date.UTC(y,m-1,d) : new Date(y,m-1,d).getTime() )/864e5+25569 }

I haven't find anywhere how to set it, but if UTC is required there is an simple work around

procedure TLoginForm.W3Button2Click(Sender: TObject);
var D: TDateTime;
i: integer;
s,x: string;
T: TTimeLog;
begin
s := '2014-06-28T11:50:22';
//---------- the change of $TZ--------------------
asm
  var tempTZ = eval("$TZ");
  eval("$TZ = 2");
end;
//------------------------------
D := Iso8601ToDateTime(s);
//----------- back -------------------
asm
  eval("$TZ = tempTZ");
end;
//------------------------------
WriteLn(D);
// ver2.1.2.3592 D= 41818.493310185186 --> 6/28/14 11:50:22
// ver2.2.0.4093 D= 41818.305810185186 --> 6/28/14 7:20:22
end;



  • Nico Wouterse likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users