
/************************************************************************/
/*									*/
/*				Clock / Calendar			*/
/*									*/
/*			Author: Peter Dannegger				*/
/*      			danni@specs.de				*/
/*									*/
/************************************************************************/
#pragma pl(30000) cd noco


typedef unsigned char  u8;
typedef   signed char  s8;
typedef unsigned short u16;
typedef   signed short s16;
typedef unsigned long  u32;
typedef   signed long  s32;


struct time {
  u8  second;
  u8  minute;
  u8  hour;
  u8  day;
  u8  month;
  u16 year;
  u8  wday;
};


// 4294967295 sec = 0136 y + 4 m + 16 d + 6 h + 28 min + 15 sec

#define FIRSTYEAR	1990			// start year
#define FIRSTDAY	1			// 0 = Sunday


void gettime( u32 sec, struct time idata *t )
{
  u16 day;
  u8 year;
  u16 dayofyear;
  u8 leap400;
  u8 month;
  u8 code DayOfMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  t->second = sec % 60;
  sec /= 60;
  t->minute = sec % 60;
  sec /= 60;
  t->hour = sec % 24;
  day = sec / 24;

  t->wday = (day + FIRSTDAY) % 7;		// weekday

  year = FIRSTYEAR % 100;			// 0..99
  leap400 = 4 - FIRSTYEAR % 400 / 100;		// 4, 3, 2, 1

  for(;;){
    dayofyear = 365;
    if( (year & 3) == 0 )
      dayofyear = 366;					// leap year
    if( year == 0 || year == 100 || year == 200 ){	// 100 year exception
      if( --leap400 )					// 400 year exception
        dayofyear = 365;
    }
    if( day < dayofyear )
      break;
    day -= dayofyear;
    year++;					// 00..136 / 99..235
  }
  t->year = year + FIRSTYEAR / 100 * 100;	// + century

  if( dayofyear & 1 && day > 58 )		// no leap year and after 28.2.
    day++;					// skip 29.2.

  for( month = 1; day >= DayOfMonth[month-1]; month++ )
    day -= DayOfMonth[month-1];

  t->month = month;				// 1..12
  t->day = day + 1;				// 1..31
}


void main (void)
{
  u32 sec;
  struct time idata current_time;

  gettime( 4294967295L, &current_time );
  for( sec = 0;; sec += 1 ){
    gettime( sec, &current_time );
  }
}

