diff --git a/apps/time/cron.c b/apps/time/cron.c index 7fa21a0d1..22b4132d5 100644 --- a/apps/time/cron.c +++ b/apps/time/cron.c @@ -38,7 +38,7 @@ #include #include "contiki.h" #include "cron.h" -#include "time.h" +#include "xtime.h" #undef DEBUG @@ -50,7 +50,7 @@ static struct cron_entry *entry_freelist = NULL; static struct cron_entry *entry_list = NULL; typedef int64_t minute_t; -static time_t start_time; +static xtime_t start_time; static minute_t cron_clock_time; /* Register a new cron command @@ -313,19 +313,19 @@ int parse_crontab_line static void set_time (void) { - struct tm *tm; - struct timeval tv; - gettimeofday (&tv, NULL); + struct xtm *tm; + struct xtimeval tv; + xgettimeofday (&tv, NULL); start_time = tv.tv_sec; - tm = localtime (&start_time); + tm = xlocaltime (&start_time); /* We adjust the time to GMT so we can catch DST changes */ - cron_clock_time = (start_time + tm->tm_gmtoff) / (time_t)SECONDS_PER_MINUTE; + cron_clock_time = (start_time + tm->tm_gmtoff) / (xtime_t)SECONDS_PER_MINUTE; } static void find_jobs (minute_t vtime, int do_wild, int do_nonwild) { - time_t virtual_second = vtime * SECONDS_PER_MINUTE; - struct tm *tm = gmtime (&virtual_second); + xtime_t virtual_second = vtime * SECONDS_PER_MINUTE; + struct xtm *tm = xgmtime (&virtual_second); int minute, hour, dom, month, dow; struct cron_entry *e; diff --git a/apps/time/resource_gmtime.c b/apps/time/resource_gmtime.c index 16ba6db02..9aef78efe 100644 --- a/apps/time/resource_gmtime.c +++ b/apps/time/resource_gmtime.c @@ -11,7 +11,7 @@ #include #include #include "contiki.h" -#include "time.h" +#include "xtime.h" #include "time_resource.h" #include "jsonparse.h" #include "er-coap.h" @@ -19,13 +19,13 @@ size_t time_to_string (const char *name, const char *uri, char *buf, size_t bs) { - struct timeval tv; - struct tm tm; - struct tm *(*method)(const time_t *, struct tm *) = gmtime_r; + struct xtimeval tv; + struct xtm tm; + struct xtm *(*method)(const xtime_t *, struct xtm *) = xgmtime_r; if (0 == strcmp (name, "localtime")) { - method = localtime_r; + method = xlocaltime_r; } - gettimeofday (&tv, NULL); + xgettimeofday (&tv, NULL); method (&tv.tv_sec, &tm); return snprintf ( buf diff --git a/apps/time/resource_timestamp.c b/apps/time/resource_timestamp.c index 2406bfea9..aa18a19eb 100644 --- a/apps/time/resource_timestamp.c +++ b/apps/time/resource_timestamp.c @@ -15,7 +15,7 @@ #include #include #include "contiki.h" -#include "time.h" +#include "xtime.h" #include "time_resource.h" #include "jsonparse.h" #include "er-coap.h" @@ -23,18 +23,18 @@ int timestamp_from_string (const char *name, const char *uri, const char *s) { - struct timeval tv; + struct xtimeval tv; // FIXME: Platform has no strtoll (long long)? tv.tv_sec = strtol (s, NULL, 10); - settimeofday (&tv, NULL); + xsettimeofday (&tv, NULL); return 0; } size_t timestamp_to_string (const char *name, const char *uri, char *buf, size_t bsize) { - struct timeval tv; - gettimeofday (&tv, NULL); + struct xtimeval tv; + xgettimeofday (&tv, NULL); // FIXME: Platform doesn't seem to support long long printing // We get empty string return snprintf (buf, bsize, "%ld", (long)tv.tv_sec); diff --git a/apps/time/resource_timezone.c b/apps/time/resource_timezone.c index 2937a3de3..1e06ad0d1 100644 --- a/apps/time/resource_timezone.c +++ b/apps/time/resource_timezone.c @@ -11,7 +11,7 @@ #include #include #include "contiki.h" -#include "time.h" +#include "xtime.h" #include "time_resource.h" #include "jsonparse.h" #include "er-coap.h" diff --git a/apps/time/time.c b/apps/time/time.c index 4916ae126..654a88de0 100644 --- a/apps/time/time.c +++ b/apps/time/time.c @@ -10,7 +10,7 @@ #include #include #include "contiki-lib.h" -#include "time.h" +#include "xtime.h" #include "tzparse.h" #define SECSPERMIN 60 @@ -37,7 +37,7 @@ static struct tzoffset_info localtime_tzoffset; * internal value may be overwritten "by subsequent calls to any of the * date and time functions". */ -static struct tm tm; +static struct xtm tm; /* * Internal variables to manage offset of utc from the contiki clock @@ -48,21 +48,21 @@ static struct tm tm; * The last_seconds is used to check if we had a seconds overflow, * although this happens only every 136 years :-) */ -static time_t clock_offset; +static xtime_t clock_offset; static uint32_t last_seconds; -static time_t -transtime (time_t janfirst, int year, const struct tzrule *rp, long offset); +static xtime_t +transtime (xtime_t janfirst, int year, const struct tzrule *rp, long offset); # define LEAP_YEAR(_year) \ ((_year % 4) == 0 && (_year % 100 != 0 || _year % 400 == 0)) # define YDAYS(_year) (LEAP_YEAR(year) ? 366 : 365) -struct tm *gmtime_r (const time_t *timep, struct tm *ptm) +struct xtm *xgmtime_r (const xtime_t *timep, struct xtm *ptm) { unsigned int year; int days, month, month_len; - time_t t = *timep; + xtime_t t = *timep; ptm->tm_sec = t % 60; t /= 60; ptm->tm_min = t % 60; @@ -113,18 +113,18 @@ struct tm *gmtime_r (const time_t *timep, struct tm *ptm) return ptm; } -struct tm *gmtime (const time_t *timep) +struct xtm *xgmtime (const xtime_t *timep) { - return gmtime_r (timep, &tm); + return xgmtime_r (timep, &tm); } /* * Compute is_dst flag of given timestamp */ -static int is_dst (const time_t *timep, const struct tzoffset_info *tzo) +static int is_dst (const xtime_t *timep, const struct tzoffset_info *tzo) { - time_t janfirst = 0; - time_t starttime, endtime; + xtime_t janfirst = 0; + xtime_t starttime, endtime; int year = 1970; int lastdst = 0; if (tzo->dstname == NULL) { @@ -148,12 +148,12 @@ static int is_dst (const time_t *timep, const struct tzoffset_info *tzo) return lastdst; } -struct tm *localtime_r (const time_t *timep, struct tm *ptm) +struct xtm *xlocaltime_r (const xtime_t *timep, struct xtm *ptm) { const struct tzoffset_info *tzo = &localtime_tzoffset; int isdst = 0; long offset = 0; - time_t t = *timep; + xtime_t t = *timep; if (tzo->stdname == NULL) { set_tz (DEFAULT_TIMEZONE); @@ -161,28 +161,28 @@ struct tm *localtime_r (const time_t *timep, struct tm *ptm) isdst = is_dst (timep, tzo); offset = isdst ? tzo->dstoffset : tzo->stdoffset; t -= offset; - gmtime_r (&t, ptm); + xgmtime_r (&t, ptm); ptm->tm_isdst = isdst; ptm->tm_gmtoff = -offset; ptm->tm_zone = isdst ? tzo->dstname : tzo->stdname; return ptm; } -struct tm *localtime (const time_t *timep) +struct xtm *xlocaltime (const xtime_t *timep) { - return localtime_r (timep, &tm); + return xlocaltime_r (timep, &tm); } /** * \brief Get time in seconds and microseconds - * gettimeofday will return the clock time as the microseconds part - * while settimeofday will *ignore* the microseconds part (for now). + * xgettimeofday will return the clock time as the microseconds part + * while xsettimeofday will *ignore* the microseconds part (for now). * Note that the contiki clock interface is broken anyway, we can't read * seconds and sub-seconds atomically. We try to work around this by * repeatedly reading seconds, sub-seconds, seconds until first and * second read of seconds match. */ -int gettimeofday (struct timeval *tv, struct timezone *tz) +int xgettimeofday (struct xtimeval *tv, struct timezone *tz) { uint32_t cs; if (tv) { @@ -196,7 +196,7 @@ int gettimeofday (struct timeval *tv, struct timezone *tz) } last_seconds = cs; tv->tv_sec = cs + clock_offset; - tv->tv_usec = ((time_t)(clock_time () % CLOCK_SECOND)) + tv->tv_usec = ((xtime_t)(clock_time () % CLOCK_SECOND)) * 1000000L / CLOCK_SECOND; if (cs == clock_seconds ()) { break; @@ -218,7 +218,7 @@ int gettimeofday (struct timeval *tv, struct timezone *tz) /** * \brief Set time in seconds, microseconds ignored for now */ -int settimeofday (const struct timeval *tv, const struct timezone *tz) +int xsettimeofday (const struct xtimeval *tv, const struct timezone *tz) { /* Don't allow setting timezone */ if (tz) { @@ -290,11 +290,11 @@ static int save_tznames * calculate the Epoch-relative time that rule takes effect. */ -static time_t -transtime(time_t janfirst, int year, const struct tzrule *rulep, long offset) +static xtime_t +transtime(xtime_t janfirst, int year, const struct tzrule *rulep, long offset) { int leapyear; - time_t value; + xtime_t value; int i; int d, m1, yy0, yy1, yy2, dow; diff --git a/apps/time/time.h b/apps/time/xtime.h similarity index 74% rename from apps/time/time.h rename to apps/time/xtime.h index 2ea8d53c2..8dcd78e5f 100644 --- a/apps/time/time.h +++ b/apps/time/xtime.h @@ -16,23 +16,21 @@ * Ralf Schlatterbeck */ -#ifndef time_h -#define time_h +#ifndef xtime_h +#define xtime_h + +/* This is a time.h implementation but to avoid name-clashes with libs + * trying to be helpfull we add the prefix x + */ #ifdef LOCAL_COMPILE -#define tm l_tm -#define timeval l_timeval -#define time_t l_time_t -#define gmtime l_gmtime -#define localtime l_localtime -#define gettimeofday l_gettimeofday #define clock_seconds() 1 #define clock_time() 1 #endif #define DEFAULT_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3" -typedef signed long long time_t; +typedef signed long long xtime_t; typedef signed long suseconds_t; #ifdef __cplusplus @@ -40,7 +38,7 @@ extern "C" { #endif /* tm_gmtoff and tm_zone are BSD additions */ -struct tm { +struct xtm { uint32_t tm_year; /* year */ uint16_t tm_yday; /* day in the year */ uint8_t tm_sec; /* seconds */ @@ -54,8 +52,8 @@ struct tm { const char *tm_zone; /* Timezone abbreviation */ }; -struct timeval { - time_t tv_sec; /* seconds */ +struct xtimeval { + xtime_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; @@ -64,13 +62,13 @@ struct timezone { int tz_dsttime; /* type of DST correction, unused */ }; -struct tm *gmtime (const time_t *timep); -struct tm *gmtime_r (const time_t *timep, struct tm *result); -struct tm *localtime (const time_t *timep); -struct tm *localtime_r (const time_t *timep, struct tm *result); +struct xtm *xgmtime (const xtime_t *timep); +struct xtm *xgmtime_r (const xtime_t *timep, struct xtm *result); +struct xtm *xlocaltime (const xtime_t *timep); +struct xtm *xlocaltime_r (const xtime_t *timep, struct xtm *result); -int gettimeofday (struct timeval *tv, struct timezone *tz); -int settimeofday (const struct timeval *tv, const struct timezone *tz); +int xgettimeofday (struct xtimeval *tv, struct timezone *tz); +int xsettimeofday (const struct xtimeval *tv, const struct timezone *tz); /* * Maximum length of all timezone names, this is much longer in UNIX @@ -92,5 +90,5 @@ size_t len_tz (void); } #endif -#endif // time_h +#endif // xtime_h /** @} */ diff --git a/examples/osd/wallclock-time/er-example-server.c b/examples/osd/wallclock-time/er-example-server.c index bc5d179ac..709844d2d 100644 --- a/examples/osd/wallclock-time/er-example-server.c +++ b/examples/osd/wallclock-time/er-example-server.c @@ -43,7 +43,7 @@ #include "contiki.h" #include "contiki-net.h" #include "er-coap-engine.h" -#include "time.h" +#include "xtime.h" #include "cron.h" #include "time_resource.h" #include "jsonparse.h"