[solved]getting the day of the week

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

[solved]getting the day of the week

Post 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
Last edited by Pyrofan1 on Sun Dec 16, 2007 5:51 pm, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

void
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post by Pyrofan1 »

week_day is a global variable
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: getting the day of the week

Post 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[]
My OS is Perception.
Pyrofan1
Member
Member
Posts: 234
Joined: Sun Apr 29, 2007 1:13 am

Post 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"};
Post Reply