CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Weekday Calculator

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Weekday Calculator
PostPosted: Sun Nov 05, 2017 12:43 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group