Day of week function

Hello Guys,

Sorry if this has been answered before, but the search facility is still really really $%^£.

Most languages have a function that returns the day of the week built in. I really Want a function that takes a date and returns the day of the week  either 0-7 or "Sunday" - "Saturday" strange there isn't one, odd I haven't needed one in dxl before really. Fairly simple to build I guess just need to figure out what day it was on January 1st 1970 and mess around with months, years and leap years, rather not have to bother if someone has one in their back pocket, or more likely there is one buried in the "new improved" forum which still has no acceptable search facility.

Richard


Richard_Good - Tue Apr 30 07:17:29 EDT 2013

Re: Day of week function
Richard_Good - Tue Apr 30 07:28:28 EDT 2013

Just about to fail the IQ test, when I found this from Reik Schroeder I think.

 

const int DATE_INC_SECONDS_PER_DAY = 60*60*24

int dayOfWeek(Date d)

{

return ((intof(d) / DATE_INC_SECONDS_PER_DAY) +4) %7

}

Re: Day of week function
llandale - Tue Apr 30 14:09:46 EDT 2013

Richard_Good - Tue Apr 30 07:28:28 EDT 2013

Just about to fail the IQ test, when I found this from Reik Schroeder I think.

 

const int DATE_INC_SECONDS_PER_DAY = 60*60*24

int dayOfWeek(Date d)

{

return ((intof(d) / DATE_INC_SECONDS_PER_DAY) +4) %7

}

Good chance mine came from that; but you could do more error checking:

const int    cl_SecsInDay  = 60*60*24  // # seconds in a day
const string cl_DaysOfTheWeek[] =
   {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

//***************
int fDayOfWeek(Date dat)
{ // Return the day of the week integer, Sunday is number zero.
 //  Not sure how this works, but it appears that DOORS originate its
 // dates on the 4th day of the week, Wednesday.
 if (null dat)    return(0)
 if (dat == cl_datNull)   return(0) // Looks like "(not defined)"
 return(((intOf(dat) / cl_SecsInDay) + 4) % 7)
} // end fDayOfWeek(int)

//----------------------------
string fDayOfWeek(Date dat)
{ // overloaded, return the string day e.g. 'Sunday'.
 if (null dat)    return("")
 if (dat == cl_datNull)   return("") // Looks like "(not defined)"
 return(cl_DaysOfTheWeek[fDayOfWeek(dat)])
} // end fDayOfWeek(string)

-Louie

Wonder how this will look when posted.

Re: Day of week function
Tony_Goodman - Wed May 01 04:08:51 EDT 2013

llandale - Tue Apr 30 14:09:46 EDT 2013

Good chance mine came from that; but you could do more error checking:

const int    cl_SecsInDay  = 60*60*24  // # seconds in a day
const string cl_DaysOfTheWeek[] =
   {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

//***************
int fDayOfWeek(Date dat)
{ // Return the day of the week integer, Sunday is number zero.
 //  Not sure how this works, but it appears that DOORS originate its
 // dates on the 4th day of the week, Wednesday.
 if (null dat)    return(0)
 if (dat == cl_datNull)   return(0) // Looks like "(not defined)"
 return(((intOf(dat) / cl_SecsInDay) + 4) % 7)
} // end fDayOfWeek(int)

//----------------------------
string fDayOfWeek(Date dat)
{ // overloaded, return the string day e.g. 'Sunday'.
 if (null dat)    return("")
 if (dat == cl_datNull)   return("") // Looks like "(not defined)"
 return(cl_DaysOfTheWeek[fDayOfWeek(dat)])
} // end fDayOfWeek(string)

-Louie

Wonder how this will look when posted.

Another example.

This one does not assume that the world began on 1 January 1970.

I was born on a Wednesday which suggests I am full of woe :-(

 

 /***********************************
dayOfWeek
 
Returns the day of the week for any given date.
Based on Sakamoto's algorithm.
***********************************/
string dayOfWeek(Date theDate)
{
const int MONTH_TABLE[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
const string DAYS[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
 
int d = 0
    int m = 0
    int y = 0
int day = 0
 
string dateString = stringOf(theDate, "ddMMyyyy")
 
d = intOf(dateString[0:1])
m = intOf(dateString[2:3])
y = intOf(dateString[4:7])
 
if (m < 3)
{
y -= 1
}
 
day = (y + y/4 - y/100 + y/400 + MONTH_TABLE[m-1] + d) % 7
 
return(DAYS[day])
}

 

 

Re: Day of week function
Wolfgang Uhr - Wed May 01 07:55:46 EDT 2013

Tony_Goodman - Wed May 01 04:08:51 EDT 2013

Another example.

This one does not assume that the world began on 1 January 1970.

I was born on a Wednesday which suggests I am full of woe :-(

 

 /***********************************
dayOfWeek
 
Returns the day of the week for any given date.
Based on Sakamoto's algorithm.
***********************************/
string dayOfWeek(Date theDate)
{
const int MONTH_TABLE[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
const string DAYS[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
 
int d = 0
    int m = 0
    int y = 0
int day = 0
 
string dateString = stringOf(theDate, "ddMMyyyy")
 
d = intOf(dateString[0:1])
m = intOf(dateString[2:3])
y = intOf(dateString[4:7])
 
if (m < 3)
{
y -= 1
}
 
day = (y + y/4 - y/100 + y/400 + MONTH_TABLE[m-1] + d) % 7
 
return(DAYS[day])
}

 

 

We shall start a small coding battle with the categories

 

  1. Longest Code
  2. Shortest Code
  3. Most complicated code
  4. Most interesting comments ...

Re: Day of week function
nm3210 - Wed May 01 09:35:47 EDT 2013

llandale - Tue Apr 30 14:09:46 EDT 2013

Good chance mine came from that; but you could do more error checking:

const int    cl_SecsInDay  = 60*60*24  // # seconds in a day
const string cl_DaysOfTheWeek[] =
   {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

//***************
int fDayOfWeek(Date dat)
{ // Return the day of the week integer, Sunday is number zero.
 //  Not sure how this works, but it appears that DOORS originate its
 // dates on the 4th day of the week, Wednesday.
 if (null dat)    return(0)
 if (dat == cl_datNull)   return(0) // Looks like "(not defined)"
 return(((intOf(dat) / cl_SecsInDay) + 4) % 7)
} // end fDayOfWeek(int)

//----------------------------
string fDayOfWeek(Date dat)
{ // overloaded, return the string day e.g. 'Sunday'.
 if (null dat)    return("")
 if (dat == cl_datNull)   return("") // Looks like "(not defined)"
 return(cl_DaysOfTheWeek[fDayOfWeek(dat)])
} // end fDayOfWeek(string)

-Louie

Wonder how this will look when posted.

Regarding why it's +4/Wed, it's because POSIX time is since midnight (UTC), 1 January 1970 which if you're in the minus hours of time zones (americas) was a wednesday.

Re: Day of week function
llandale - Wed May 01 11:06:32 EDT 2013

Tony_Goodman - Wed May 01 04:08:51 EDT 2013

Another example.

This one does not assume that the world began on 1 January 1970.

I was born on a Wednesday which suggests I am full of woe :-(

 

 /***********************************
dayOfWeek
 
Returns the day of the week for any given date.
Based on Sakamoto's algorithm.
***********************************/
string dayOfWeek(Date theDate)
{
const int MONTH_TABLE[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}
const string DAYS[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
 
int d = 0
    int m = 0
    int y = 0
int day = 0
 
string dateString = stringOf(theDate, "ddMMyyyy")
 
d = intOf(dateString[0:1])
m = intOf(dateString[2:3])
y = intOf(dateString[4:7])
 
if (m < 3)
{
y -= 1
}
 
day = (y + y/4 - y/100 + y/400 + MONTH_TABLE[m-1] + d) % 7
 
return(DAYS[day])
}

 

 

I saw that algorithm on the walls of a Mayan temple in Atlantis.

Seems to cleverly use leap-year calculation; but seems to be missing "-y/2000"; unless of course I'm full of donkey-calc.

I note that:

  • The beginning of time was indeed 1 January 1970; I read that in the DUG.
  • The beginning f time was not  1 January 1 AD; I read that in the Quran.
  • null input dates cause abortion.
  • MONTH_TABLE is the day of the week for each month in year 1?

-Louie

[http://en.wikipedia.org/wiki/Write-only_language]

Re: Day of week function
Tony_Goodman - Thu May 02 09:35:20 EDT 2013

Wolfgang Uhr - Wed May 01 07:55:46 EDT 2013

We shall start a small coding battle with the categories

 

  1. Longest Code
  2. Shortest Code
  3. Most complicated code
  4. Most interesting comments ...

I think we should also add a category for most laughable implementation.

I found this example (nothing to do with me) that demonstrates what you get when you pay monkeys to write your DXL.

 

 

//determines whether the day of archive is a Sunday
bool isSunday(){
  Date d="04 April 2010", e //4 April 2010 was a Sunday
  string Today
  real m, n, weeks, fractionweek, temp
  Today=stringOf(today()) //set today as a string to avoid unwanted seconds since start of day
  e=Today //convert the string back to date format
  m = realOf(intOf(d)) //real value of number of seconds since 1 Jan 1970 for Date variable d
  n = realOf(intOf(e)) //real value of number of seconds since 1 Jan 1970 for start of today
  weeks =(n-m)/3600.0/24.0/7.0 //number of weeks since date d and today (archive)
  temp = truncateReal(weeks) //truncate to determine integer of number of weeks since Date d
  fractionweek = weeks - temp //isolate the fractional part of weeks
  //when fraction is zero then archive day is a Sunday
  if (fractionweek<0.05){
return true // today is Sunday
}else{
return false // today is not Sunday
}
}

Re: Day of week function
Adamarla - Thu May 02 09:51:38 EDT 2013

Wolfgang Uhr - Wed May 01 07:55:46 EDT 2013

We shall start a small coding battle with the categories

 

  1. Longest Code
  2. Shortest Code
  3. Most complicated code
  4. Most interesting comments ...

I'll enter Battle 2 - Shortest Code:

print("Day: " (stringOf(today(), userLocale(), "dddd")) "\n")

Re: Day of week function
Tony_Goodman - Thu May 02 11:27:40 EDT 2013

Adamarla - Thu May 02 09:51:38 EDT 2013

I'll enter Battle 2 - Shortest Code:

print("Day: " (stringOf(today(), userLocale(), "dddd")) "\n")

I didn't know you could do that!

Thanks for sharing.

This also works for my birthday, which was before the beginning of the world.

Re: Day of week function
Wolfgang Uhr - Thu May 02 11:47:52 EDT 2013

Tony_Goodman - Thu May 02 09:35:20 EDT 2013

I think we should also add a category for most laughable implementation.

I found this example (nothing to do with me) that demonstrates what you get when you pay monkeys to write your DXL.

 

 

//determines whether the day of archive is a Sunday
bool isSunday(){
  Date d="04 April 2010", e //4 April 2010 was a Sunday
  string Today
  real m, n, weeks, fractionweek, temp
  Today=stringOf(today()) //set today as a string to avoid unwanted seconds since start of day
  e=Today //convert the string back to date format
  m = realOf(intOf(d)) //real value of number of seconds since 1 Jan 1970 for Date variable d
  n = realOf(intOf(e)) //real value of number of seconds since 1 Jan 1970 for start of today
  weeks =(n-m)/3600.0/24.0/7.0 //number of weeks since date d and today (archive)
  temp = truncateReal(weeks) //truncate to determine integer of number of weeks since Date d
  fractionweek = weeks - temp //isolate the fractional part of weeks
  //when fraction is zero then archive day is a Sunday
  if (fractionweek<0.05){
return true // today is Sunday
}else{
return false // today is not Sunday
}
}

I'm just thinking about the most interesting display format. Is it the post in the forum or the content of the mail?


Attachments

TheMessage.jpg

Re: Day of week function
llandale - Thu May 02 12:06:13 EDT 2013

Wolfgang Uhr - Thu May 02 11:47:52 EDT 2013

I'm just thinking about the most interesting display format. Is it the post in the forum or the content of the mail?

His ..err.. Tony's post looks OK except that white space is collapsed.  Your attachment seems to have single EOLs collapsed as well making it rather unreadable.

-Louie

EPSIDC didn't have any of these problems.... lol

Re: Day of week function
Wolfgang Uhr - Thu May 02 12:18:47 EDT 2013

llandale - Thu May 02 12:06:13 EDT 2013

His ..err.. Tony's post looks OK except that white space is collapsed.  Your attachment seems to have single EOLs collapsed as well making it rather unreadable.

-Louie

EPSIDC didn't have any of these problems.... lol

Ok that may be something in outlook. Sometimes outlook does such things. I'll see if I can find a parameter to change it.

 

 

Re: Day of week function
llandale - Thu May 02 12:30:37 EDT 2013

Tony_Goodman - Thu May 02 09:35:20 EDT 2013

I think we should also add a category for most laughable implementation.

I found this example (nothing to do with me) that demonstrates what you get when you pay monkeys to write your DXL.

 

 

//determines whether the day of archive is a Sunday
bool isSunday(){
  Date d="04 April 2010", e //4 April 2010 was a Sunday
  string Today
  real m, n, weeks, fractionweek, temp
  Today=stringOf(today()) //set today as a string to avoid unwanted seconds since start of day
  e=Today //convert the string back to date format
  m = realOf(intOf(d)) //real value of number of seconds since 1 Jan 1970 for Date variable d
  n = realOf(intOf(e)) //real value of number of seconds since 1 Jan 1970 for start of today
  weeks =(n-m)/3600.0/24.0/7.0 //number of weeks since date d and today (archive)
  temp = truncateReal(weeks) //truncate to determine integer of number of weeks since Date d
  fractionweek = weeks - temp //isolate the fractional part of weeks
  //when fraction is zero then archive day is a Sunday
  if (fractionweek<0.05){
return true // today is Sunday
}else{
return false // today is not Sunday
}
}

That "Monkey" at least had the decency to comment the code.  Compare that to the one two posts higher...   lol

The algorithm is quite sound; calculate the number of full days between the target date and one known to be a Sunday; and if it divides evenly by 7 then that target date was also a Sunday.  Yes, using the "%" (remainder) function instead of all those "real" numbers would certainly have been better. 

Whereas your algorithm presumes:

  • You know the precise leap-year calculation algorithm
  • That leap-year calculation extends 2013 years into the past
  • A "Sunday" is one which "should" have been a Sunday had the Intelectual-Monkeys in the old days implemented the Leap Year calculation before they did.

That means your algorithm is wrong ... in the sense that if you go back in time 900 years to a date your algorithm says is a "Sunday", you will probably find that nobody is going to church that day because they don't think it is Sunday.

-Louie

Re: Day of week function
Tony_Goodman - Fri May 03 06:05:50 EDT 2013

llandale - Thu May 02 12:06:13 EDT 2013

His ..err.. Tony's post looks OK except that white space is collapsed.  Your attachment seems to have single EOLs collapsed as well making it rather unreadable.

-Louie

EPSIDC didn't have any of these problems.... lol

My laughable code example was not mangled by the forum - it was written exactly as it appears!

 

Re: Day of week function
llandale - Fri May 03 11:06:21 EDT 2013

Tony_Goodman - Thu May 02 11:27:40 EDT 2013

I didn't know you could do that!

Thanks for sharing.

This also works for my birthday, which was before the beginning of the world.

AD not BC yes?   lol