Weekday constant for DayOfWeek()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Weekday constant for DayOfWeek()

Post 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
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Weekday constant for DayOfWeek()

Post 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
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: Weekday constant for DayOfWeek()

Post 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?
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Weekday constant for DayOfWeek()

Post 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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Weekday constant for DayOfWeek()

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Weekday constant for DayOfWeek()

Post 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.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Weekday constant for DayOfWeek()

Post 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.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Weekday constant for DayOfWeek()

Post 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
Last edited by Demivec on Tue Jan 29, 2013 11:19 pm, edited 1 time in total.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Weekday constant for DayOfWeek()

Post 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. :-)
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Weekday constant for DayOfWeek()

Post 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. ;)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Weekday constant for DayOfWeek()

Post 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:
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Weekday constant for DayOfWeek()

Post 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
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Weekday constant for DayOfWeek()

Post 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?
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Weekday constant for DayOfWeek()

Post 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.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply