Gabriel
 
 
  Joined: 03 Aug 2009 Posts: 1074 Location: Panama 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Weekday Calculator | 
			 
			
				 Posted: Sun Nov 05, 2017 12:43 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Hi All,
 
 
Just a quick Function I coded today based on this:
 
https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
 
 
Pretty self explanatory: call it with year*, month, day as arguments 
 
*(2017 = 17)
 
 
the function will return 1 Sunday, 2 Monday ...etc.
 
It will also print the Day Name to terminal.
 
 
 	  | Code: | 	 		  
 
int Weekday(int year,int month,int day)
 
{
 
    // Algorithm taken from: https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
 
 
   const int Month_Value[13]={0,1,4,4,0,2,5,0,3,6,1,4,6}; // Magic Numbers Yeih!
 
   int Temp_Day=0;
 
   
 
   Temp_Day=year/4;
 
   Temp_Day+=day;
 
   Temp_Day+=Month_Value[month];
 
   if((year%4)==0)
 
   {
 
      if((month==1)||(month==2))Temp_Day-=1;
 
   }
 
   Temp_Day+=6; //fixed value for 2000´s Only
 
   Temp_Day+=year;
 
   Temp_Day%=7;
 
   if(Temp_Day==0)Temp_Day=7;
 
   
 
   switch(Temp_Day)
 
   {
 
      case 1: 
 
         fprintf(lcd_putc,"APP - Today is: SUNDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 2: 
 
         fprintf(lcd_putc,"APP - Today is: MONDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 3: 
 
         fprintf(lcd_putc,"APP - Today is: TUESDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 4: 
 
         fprintf(lcd_putc,"APP - Today is: WEDNESDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 5: 
 
         fprintf(lcd_putc,"APP - Today is: THURSDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 6: 
 
         fprintf(lcd_putc,"APP - Today is: FRIDAY - %u\r\n",Temp_Day);
 
      break;
 
      case 7: 
 
         fprintf(lcd_putc,"APP - Today is: SATURDAY - %u\r\n",Temp_Day);
 
      break;
 
      default:
 
            fprintf(lcd_putc,"APP - DAY ERROR - %u\r\n",Temp_Day);
 
            Temp_Day=55;
 
      break;
 
   }
 
   return(Temp_Day);
 
}
 
 | 	  
 
 
should make scheduled tasks easier... especially those that don't need to run on weekends!
 
 
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 | 
			 
		  |