Fixed error in gmtime example

gmtime and gmtime_r take a time_t pointer, so have to store the value
of time(NULL) on the stack.

Signed-off-by: Ole Herman Schumacher Elgesem <oleherman93@gmail.com>
This commit is contained in:
Ole Herman Schumacher Elgesem
2018-08-28 11:08:52 -07:00
parent 1d202dd7cd
commit 00c552fe2f

View File

@@ -1,12 +1,14 @@
// BAD: using gmtime
int is_morning_bad() {
struct tm *now = gmtime(time(NULL));
const time_t now_seconds = time(NULL);
struct tm *now = gmtime(&now_seconds);
return (now->tm_hour < 12);
}
// GOOD: using gmtime_r
int is_morning_good() {
const time_t now_seconds = time(NULL);
struct tm now;
gmtime_r(time(NULL), &now);
gmtime_r(&now_seconds, &now);
return (now.tm_hour < 12);
}