Add 'x' prefix to time functions

.. to avoid name-clashes with (some) libraries. This now also should
make it work for the 'native' target (untested).
This commit is contained in:
Ralf Schlatterbeck 2016-03-29 17:47:03 +02:00
parent b2a289924b
commit 743245e230
7 changed files with 64 additions and 66 deletions

View file

@ -38,7 +38,7 @@
#include <assert.h> #include <assert.h>
#include "contiki.h" #include "contiki.h"
#include "cron.h" #include "cron.h"
#include "time.h" #include "xtime.h"
#undef DEBUG #undef DEBUG
@ -50,7 +50,7 @@ static struct cron_entry *entry_freelist = NULL;
static struct cron_entry *entry_list = NULL; static struct cron_entry *entry_list = NULL;
typedef int64_t minute_t; typedef int64_t minute_t;
static time_t start_time; static xtime_t start_time;
static minute_t cron_clock_time; static minute_t cron_clock_time;
/* Register a new cron command /* Register a new cron command
@ -313,19 +313,19 @@ int parse_crontab_line
static void set_time (void) static void set_time (void)
{ {
struct tm *tm; struct xtm *tm;
struct timeval tv; struct xtimeval tv;
gettimeofday (&tv, NULL); xgettimeofday (&tv, NULL);
start_time = tv.tv_sec; 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 */ /* 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) static void find_jobs (minute_t vtime, int do_wild, int do_nonwild)
{ {
time_t virtual_second = vtime * SECONDS_PER_MINUTE; xtime_t virtual_second = vtime * SECONDS_PER_MINUTE;
struct tm *tm = gmtime (&virtual_second); struct xtm *tm = xgmtime (&virtual_second);
int minute, hour, dom, month, dow; int minute, hour, dom, month, dow;
struct cron_entry *e; struct cron_entry *e;

View file

@ -11,7 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "contiki.h" #include "contiki.h"
#include "time.h" #include "xtime.h"
#include "time_resource.h" #include "time_resource.h"
#include "jsonparse.h" #include "jsonparse.h"
#include "er-coap.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) size_t time_to_string (const char *name, const char *uri, char *buf, size_t bs)
{ {
struct timeval tv; struct xtimeval tv;
struct tm tm; struct xtm tm;
struct tm *(*method)(const time_t *, struct tm *) = gmtime_r; struct xtm *(*method)(const xtime_t *, struct xtm *) = xgmtime_r;
if (0 == strcmp (name, "localtime")) { if (0 == strcmp (name, "localtime")) {
method = localtime_r; method = xlocaltime_r;
} }
gettimeofday (&tv, NULL); xgettimeofday (&tv, NULL);
method (&tv.tv_sec, &tm); method (&tv.tv_sec, &tm);
return snprintf return snprintf
( buf ( buf

View file

@ -15,7 +15,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "contiki.h" #include "contiki.h"
#include "time.h" #include "xtime.h"
#include "time_resource.h" #include "time_resource.h"
#include "jsonparse.h" #include "jsonparse.h"
#include "er-coap.h" #include "er-coap.h"
@ -23,18 +23,18 @@
int timestamp_from_string (const char *name, const char *uri, const char *s) 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)? // FIXME: Platform has no strtoll (long long)?
tv.tv_sec = strtol (s, NULL, 10); tv.tv_sec = strtol (s, NULL, 10);
settimeofday (&tv, NULL); xsettimeofday (&tv, NULL);
return 0; return 0;
} }
size_t size_t
timestamp_to_string (const char *name, const char *uri, char *buf, size_t bsize) timestamp_to_string (const char *name, const char *uri, char *buf, size_t bsize)
{ {
struct timeval tv; struct xtimeval tv;
gettimeofday (&tv, NULL); xgettimeofday (&tv, NULL);
// FIXME: Platform doesn't seem to support long long printing // FIXME: Platform doesn't seem to support long long printing
// We get empty string // We get empty string
return snprintf (buf, bsize, "%ld", (long)tv.tv_sec); return snprintf (buf, bsize, "%ld", (long)tv.tv_sec);

View file

@ -11,7 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "contiki.h" #include "contiki.h"
#include "time.h" #include "xtime.h"
#include "time_resource.h" #include "time_resource.h"
#include "jsonparse.h" #include "jsonparse.h"
#include "er-coap.h" #include "er-coap.h"

View file

@ -10,7 +10,7 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "contiki-lib.h" #include "contiki-lib.h"
#include "time.h" #include "xtime.h"
#include "tzparse.h" #include "tzparse.h"
#define SECSPERMIN 60 #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 * internal value may be overwritten "by subsequent calls to any of the
* date and time functions". * date and time functions".
*/ */
static struct tm tm; static struct xtm tm;
/* /*
* Internal variables to manage offset of utc from the contiki clock * 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, * The last_seconds is used to check if we had a seconds overflow,
* although this happens only every 136 years :-) * although this happens only every 136 years :-)
*/ */
static time_t clock_offset; static xtime_t clock_offset;
static uint32_t last_seconds; static uint32_t last_seconds;
static time_t static xtime_t
transtime (time_t janfirst, int year, const struct tzrule *rp, long offset); transtime (xtime_t janfirst, int year, const struct tzrule *rp, long offset);
# define LEAP_YEAR(_year) \ # define LEAP_YEAR(_year) \
((_year % 4) == 0 && (_year % 100 != 0 || _year % 400 == 0)) ((_year % 4) == 0 && (_year % 100 != 0 || _year % 400 == 0))
# define YDAYS(_year) (LEAP_YEAR(year) ? 366 : 365) # 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; unsigned int year;
int days, month, month_len; int days, month, month_len;
time_t t = *timep; xtime_t t = *timep;
ptm->tm_sec = t % 60; ptm->tm_sec = t % 60;
t /= 60; t /= 60;
ptm->tm_min = t % 60; ptm->tm_min = t % 60;
@ -113,18 +113,18 @@ struct tm *gmtime_r (const time_t *timep, struct tm *ptm)
return 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 * 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; xtime_t janfirst = 0;
time_t starttime, endtime; xtime_t starttime, endtime;
int year = 1970; int year = 1970;
int lastdst = 0; int lastdst = 0;
if (tzo->dstname == NULL) { if (tzo->dstname == NULL) {
@ -148,12 +148,12 @@ static int is_dst (const time_t *timep, const struct tzoffset_info *tzo)
return lastdst; 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; const struct tzoffset_info *tzo = &localtime_tzoffset;
int isdst = 0; int isdst = 0;
long offset = 0; long offset = 0;
time_t t = *timep; xtime_t t = *timep;
if (tzo->stdname == NULL) { if (tzo->stdname == NULL) {
set_tz (DEFAULT_TIMEZONE); set_tz (DEFAULT_TIMEZONE);
@ -161,28 +161,28 @@ struct tm *localtime_r (const time_t *timep, struct tm *ptm)
isdst = is_dst (timep, tzo); isdst = is_dst (timep, tzo);
offset = isdst ? tzo->dstoffset : tzo->stdoffset; offset = isdst ? tzo->dstoffset : tzo->stdoffset;
t -= offset; t -= offset;
gmtime_r (&t, ptm); xgmtime_r (&t, ptm);
ptm->tm_isdst = isdst; ptm->tm_isdst = isdst;
ptm->tm_gmtoff = -offset; ptm->tm_gmtoff = -offset;
ptm->tm_zone = isdst ? tzo->dstname : tzo->stdname; ptm->tm_zone = isdst ? tzo->dstname : tzo->stdname;
return ptm; 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 * \brief Get time in seconds and microseconds
* gettimeofday will return the clock time as the microseconds part * xgettimeofday will return the clock time as the microseconds part
* while settimeofday will *ignore* the microseconds part (for now). * while xsettimeofday will *ignore* the microseconds part (for now).
* Note that the contiki clock interface is broken anyway, we can't read * 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 * seconds and sub-seconds atomically. We try to work around this by
* repeatedly reading seconds, sub-seconds, seconds until first and * repeatedly reading seconds, sub-seconds, seconds until first and
* second read of seconds match. * second read of seconds match.
*/ */
int gettimeofday (struct timeval *tv, struct timezone *tz) int xgettimeofday (struct xtimeval *tv, struct timezone *tz)
{ {
uint32_t cs; uint32_t cs;
if (tv) { if (tv) {
@ -196,7 +196,7 @@ int gettimeofday (struct timeval *tv, struct timezone *tz)
} }
last_seconds = cs; last_seconds = cs;
tv->tv_sec = cs + clock_offset; 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; * 1000000L / CLOCK_SECOND;
if (cs == clock_seconds ()) { if (cs == clock_seconds ()) {
break; break;
@ -218,7 +218,7 @@ int gettimeofday (struct timeval *tv, struct timezone *tz)
/** /**
* \brief Set time in seconds, microseconds ignored for now * \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 */ /* Don't allow setting timezone */
if (tz) { if (tz) {
@ -290,11 +290,11 @@ static int save_tznames
* calculate the Epoch-relative time that rule takes effect. * calculate the Epoch-relative time that rule takes effect.
*/ */
static time_t static xtime_t
transtime(time_t janfirst, int year, const struct tzrule *rulep, long offset) transtime(xtime_t janfirst, int year, const struct tzrule *rulep, long offset)
{ {
int leapyear; int leapyear;
time_t value; xtime_t value;
int i; int i;
int d, m1, yy0, yy1, yy2, dow; int d, m1, yy0, yy1, yy2, dow;

View file

@ -16,23 +16,21 @@
* Ralf Schlatterbeck <rsc@tux.runtux.com> * Ralf Schlatterbeck <rsc@tux.runtux.com>
*/ */
#ifndef time_h #ifndef xtime_h
#define time_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 #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_seconds() 1
#define clock_time() 1 #define clock_time() 1
#endif #endif
#define DEFAULT_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3" #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; typedef signed long suseconds_t;
#ifdef __cplusplus #ifdef __cplusplus
@ -40,7 +38,7 @@ extern "C" {
#endif #endif
/* tm_gmtoff and tm_zone are BSD additions */ /* tm_gmtoff and tm_zone are BSD additions */
struct tm { struct xtm {
uint32_t tm_year; /* year */ uint32_t tm_year; /* year */
uint16_t tm_yday; /* day in the year */ uint16_t tm_yday; /* day in the year */
uint8_t tm_sec; /* seconds */ uint8_t tm_sec; /* seconds */
@ -54,8 +52,8 @@ struct tm {
const char *tm_zone; /* Timezone abbreviation */ const char *tm_zone; /* Timezone abbreviation */
}; };
struct timeval { struct xtimeval {
time_t tv_sec; /* seconds */ xtime_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */ suseconds_t tv_usec; /* microseconds */
}; };
@ -64,13 +62,13 @@ struct timezone {
int tz_dsttime; /* type of DST correction, unused */ int tz_dsttime; /* type of DST correction, unused */
}; };
struct tm *gmtime (const time_t *timep); struct xtm *xgmtime (const xtime_t *timep);
struct tm *gmtime_r (const time_t *timep, struct tm *result); struct xtm *xgmtime_r (const xtime_t *timep, struct xtm *result);
struct tm *localtime (const time_t *timep); struct xtm *xlocaltime (const xtime_t *timep);
struct tm *localtime_r (const time_t *timep, struct tm *result); struct xtm *xlocaltime_r (const xtime_t *timep, struct xtm *result);
int gettimeofday (struct timeval *tv, struct timezone *tz); int xgettimeofday (struct xtimeval *tv, struct timezone *tz);
int settimeofday (const struct timeval *tv, const 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 * Maximum length of all timezone names, this is much longer in UNIX
@ -92,5 +90,5 @@ size_t len_tz (void);
} }
#endif #endif
#endif // time_h #endif // xtime_h
/** @} */ /** @} */

View file

@ -43,7 +43,7 @@
#include "contiki.h" #include "contiki.h"
#include "contiki-net.h" #include "contiki-net.h"
#include "er-coap-engine.h" #include "er-coap-engine.h"
#include "time.h" #include "xtime.h"
#include "cron.h" #include "cron.h"
#include "time_resource.h" #include "time_resource.h"
#include "jsonparse.h" #include "jsonparse.h"