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

Calendar calculations

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

Calendar calculations
PostPosted: Thu Mar 12, 2009 1:26 pm     Reply with quote

Hi,

Has anyone written a software calendar? I dont want to reinvent the wheel Laughing
I have the following setup and i need to add a function to increment (by one second) and a function to get what day-of-the-week out of a given date. I already have a 1 sec timer.

Code:

///////////////////////////////////////////////////////////////////////////////
#define  AM    0
#define  PM    1
#define  H_12  0
#define  H_24  1
///////////////////////////////////////////////////////////////////////////////
typedef struct _calendar {
                  int8 Seconds;
                  int8 Minutes;
                  int8 Hours;
                  int8 DayOfWeek;
                  int8 Day;
                  int8 Month;
                  int8 Year;
                  int1 H_12_24;
                  int1 AM_PM;
                  }Calendar;
Calendar MyCalendar;
///////////////////////////////////////////////////////////////////////////////
void init_Calendar()
{
   MyCalendar.Seconds = 0;
   MyCalendar.Minutes = 0;
   MyCalendar.Hours = 12;
   MyCalendar.DayOfWeek = 5;  //Thursday
   MyCalendar.Day = 12;       //12th
   MyCalendar.Month = 3;      //March
   MyCalendar.Year = 9;       //2009
   MyCalendar.H_12_24 = H_12; //12 hours
   MyCalendar.AM_PM = PM;     //PM
}
///////////////////////////////////////////////////////////////////////////////

That would spare a bit of time...
Thanks in advance...


Last edited by PICoHolic on Fri Mar 13, 2009 1:26 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 12, 2009 1:33 pm     Reply with quote

Put this into CCS search engine:
Quote:
DayofWeek

Get this:
Date and time routines by VanHauser
http://www.ccsinfo.com/forum/viewtopic.php?t=25611
Which contains this routine:
Quote:
Weekday computation. Adapted ONLY for years 2000 to 2099

int ISODayOfWeek(int YY, int MM, int DD) {
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Mar 12, 2009 11:19 pm     Reply with quote

I use the following two functions for day of week calculation:

Code:


#define START_WEEKDAY 5 /* Saturday Jan 1, 2000 , zero being Monday */

typedef struct {
  UCHAR year;
  UCHAR month;
  UCHAR day;
  UCHAR hour;
  UCHAR minute;
  UCHAR second;
} DATE_TIME;

int16  const CummulativeMonthDays[12] = {  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

// Compute day as number from year 0 day 1.
#separate
int16 RTC_ComputeDay(DATE_TIME *dt)
  {
  int16 DayAcc = 0;                                                                       // clear day accumulator
  DayAcc  = dt->Year * 365 + ((dt->Year - 1) >> 2);                                      // multiply years by 365 and add a day for every four years
  DayAcc += CummulativeMonthDays[dt->Month - 1] + dt->Day;                               // add days of all months in current year up to current month
  if (dt->Month > 2 && (dt->Year & 3) == 0)                                              // if this year is a leap year and month is March to
    DayAcc++;                                                                            // December add one day for February 29th
  return DayAcc;                                                                         // return day accumulator
  }


// compute day of week. return 0 for Monday, 1 for Tuesday .. 6 for Sunday
#separate
int8 RTC_ComputeWeekday(DATE_TIME *dt)
  {
  int16 WeekDay;
  WeekDay = RTC_ComputeDay(dt) + START_WEEKDAY;                                          // compute today's number and add
  return WeekDay % 7;                                                                    // 5. return day of week
  }

_________________
Every solution has a problem.
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

PostPosted: Fri Mar 13, 2009 1:37 am     Reply with quote

Thanks to all
RVaughn13



Joined: 09 Feb 2006
Posts: 13
Location: Santa Fe, Texas

View user's profile Send private message Visit poster's website

Day of Week Infomation
PostPosted: Sat Mar 14, 2009 8:32 pm     Reply with quote

Hi,
I have included a link that will give you more information about day-of-week calculations, if you still need it.
Hope this helps!
Rick
http://www.e-sys.us/Files%20and%20Pictures/es003.PDF
_________________
--Rick
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion 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