2014-05-29 17:06:43 +02:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Resource for gmtime (utc) / localtime handling
|
|
|
|
* \author
|
|
|
|
* Ralf Schlatterbeck <rsc@runtux.com>
|
|
|
|
*
|
|
|
|
* \brief get time as a string in utc or localtime
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "contiki.h"
|
2016-03-29 17:47:03 +02:00
|
|
|
#include "xtime.h"
|
2014-05-29 17:06:43 +02:00
|
|
|
#include "time_resource.h"
|
|
|
|
#include "jsonparse.h"
|
2014-12-29 08:16:47 +01:00
|
|
|
#include "er-coap.h"
|
2014-06-27 22:25:51 +02:00
|
|
|
#include "generic_resource.h"
|
2014-05-29 17:06:43 +02:00
|
|
|
|
2016-02-26 07:54:05 +01:00
|
|
|
size_t time_to_string (const char *name, const char *uri, char *buf, size_t bs)
|
2014-06-27 22:25:51 +02:00
|
|
|
{
|
2016-03-29 17:47:03 +02:00
|
|
|
struct xtimeval tv;
|
|
|
|
struct xtm tm;
|
|
|
|
struct xtm *(*method)(const xtime_t *, struct xtm *) = xgmtime_r;
|
2014-06-27 22:25:51 +02:00
|
|
|
if (0 == strcmp (name, "localtime")) {
|
2016-03-29 17:47:03 +02:00
|
|
|
method = xlocaltime_r;
|
2014-06-27 22:25:51 +02:00
|
|
|
}
|
2016-03-29 17:47:03 +02:00
|
|
|
xgettimeofday (&tv, NULL);
|
2014-06-27 22:25:51 +02:00
|
|
|
method (&tv.tv_sec, &tm);
|
|
|
|
return snprintf
|
|
|
|
( buf
|
|
|
|
, bs
|
2016-02-26 07:54:05 +01:00
|
|
|
, "%lu-%02u-%02u %02u:%02u:%02u %s"
|
2014-06-27 22:25:51 +02:00
|
|
|
, 1900 + tm.tm_year
|
|
|
|
, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec
|
2016-02-18 09:55:07 +01:00
|
|
|
, tm.tm_zone
|
2014-06-27 22:25:51 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
GENERIC_RESOURCE \
|
2014-05-29 17:06:43 +02:00
|
|
|
( localtime
|
2014-06-27 22:25:51 +02:00
|
|
|
, Local time
|
|
|
|
, formatted time
|
2016-02-26 07:54:05 +01:00
|
|
|
, 1
|
2014-06-27 22:25:51 +02:00
|
|
|
, NULL
|
|
|
|
, time_to_string
|
2014-05-29 17:06:43 +02:00
|
|
|
);
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
GENERIC_RESOURCE \
|
|
|
|
( utc
|
|
|
|
, UTC
|
|
|
|
, formatted time
|
2016-02-26 07:54:05 +01:00
|
|
|
, 1
|
2014-06-27 22:25:51 +02:00
|
|
|
, NULL
|
|
|
|
, time_to_string
|
2014-05-29 17:06:43 +02:00
|
|
|
);
|
|
|
|
|
2014-06-27 22:25:51 +02:00
|
|
|
/*
|
|
|
|
* VI settings, see coding style
|
|
|
|
* ex:ts=8:et:sw=2
|
|
|
|
*/
|
2014-05-29 17:06:43 +02:00
|
|
|
|