Skip to content

Commit 00c552f

Browse files
committed
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>
1 parent 1d202dd commit 00c552f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// BAD: using gmtime
22
int is_morning_bad() {
3-
struct tm *now = gmtime(time(NULL));
3+
const time_t now_seconds = time(NULL);
4+
struct tm *now = gmtime(&now_seconds);
45
return (now->tm_hour < 12);
56
}
67

78
// GOOD: using gmtime_r
89
int is_morning_good() {
10+
const time_t now_seconds = time(NULL);
911
struct tm now;
10-
gmtime_r(time(NULL), &now);
12+
gmtime_r(&now_seconds, &now);
1113
return (now.tm_hour < 12);
1214
}

0 commit comments

Comments
 (0)