Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
asctime_r.c
Go to the documentation of this file.
1 #define _GNU_SOURCE
2 #include <assert.h>
3 #include <langinfo.h>
4 #include <stdio.h>
5 #include <time.h>
6 
7 char* asctime_r(const struct tm* restrict tm, char* restrict buf)
8 {
9  if(snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", nl_langinfo(ABDAY_1 + tm->tm_wday),
11  1900 + tm->tm_year) >= 26)
12  {
13  /* ISO C requires us to use the above format string,
14  * even if it will not fit in the buffer. Thus asctime_r
15  * is _supposed_ to crash if the fields in tm are too large.
16  * We follow this behavior and crash "gracefully" to warn
17  * application developers that they may not be so lucky
18  * on other implementations (e.g. stack smashing..).
19  */
20  assert(0 && "tm fields are too large for buffer");
21  }
22  return buf;
23 }
int tm_hour
Definition: time.h:45
int tm_sec
Definition: time.h:43
int tm_mday
Definition: time.h:46
char * nl_langinfo(nl_item item)
Definition: langinfo.c:64
#define assert(x)
Definition: assert.h:11
#define ABDAY_1
Definition: langinfo.h:27
int tm_mon
Definition: time.h:47
int tm_year
Definition: time.h:48
int tm_wday
Definition: time.h:49
Definition: time.h:41
char * asctime_r(const struct tm *restrict tm, char *restrict buf)
Definition: asctime_r.c:7
#define ABMON_1
Definition: langinfo.h:43
int tm_min
Definition: time.h:44