Jump to content
markus_ja

EncodeDate Rounding issue

Recommended Posts

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.org/egrange/dwscript/issues/65/encodedate-rounding-issue

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×