Skip to content

Commit

Permalink
samples: posix: gettimeofday: incorporate libc time API tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pabigot authored and carlescufi committed Sep 4, 2020
1 parent 3cb353c commit 1d02670
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions samples/posix/gettimeofday/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ Overview
********

This sample application demonstrates using the POSIX gettimeofday()
function to display the absolute wall clock time every second. At
system startup, the current time is queried using the SNTP networking
protocol, enabled by setting the :option:`CONFIG_NET_CONFIG_CLOCK_SNTP_INIT`
and :option:`CONFIG_NET_CONFIG_SNTP_INIT_SERVER` options.
function to display the absolute wall clock time and local time every
second. At system startup, the current time is queried using the SNTP
networking protocol, enabled by setting the
:option:`CONFIG_NET_CONFIG_CLOCK_SNTP_INIT` and
:option:`CONFIG_NET_CONFIG_SNTP_INIT_SERVER` options.

Requirements
************
Expand Down
8 changes: 6 additions & 2 deletions samples/posix/gettimeofday/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ int main(void)

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", (unsigned int)(tv.tv_sec >> 32),
(unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec);
"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);
}
}

0 comments on commit 1d02670

Please sign in to comment.