zephyr/samples/posix/gettimeofday/src/main.c
Peter Bigot 1d026703c4 samples: posix: gettimeofday: incorporate libc time API tests
Use time(), localtime_r(), and asctime() to confirm that the
declarations required for these (newlib) libc functions are made
available in CONFIG_POSIX context.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
2020-09-04 14:46:43 +02:00

35 lines
655 B
C

/*
* Copyright (c) 2019 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include <unistd.h>
int main(void)
{
struct timeval tv;
while (1) {
int res = gettimeofday(&tv, NULL);
time_t now = time(NULL);
struct tm tm;
localtime_r(&now, &tm);
if (res < 0) {
printf("Error in gettimeofday(): %d\n", errno);
return 1;
}
printf("gettimeofday(): HI(tv_sec)=%d, LO(tv_sec)=%d, "
"tv_usec=%d\n\t%s\n", (unsigned int)(tv.tv_sec >> 32),
(unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec,
asctime(&tm));
sleep(1);
}
}