From 0b8bd28b705e3b85b9e22d2372a6377a99adb13e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Janosch=20Gr=C3=A4f?= Date: Wed, 24 Dec 2008 17:22:45 +0100 Subject: [PATCH] stdlibc/time: +calculation of weekday and yearday --- apps/include/time.h | 13 ++++++------ apps/lib/stdlibc/time.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/apps/include/time.h b/apps/include/time.h index 9e116c6..db49dbf 100644 --- a/apps/include/time.h +++ b/apps/include/time.h @@ -19,7 +19,12 @@ #ifndef _TIME_H_ #define _TIME_H_ -#include +#include + +#define CLOCKS_PER_SEC 100 /// @see kernel2/cpu.h +#define CLOCK_REALTIME 0 // ID of Realtime clock (?) ///< @see ? +#define TIMER_ABSTIME 0 // Flag indicating time is absolute ///< @see ? +#define CLOCK_MONOTONIC 1 // ID of monotonic clock (?) ///< @see ? struct tm { int tm_sec; // Seconds [0..59] @@ -41,13 +46,9 @@ struct itimerspec { struct timespec it_value; // Timer expiration. }; -#define CLOCKS_PER_SEC 100 /// @see kernel2/cpu.h -#define CLOCK_REALTIME 0 // ID of Realtime clock (?) ///< @see ? -#define TIMER_ABSTIME 0 // Flag indicating time is absolute ///< @see ? -#define CLOCK_MONOTONIC 1 // ID of monotonic clock (?) ///< @see ? - time_t time(time_t *tloc); time_t mktime(struct tm *tm); +char *asctime(const struct tm *timeptr); clock_t clock(); #endif diff --git a/apps/lib/stdlibc/time.c b/apps/lib/stdlibc/time.c index 303c14c..2dfeb8d 100644 --- a/apps/lib/stdlibc/time.c +++ b/apps/lib/stdlibc/time.c @@ -19,10 +19,59 @@ #include #include #include +#include +#include static time_t start_time = 0; static clock_t start_ticks; +static int is_leapyear(unsigned int year) { + if (year%400==0) return 1; + else if (year%100==0) return 0; + else if (year%4==0) return 1; + else return 0; +} + +static void getyday(struct tm *tm) { + unsigned int mdays[12] = { + 31, + 31+28, + 31+28+31, + 31+28+31+30, + 31+28+31+30+31, + 31+28+31+30+31+30, + 31+28+31+30+31+30+31, + 31+28+31+30+31+30+31+31, + 31+28+31+30+31+30+31+31+30, + 31+28+31+30+31+30+31+31+30+31, + 31+28+31+30+31+30+31+31+30+31+30, + 31+28+31+30+31+30+31+31+30+31+30+31 + }; + + tm->tm_yday = (tm->tm_mon>0?mdays[tm->tm_mon-1]:0)+tm->tm_mday; + if (is_leapyear(tm->tm_year+1900)) tm->tm_yday++; +} + +static void getwday(struct tm *tm) { + unsigned int doomsdays[] = {1,6,4,2}; + unsigned int year = tm->tm_year+1900; + unsigned int dday_idx = (year/100)%4; + unsigned int step1 = (year%100)/12; + unsigned int step2 = (year%100)%12; + unsigned int step3 = step2/4; + unsigned int dday_year = (doomsdays[dday_idx]+((step1+step2+step3)%7))%7; + unsigned int january0 = dday_year+(7-((59+is_leapyear(year)?1:0)%7)); + tm->tm_wday = ((january0+(tm->tm_yday%7))%7)-1; +} + +char *asctime(const struct tm *timeptr) { + char *wday_name[] = {"Sun","Mon","Tue","Wed","Thu", "Fri","Sat"}; + char *mon_name[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; + static char result[26]; + sprintf(result,"%s %s %u %02u:%02u:%02u %u\n",wday_name[timeptr->tm_wday],mon_name[timeptr->tm_mon],timeptr->tm_mday,timeptr->tm_hour,timeptr->tm_min, timeptr->tm_sec,1900+timeptr->tm_year); + return result; +} + time_t time(time_t *tloc) { if (start_time==0) { struct tm tm = { @@ -33,6 +82,8 @@ time_t time(time_t *tloc) { .tm_mon = cmos_getmonth()-1, .tm_year = cmos_getyear()-1900 }; + getyday(&tm); + getwday(&tm); start_time = mktime(&tm); start_ticks = clock(); @@ -45,7 +96,9 @@ time_t time(time_t *tloc) { } time_t mktime(struct tm *tm) { - return 0; + return tm->tm_sec + tm->tm_min*60 + tm->tm_hour*3600 + tm->tm_yday*86400 + + (tm->tm_year-70)*31536000 + ((tm->tm_year-69)/4)*86400 - + ((tm->tm_year-1)/100)*86400 + ((tm->tm_year+299)/400)*86400; } static clock_t start_clock = -1; -- 2.11.4.GIT