/////////////////////////////////////////////////////////////////////////////
///                                                                       ///
///                               time.h                                  ///
///                                                                       ///
/// Time algorithms.  This follows the standard C API, with the following ///
/// exceptions:                                                           ///
///   * ctime() has another input parameter which is the pointer to where ///
///     the input string is.  Normally ctime() and asctime() write the    ///
///     output to a globally allocated string and return a pointer to     ///
///     this string.  This library doesn't want to make this assumption   ///
///     so the library doesn't allocate the space needed for those        ///
///     functions.                                                        ///
///   * asctime() has nother input parameter which is the pointer to      ///
///     where the input string is.  See the above paragraph.              ///
///   * strftime() is not supported.                                      ///
///   * SetTime() is added that initiliazes/set the current time.         ///
///                                                                       ///
/// This file only provides the prototypes and definitions needed to      ///
/// proved a time alogrithm that follows the C standard library.  You     ///
/// also need to include/link the actual library that performs the time   ///
/// base.  As of this writing CCS provides such a library, rtctimer.c,    ///
/// which performs the needed timebase on Timer2.  You may see a similar  ///
/// library for the dsPIC which has an internal RTC.                      ///
///                                                                       ///
/// API:                                                                  ///
///                                                                       ///
/// Variable definitions:                                                 ///
///   c - clock timer (clock_t), number of ticks since powerup.  See      ///
///       CLOCKS_PER_SECOND to determine clock rate.                      ///
///                                                                       ///
///   t - second timer (time_t), number of seconds since Jan 1st, 1970.   ///
///                                                                       ///
///   ts - time struct (struct_tm), a structure that holds time in        ///
///        descriptive format (seconds, minutes, hours, day, month, etc). ///
///                                                                       ///
/// CLOCKS_PER_SECOND - This is a constant which needs to be defined that ///
///   configures the timebase used by the clock timer and clock().        ///
///   If you are not using clock() then you don't need to define this.    ///
///   If you are using a method such a PIC's timer for the timebase then  ///
///   you will need to set this.                                          ///
///                                                                       ///
/// c = clock() - Return current clock timer.                             ///
///                                                                       ///
/// t = time(*t) - Return current second timer.  Returns twice (as a      ///
///         a return, and saves to input pointer).                        ///
///                                                                       ///
/// SetTime(*ts) - Initializes the current time.                           ///
///                                                                       ///
/// t = mktime(*ts) - Converts a time struct to a second timer.           ///
///                                                                       ///
/// t = difftime(t,t) - Returns difference between two second timers.     ///
///                                                                       ///
/// *char = ctime(*t, *char) - Converts second timer to a readable string ///
///                            Www Mmm dd hh:mm:ss yyyy                   ///
///                                                                       ///
/// *char = asctime(*ts, *char) - Converts time struct to a readable      ///
///                            string.                                    ///
///                            Www Mmm dd hh:mm:ss yyyy                   ///
///                                                                       ///
/// *ts = localtime(*t) - Converts second timer to a time struct.         ///
///                  *ts points to a global time struct and will be       ///
///                  corrupted in future calls to localtime().            ///
///                                                                       ///
/// NON-STANDARD ADDITIONS:                                               ///
/// WeekdayAbbreviations(day, *char) - Given the current day (in integer  ///
///            format, see enum Weekday) format to a string.              ///
///                                                                       ///
/// MonthAbbreviations(month, *char) - Given the current month (in        /// 
///            integer format, see enum Month) format to a string.        ///
///                                                                       ///
/// n = DaysInMonth(month, isLeapYear) - Returns the number of days in    ///
///            this month.                                                ///
///                                                                       ///
/// isLeapYear = LeapYear(year) - Returns TRUE if this is a leap year.    ///
///                                                                       ///
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2007 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS    ////
//// C compiler.  This source code may only be distributed to other    ////
//// licensed users of the CCS C compiler.  No other use,              ////
//// reproduction or distribution is permitted without written         ////
//// permission.  Derivative programs created using this software      ////
//// in object code form are not restricted in any way.                ////
///////////////////////////////////////////////////////////////////////////

#ifndef __TIME_H__
#define __TIME_H__

#include <stdlibm.h>

#ifndef CLOCKS_PER_SECOND
#define CLOCKS_PER_SECOND 1000
#endif

/* API Types*/
typedef signed int32 time_t;
typedef signed int32 clock_t;

typedef enum
{
   SUNDAY = 0,
   MONDAY,
   TUESDAY,
   WEDNESDAY,
   THURSDAY,
   FRIDAY,
   SATURDAY   
}  Weekday;

typedef enum
{
   JANUARY = 0,
   FEBRUARY,
   MARCH,
   APRIL,
   MAY,
   JUNE,
   JULY,
   AUGUST,
   SEPTEMBER,
   OCTOBER,
   NOVEMBER,
   DECEMBER
}  Month;

typedef struct
{
   unsigned int tm_sec;
   unsigned int tm_min;
   unsigned int tm_hour;
   unsigned int tm_mday;
   Month tm_mon;
   unsigned int16 tm_year;
   Weekday tm_wday;
   unsigned int16 tm_yday;
} struct_tm;


/* Functions */
clock_t clock(void);
time_t time(time_t * timer);
double difftime(time_t later, time_t earlier);
time_t mktime(struct_tm * timeT);

char * asctime(struct_tm * timeptr, char *szTime);
char * ctime(time_t * timer, char *szTime);
struct_tm * localtime ( time_t * timer );
void SetTime(time_t *nTime);

/* Globals & Resources */
static clock_t clock_ticks;
static time_t calendar_time;

void WeekdayAbbreviations(int day, char* wString);
void MonthAbbreviations(int month, char* mString);
int DaysInMonth(int month, int1 IsLeapYear);
int1 LeapYear(int16 year);

#endif
