Page 1 of 1

Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 7:21 pm
by uwekel
Hi,

i would like to have constants for the DayOfWeek() function. Something like this:

Code: Select all

Enumeration
  #Sunday
  #Monday
  #Tuesday
  #Wednesday
  #Thursday
  #Friday
  #Saturday
EndEnumeration
Best regards
Uwe

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 8:01 pm
by Little John
uwekel wrote:i would like to have constants for the DayOfWeek() function. Something like this:

Code: Select all

Enumeration
  #Sunday
  #Monday
  #Tuesday
  #Wednesday
  #Thursday
  #Friday
  #Saturday
EndEnumeration
No, please!

According to the respective ISO norm from 1973(!), Monday is the first and Sunday is the last day of the week. So Sunday must equal 7, not 0 (or Monday=0 .... Sunday=6).

Unfortunately, PB's built-in weekday function does not work according to that norm. However, it's easy to write some code ourselves e.g. like this:

Code: Select all

Enumeration 1
   #Monday
   #Tuesday
   #Wednesday
   #Thursday
   #Friday
   #Saturday
   #Sunday
EndEnumeration

Procedure IsoDayOfWeek (d)
   Protected w = DayOfWeek(d)
   If w = 0
      w = 7
   EndIf
   ProcedureReturn w
EndProcedure

If IsoDayOfWeek(d) = #Sunday
   ; [...]
EndIf
If PB would have built-in wrong constants, the situation would become unnecessarily more confusing.

Regards, Little John

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 8:46 pm
by uwekel
For me it is not that important to be ISO conform. My proposal is just according to PB's DayOfWeek() function. But even if Fred would make it like ISO, it is a trifle to adapt the source, isn't it?

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 9:13 pm
by Little John
uwekel wrote:My proposal is just according to PB's DayOfWeek() function.
Yes, of course.

It seems to me that you do not realize the dilemma:
If there were built-in constants which were ISO conform, then those constants were not conform to PB's DayOfWeek() function. On the other hand, if there were built-in constants which were conform to PB's DayOfWeek() function, then those constants were not ISO conform. And having no built-in constants at all is better than built-in constants that are not ISO conform.
uwekel wrote:For me it is not that important to be ISO conform.
Then just write an Enumeration like the one in your first post above, and all is fine.
And people like me who like to stick to standards can write code like the one I wrote in my first post above, and there will be no conflict with any wrong built-in constants ("wrong" from the point of view of everyone who is interested in sticking to standards).

In this situation, adding any built-in constants for the weekday will be a disimprovement.

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 9:24 pm
by ts-soft
What deals DayOfWeek with the first day of the week?
It is completely irrelevant which internal number is used there.

The enumeration should have the normal prefix for PB Constants:

Code: Select all

Enumeration
  #PB_Sunday
  #PB_Monday
  #PB_Tuesday
  #PB_Wednesday
  #PB_Thursday
  #PB_Friday
  #PB_Saturday
EndEnumeration

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 10:15 pm
by Little John
ts-soft wrote:It is completely irrelevant which internal number is used there.
This sentence is completely wrong.
If you don't know anything about ordinal numbers: no problem, but then better don't talk about them.

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 10:26 pm
by ts-soft
The ISO 8601 says only, Monday is the first Day of the week, but not Monday is numbered to 0 or 1.

In PB stays 0 for Sunday and not for FirstDayOfWeek.

Re: Weekday constant for DayOfWeek()

Posted: Mon Jan 28, 2013 10:54 pm
by Demivec
Little John wrote:In this situation, adding any built-in constants for the weekday will be a disimprovement.
It is simple enough to wrap whatever the constants are, and it would make more sense than "If w = 7: w = 0: EndIf".

Code: Select all

; ;proposed pre-defined constants
; Enumeration
  ; #PB_Sunday
  ; #PB_Monday
  ; #PB_Tuesday
  ; #PB_Wednesday
  ; #PB_Thursday
  ; #PB_Friday
  ; #PB_Saturday
; EndEnumeration

Enumeration 
  #ISO_Monday = 1
  #ISO_Tuesday
  #ISO_Wednesday
  #ISO_Thursday
  #ISO_Friday
  #ISO_Saturday
  #ISO_Sunday
EndEnumeration

Procedure IsoDayOfWeek (d)
  Protected w
  Select DayOfWeek(d)
    Case #PB_Sunday
      w = #ISO_Sunday
    Case #PB_Monday
      w = #ISO_Monday
    Case #PB_Tuesday
      w = #ISO_Tuesday
    Case #PB_Wednesday
      w = #ISO_Wednesday
    Case #PB_Thursday
      w = #ISO_Thursday
    Case #PB_Friday
      w = #ISO_Friday
    Case #PB_Saturday
      w = #ISO_Saturday
  EndSelect

  ProcedureReturn w
EndProcedure

If IsoDayOfWeek(d) = #ISO_Sunday
  ; [...]
EndIf

Re: Weekday constant for DayOfWeek()

Posted: Tue Jan 29, 2013 12:22 am
by Little John
ts-soft wrote:The ISO 8601 says only, Monday is the first Day of the week, but not Monday is numbered to 0 or 1.

In PB stays 0 for Sunday and not for FirstDayOfWeek.
It is a matter of course, that the number of the first day of the week has to be smaller than the number of the second day of the week. And the number of the second day of the week has to be smaller than the number of the third day of the week etc. Thus, if Monday is the first day of the week, you obviously can not have Monday=1, Tuesday=2 ... Sunday=0.
Demivec wrote:
Little John wrote:In this situation, adding any built-in constants for the weekday will be a disimprovement.
It is simple enough to wrap whatever the constants are, and it would make more since then "If w = 7: w = 0: EndIf".
IMHO it's simpler and less confusing just to let it as is. :-)

Re: Weekday constant for DayOfWeek()

Posted: Tue Jan 29, 2013 12:58 am
by MachineCode
uwekel wrote:i would like to have constants for the DayOfWeek() function.
+1. All the other discussion is just silly. We just want constants to match the values in the Help file. ;)

Re: Weekday constant for DayOfWeek()

Posted: Tue Jan 29, 2013 1:50 am
by BorisTheOld
Little John wrote:It is a matter of course, that the number of the first day of the week has to be smaller than the number of the second day of the week. And the number of the second day of the week has to be smaller than the number of the third day of the week etc. Thus, if Monday is the first day of the week, you obviously can not have Monday=1, Tuesday=2 ... Sunday=0.
When my oldest son was 4 years old he asked, "What day came before the first Monday?", and was only satisfied after we had spent an hour or so discussing various mathematical concepts. Ultimately, he went on to take a degree in mathematics.

I think my 4 year old son might have argued that if Monday = 1, then there's a good case to be made for assigning Sunday = 0. :mrgreen:

Re: Weekday constant for DayOfWeek()

Posted: Tue Jan 29, 2013 7:30 am
by Little John
BorisTheOld wrote:I think my 4 year old son might have argued that if Monday = 1, then there's a good case to be made for assigning Sunday = 0. :mrgreen:
I'm so glad that we have talked about it. :D

Re: Weekday constant for DayOfWeek()

Posted: Tue Jan 29, 2013 10:30 pm
by RichAlgeni
Forgive me, but I feel compelled to contribute, if you can call it that.

Aren't some of these requests getting a bit silly? I don't mean to call you out Uwe, but didn't you accomplish exactly what you requested in your example?

I'm not trying to tell anyone else what to do, but I have a base include program (.pbi) which contains the little extras that I like to use often, like the above constant enumeration.

Maybe something like that would come in handy for others?

Re: Weekday constant for DayOfWeek()

Posted: Wed Jan 30, 2013 12:32 am
by MachineCode
He did accomplish it, yes; but the reality is that return values from PureBasic's functions, when possible, should have a constant rather than just number, for better readability. Most functions do this already, but DayOfWeek() doesn't.