View previous topic :: View next topic |
Author |
Message |
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
Calendar calculations |
Posted: Thu Mar 12, 2009 1:26 pm |
|
|
Hi,
Has anyone written a software calendar? I dont want to reinvent the wheel
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
|
|
Posted: Thu Mar 12, 2009 1:33 pm |
|
|
Put this into CCS search engine:
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
|
|
Posted: Thu Mar 12, 2009 11:19 pm |
|
|
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
|
|
Posted: Fri Mar 13, 2009 1:37 am |
|
|
Thanks to all |
|
|
RVaughn13
Joined: 09 Feb 2006 Posts: 13 Location: Santa Fe, Texas
|
Day of Week Infomation |
Posted: Sat Mar 14, 2009 8:32 pm |
|
|
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 |
|
|
|