Page 1 of 1

[solved]getting the day of the week

Posted: Sun Dec 16, 2007 12:34 pm
by Pyrofan1
i'm trying to figure out the day of the week using the information for this page http://en.wikipedia.org/wiki/Calculatin ... f_the_week
here's my code

Code: Select all

unsigned char days_table[]={0,1,2,3,4,5,6};
unsigned char months_table[]={0,0,3,3,6,1,4,6,2,5,0,3,5};
unsigned char months_table_leap[]={0,6,2,3,6,1,4,6,2,5,0,3,5};

void get_week_day(void)
{
              unsigned short num;

              num+=year;
              num+=year/4;

              if(year%4==0)
              {
                      num+=months_table_leap[month];
              }
              else
              {
                      num+=months_table[month];
              }

              num+=day;
              num=num%7;
              week_day=(unsigned char)num;
}
this code always returns 0 instead of the right number

Posted: Sun Dec 16, 2007 3:45 pm
by Combuster
void

Posted: Sun Dec 16, 2007 5:01 pm
by Pyrofan1
week_day is a global variable

Re: getting the day of the week

Posted: Sun Dec 16, 2007 5:06 pm
by AndrewAPrice

Code: Select all

unsigned char months_table[]={0,0,3,3,6,1,4,6,2,5,0,3,5};
unsigned char months_table_leap[]={0,6,2,3,6,1,4,6,2,5,0,3,5};

int get_week_day(void)
{
              unsigned short num;

              num+=year;
              num+=year/4;

              if(year%4==0)
              {
                      num+=months_table_leap[month];
              }
              else
              {
                      num+=months_table[month];
              }

              num+=day;
              num=num%7;
              week_day=(unsigned char)num;
              return week_day;
}
Please explain your reasoning behind including the code:
unsigned char days_table[]

Posted: Sun Dec 16, 2007 5:51 pm
by Pyrofan1
fixed it
my code looked like

Code: Select all

char *days[]={
"Nothing",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
instead of

Code: Select all

char *days[]={
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};