Unverified Commit af8b548e authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add time support (time, gettimeofday, etc.) (#138)

parent 0550fe75
......@@ -149,12 +149,27 @@ extern "C" int _getpid (void) {
return -1;
}
struct timeval;
extern "C" int _gettimeofday (struct timeval *ptimeval, void *ptimezone)
{
errno = ENOSYS;
return -1;
static int64_t __timedelta_us = 0.0;
extern "C" int _gettimeofday (struct timeval *tv, void *tz) {
uint64_t now_us = to_us_since_boot(get_absolute_time()) + __timedelta_us;
if (tv) {
tv->tv_sec = now_us / 1000000L;
tv->tv_usec = now_us % 1000000L;
}
return 0;
}
extern "C" int settimeofday (const struct timeval *tv, const struct timezone *tz) {
uint64_t now_us = to_us_since_boot(get_absolute_time());
if (tv) {
uint64_t newnow_us;
newnow_us = tv->tv_sec * 1000000L;
newnow_us += tv->tv_usec;
__timedelta_us = newnow_us - now_us;
}
return 0;
}
extern "C" int _isatty (int file) {
......
/* Simple demonstration of setting and printing the current time */
/* The initial time will need to come from an RTC, NTP, or user */
/* Released to the public domain by Earle F. Philhower, III <earlephilhower@yahoo.com> */
#include <time.h>
#include <sys/time.h>
void setup() {
Serial.begin(115200);
delay(5000);
struct timeval tv;
tv.tv_sec = 1611198855; // Jan 21, 2021 3:14:15AM ...RPi Pico Release;
tv.tv_usec = 0;
settimeofday(&tv, nullptr);
}
void loop() {
time_t now;
struct tm *info;
char buff[80];
time(&now);
strftime(buff, sizeof(buff), "%c", localtime(&now));
Serial.println(buff);
delay(1000);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment