Jump to content


Photo

EncodeDate Rounding issue


  • Please log in to reply
3 replies to this topic

#1 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 06 July 2016 - 04:49 PM

Hello,

 

Encoding a date returns wrong value (=rounding issue).

procedure Test;
var
  d, d2: TDate;
  isE: Boolean;
begin
  d := Date;
  d2 := EncodeDate(YearOf(d),MonthOf(d),DAyOf(d));

  isE := d=d2;
  WriteLn(Format('Equal: %s, d: %d, d2: %d', [isE.ToString,d,d2]));

end;

Result:

Equal: False, d: 42557, d2: 42556.91666666667  [line #352]

https://bitbucket.or...-rounding-issue


  • ielite likes this

#2 Laksekjønn

Laksekjønn
  • Members
  • 440 posts

Posted 06 July 2016 - 05:30 PM

a dirty trick would be

procedure Test;
var
  d, d2: TDate;
  isE: Boolean;
begin
  d := Date;
  d2 := EncodeDate(YearOf(d,DateTimeZone.UTC),MonthOf(d,DateTimeZone.UTC),DAyOf(d,DateTimeZone.UTC), DateTimeZone.UTC);
  isE := d=d2;
  WriteLn(Format('Equal: %s, d: %d, d2: %d', [isE.ToString,d,d2]));
end;

Equal: True, d: 42557, d2: 42557


  • ielite likes this

#3 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 06 July 2016 - 05:43 PM

Thanks! Interessting.

 

But the issue I encountered is actually in the DWS function StrToDate(), which I am using. I just saw that this function is using EncodeDate, and tried to isolate the issue.

 

Is there also a trick :)


  • ielite likes this

#4 Laksekjønn

Laksekjønn
  • Members
  • 440 posts

Posted 06 July 2016 - 10:22 PM

The functions date / time were refactored to support UTC/Local variants;

FormatSettings.ShortDateFormat := 'yyyy-mm-dd';

var dtl := StrToDate('2010-12-23', DateTimeZone.Local);
var dtu := StrToDate('2010-12-23', DateTimeZone.UTC);

WriteLn(DateToStr(dtl, DateTimeZone.Local)); //2010-12-23
WriteLn(DateToStr(dtu, DateTimeZone.UTC));  //2010-12-23

FormatSettings.ShortDateFormat := 'yy-m-d';

WriteLn(DateToStr(StrToDate('2013-09-30')));  // 13-9-30
WriteLn(DateToStr(StrToDate('2014-12-24')));  //14-12-24
WriteLn(DateToStr(StrToDate('2015-3-5')));    // 15-3-5





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users