12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
- * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
- *
- * Copyright 2016-2017 (c) Julius Pfrommer, Fraunhofer IOSB
- * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
- * Copyright 2017 (c) Thomas Stalder
- */
- #ifdef UA_ARCHITECTURE_ARDUINO
- /* Enable POSIX features */
- #if !defined(_XOPEN_SOURCE)
- # define _XOPEN_SOURCE 600
- #endif
- #ifndef _DEFAULT_SOURCE
- # define _DEFAULT_SOURCE
- #endif
- /* On older systems we need to define _BSD_SOURCE.
- * _DEFAULT_SOURCE is an alias for that. */
- #ifndef _BSD_SOURCE
- # define _BSD_SOURCE
- #endif
- #include <time.h>
- #include <sys/time.h>
- #include "ua_types.h"
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- UA_DateTime UA_DateTime_now(void) {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (tv.tv_sec * UA_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
- }
- /* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
- UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
- time_t gmt, rawtime = time(NULL);
- struct tm *ptm;
- struct tm gbuf;
- ptm = gmtime_r(&rawtime, &gbuf);
- // Request that mktime() looksup dst in timezone database
- ptm->tm_isdst = -1;
- gmt = mktime(ptm);
- return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
- }
- UA_DateTime UA_DateTime_nowMonotonic(void) {
- portTickType TaskTime = xTaskGetTickCount();
- UA_DateTimeStruct UATime;
- UATime.milliSec = (UA_UInt16) TaskTime;
- struct timespec ts;
- ts.tv_sec = UATime.milliSec/1000;
- ts.tv_nsec = (UATime.milliSec % 1000)* 1000000;
- return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
- }
- #endif /* UA_ARCHITECTURE_ARDUINO */
|