Recent

Author Topic: [Solved] Doubt about DayOfWeek 's return  (Read 405 times)

devEric69

  • Full Member
  • ***
  • Posts: 118
[Solved] Doubt about DayOfWeek 's return
« on: August 19, 2019, 07:27:34 pm »
Hello,

I have a doubt about the result of the DayOfWeek function (unit sysutil \ rtl).

According to the Wiki ( https://www.freepascal.org/docs-html/rtl/sysutils/dayofweek.html ), the day "num# 1" of a week is Sunday.

However, in the attached program, today - Monday 19 August 2019 - is this day "number 1" of this week!!?

So, which is the reference, and who needs to be corrected: the coded function as it is, or the Wiki?

Regards.
« Last Edit: August 20, 2019, 10:30:44 am by devEric69 »
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

ASerge

  • Hero Member
  • *****
  • Posts: 1388
Re: Doubt about DayOfWeek 's return
« Reply #1 on: August 19, 2019, 07:57:34 pm »
Very long example, find the mistake yourself. Here is a simple example:
Code: Pascal  [Select]
  1. {$APPTYPE CONSOLE}
  2. uses SysUtils;
  3.  
  4. begin
  5.   Writeln(DayOfWeek(EncodeDate(2019, 08, 19))); // 2
  6.   Readln;
  7. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 8664
Re: Doubt about DayOfWeek 's return
« Reply #2 on: August 19, 2019, 09:28:43 pm »
https://en.wikipedia.org/wiki/Week

Usually DayOfTheWeek for Monday should return one.
Most people that want to use threading should learn to patch their jeans first: use a needle.

winni

  • Full Member
  • ***
  • Posts: 170
Re: Doubt about DayOfWeek 's return
« Reply #3 on: August 19, 2019, 10:26:55 pm »
Fight with the gods but don't fight with ISO!

Since 1976 Monday is Number 1.

In Delphi they accepted 0 AND 7  as input for Sunday.
Don't know about LCL.

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 1982
Re: Doubt about DayOfWeek 's return
« Reply #4 on: August 19, 2019, 10:56:40 pm »
I made this small test:

Code: Pascal  [Select]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   ADate: TDateTime;
  4.   AStr: String;
  5. begin
  6.   ADate := Now;
  7.   repeat
  8.     AStr := FormatDateTime('dddddd', ADate)
  9.             + ' = '
  10.             + DayOfTheWeek(ADate).ToString;
  11.     ListBox1.Items.Add(AStr);
  12.     ADate := IncDay(ADate, 1);
  13.   until DayOfTheWeek(ADate) = 1;
  14.   Clipboard.AsText := ListBox1.Items.Text;
  15. end;

The result is quite concluyent:

Code: [Select]
19 August 2019 = 1
20 August 2019 = 2
21 August 2019 = 3
22 August 2019 = 4
23 August 2019 = 5
24 August 2019 = 6
25 August 2019 = 7

which means the wiki is wrong.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 6134
Re: Doubt about DayOfWeek 's return
« Reply #5 on: August 19, 2019, 11:06:02 pm »
I made this small test:

Code: Pascal  [Select]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   ADate: TDateTime;
  4.   AStr: String;
  5. begin
  6.   ADate := Now;
  7.   repeat
  8.     AStr := FormatDateTime('dddddd', ADate)
  9.             + ' = '
  10.             + DayOfTheWeek(ADate).ToString;
  11.     ListBox1.Items.Add(AStr);
  12.     ADate := IncDay(ADate, 1);
  13.   until DayOfTheWeek(ADate) = 1;
  14.   Clipboard.AsText := ListBox1.Items.Text;
  15. end;

The result is quite concluyent:

Code: [Select]
19 August 2019 = 1
20 August 2019 = 2
21 August 2019 = 3
22 August 2019 = 4
23 August 2019 = 5
24 August 2019 = 6
25 August 2019 = 7

which means the wiki is wrong.
The wiki is correct. There are two functions named very similarly: DayOfWeek and DayOfTheWeek

DayOfWeek is the old Delphi function which matches the day names in the FormatSettings: 1 = Sunday, .., 7 = Saturday (https://www.freepascal.org/docs-html/rtl/sysutils/dayofweek.html, http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_DayOfWeek.html)
Code: Pascal  [Select]
  1. type
  2.   TWeekNameArray = array[1..7] of string;
  3.  
  4.   TFormatSettings = record
  5.     [...]
  6.     ShortDayNames: TWeekNameArray;
  7.     LongDayNames: TWeekNameArray;
  8.   end;
  9.  
  10. var
  11.   DefaultFormatSettings : TFormatSettings = (
  12.   [...]
  13.     ShortDayNames: ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  14.     LongDayNames:  ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  15.   );
               
DayOfTheWeek is the ISO-conformal function where the week begins with Monday: 1 = Monday, 7 = Sunday (http://docwiki.embarcadero.com/Libraries/Rio/en/System.DateUtils.DayOfTheWeek)
« Last Edit: August 19, 2019, 11:09:38 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

lucamar

  • Hero Member
  • *****
  • Posts: 1982
Re: Doubt about DayOfWeek 's return
« Reply #6 on: August 20, 2019, 12:18:49 am »
The wiki is correct. There are two functions named very similarly: DayOfWeek and DayOfTheWeek

Ouch! Didn't notice the extra "The". Meaningless test, then. Damit :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

winni

  • Full Member
  • ***
  • Posts: 170
Re: Doubt about DayOfWeek 's return
« Reply #7 on: August 20, 2019, 01:50:06 am »
Yes, another time a confusing Delphi.

First there was DayOfWeek.
To get it on ISO Standard everybody made

Code: Pascal  [Select]
  1. MyDayOfWeek := DayOfWeek(..) -1;
  2.  

With the result that sunday was  zero. That was default code in very early delphi times.

And that was the reason, why they later accepted both - zero and seven - as input for the Day of Week.

Later came DayOfTheWeek (ISO), but the old code was out in the wild.

And: The internet wasn't invented then; communication was not that easy.

Winni

devEric69

  • Full Member
  • ***
  • Posts: 118
Re: Doubt about DayOfWeek 's return
« Reply #8 on: August 20, 2019, 10:30:00 am »
Okay, thanks to ASerge, and everyone: it was indeed a carelessness on my part. I was coding a function, looking at the online help of another function (whose name was close) :-[ .

Code: Pascal  [Select]
  1. procedure TfrmMain.btnGetSeveralDaysOfWeekClick(Sender: TObject);
  2. var
  3.   sRawDateTime, sDateTime: string;
  4.   dtTheDay: TDateTime;
  5.   recFormatSettings: TFormatSettings;
  6. begin
  7.   with recFormatSettings do begin
  8.     DateSeparator:='/';
  9.     TimeSeparator:=':';
  10.     ShortDateFormat:='yyyymmdd';
  11.     LongTimeFormat:='hhmmss';
  12.   end;
  13.   sRawDateTime:= '20190819 173000';
  14.   sDateTime:= GetInsertedDateTimeWithSep(sRawDateTime, recFormatSettings );
  15.   dtTheDay:= StrToDateTime( sDateTime, recFormatSettings );
  16.   lblDaysOfWeek.Caption:= '1°) DayOfWeek:'+#13#10+''''+
  17.                           sRawDateTime+''' ('+sDateTime+'|DayOfWeek(EncodeDateTime('+datetimeToStr(dtTheDay)+
  18.                           '))--> DayOfWeek='+IntToStr( DayOfWeek (dtTheDay) )+'; ';
  19.  
  20.   lblDaysOfWeek.Caption:= lblDaysOfWeek.Caption+#13#10+
  21.                           'DayOfWeek(EncodeDateTime())--> DayOfWeek='+IntToStr( DayOfWeek (EncodeDateTime(2019, 08, 19, 00, 00, 00, 00)));
  22.  
  23.   lblDaysOfWeek.Caption:= lblDaysOfWeek.Caption+#13#10+#13#10+#13#10+
  24.                           '2°) DayOfTheWeek:'+#13#10+''''+
  25.                           sRawDateTime+''' ('+sDateTime+'|DayOfTheWeek(EncodeDateTime('+datetimeToStr(dtTheDay)+
  26.                           '))--> DayOfTheWeek='+IntToStr( DayOfTheWeek (dtTheDay) )+'; ';
  27.  
  28.   lblDaysOfWeek.Caption:= lblDaysOfWeek.Caption+#13#10+
  29.                           'DayOfTheWeek(EncodeDateTime())--> DayOfTheWeek='+IntToStr( DayOfTheWeek (EncodeDateTime(2019, 08, 19, 00, 00, 00, 00)));
  30. end;
  31.  
  32. function TfrmMain.GetInsertedDateTimeWithSep(sDateTime: string; const aFormatSettings: TFormatSettings): string;
  33. var
  34.   sDateSep: Char{ == '/'};
  35.   sTimeSep: Char{ == ':'};
  36. begin
  37.   sDateSep:= aFormatSettings.DateSeparator;
  38.   sTimeSep:= aFormatSettings.TimeSeparator;
  39.   Result:= Copy(sDateTime, 1, 4) + sDateSep + Copy(sDateTime, 5, 2) + sDateSep + Copy(sDateTime, 7, 2)  // date part
  40.             + ' ' +
  41.            Copy(sDateTime, 10, 2) + sTimeSep + Copy(sDateTime, 12, 2) + sTimeSep + Copy(sDateTime, 14, 2);  // time part
  42. end;

The "The" that differentiates DayOfWeek and DayOfTheWeek is not just there to "look pretty" ;) .
use: Ubuntu 18.04 + Laz. 1.8.5 + FPC 3.0.5 (64 bits).

wp

  • Hero Member
  • *****
  • Posts: 6134
Re: Doubt about DayOfWeek 's return
« Reply #9 on: August 20, 2019, 10:31:54 am »
I would not blame Delphi for this inconsistency. It is because much of the older software originates from the USA where the first day of the week is Sunday.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

winni

  • Full Member
  • ***
  • Posts: 170
Re: [Solved] Doubt about DayOfWeek 's return
« Reply #10 on: August 22, 2019, 12:15:29 am »
I don't blame Delphi. Sunday as first day of the week is old christian tradition. Germany abolished it at in 1975 (DIN), a lot of the rest of  world in 1976 (ISO), only the USA resists to change it.

It's the problem of the USA that they don't  care about the rest of the world. See ASCII, see miles & feets, see Farenheit, and, and

Winni