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 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 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
//***************
//---------------------------- -Louie Wonder how this will look when posted. |
Re: Day of week function 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
//***************
//---------------------------- -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 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
|
Re: Day of week function 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
//***************
//---------------------------- -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 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:
-Louie |
Re: Day of week function Wolfgang Uhr - Wed May 01 07:55:46 EDT 2013 We shall start a small coding battle with the categories
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 Wolfgang Uhr - Wed May 01 07:55:46 EDT 2013 We shall start a small coding battle with the categories
I'll enter Battle 2 - Shortest Code: print("Day: " (stringOf(today(), userLocale(), "dddd")) "\n") |
Re: Day of week function 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 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 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 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 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:
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 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 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 |