Skip to content

Commit

Permalink
Add time support (time, gettimeofday, etc.) (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
earlephilhower committed May 16, 2021
1 parent 0550fe7 commit af8b548
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
25 changes: 20 additions & 5 deletions cores/rp2040/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions libraries/rp2040/examples/Time/Time.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* 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);
}

0 comments on commit af8b548

Please sign in to comment.