Some time related functionality

This commit is contained in:
Stefan `Sec` Zehl 2011-07-30 15:25:14 +02:00
parent f79f8a93f5
commit e8f06b2388
4 changed files with 67 additions and 0 deletions

View File

@ -20,6 +20,7 @@ OBJS += random.o
OBJS += idle.o
OBJS += config.o
OBJS += itoa.o
OBJS += simpletime.o
LIBNAME=basic

View File

@ -206,4 +206,8 @@ int applyConfig(void);
// itoa.c
const char* IntToStrX(unsigned int num, unsigned int mxlen);
// simpletime.c
#include "basic/simpletime.h"
#endif

View File

@ -0,0 +1,46 @@
#include <sysinit.h>
#include "simpletime.h"
#include "basic/basic.h"
time_t _timet=0;
int _ytab[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
struct tm * mygmtime(register const time_t time) {
static struct tm br_time;
register struct tm *timep = &br_time;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;
dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;
timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
timep->tm_year = year - YEAR0;
timep->tm_yday = dayno;
timep->tm_mon = 0;
while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
timep->tm_mon++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;
return timep;
}
// Timezones suck. Currently we only do it all in localtime.
// I know it's broken. Sorry
time_t getSeconds(void){
return _timet+(getTimer()*SYSTICKSPEED/1000);
};

View File

@ -0,0 +1,16 @@
#ifndef __SIMPLETIME_H_
#define __SIMPLETIME_H_
#include <time.h>
#define YEAR0 1900 /* the first year */
#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
extern time_t _timet;
struct tm * mygmtime(register const time_t time);
time_t getSeconds(void);
#endif