]> pd.if.org Git - pdclib/blob - functions/time/strftime.c
Started implementation of <time.h>
[pdclib] / functions / time / strftime.c
1 /* strftime( char * restrict, size_t, const char * restrict, const struct tm * restrict )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <time.h>
8
9 #ifndef REGTEST
10
11 size_t strftime( char * _PDCLIB_restrict s, size_t maxsize, const char * _PDCLIB_restrict format, const struct tm * _PDCLIB_restrict timeptr )
12 {
13     /*
14     If the total number of resulting characters including the terminating null character is not
15     more than maxsize, the strftime function returns the number of characters placed
16     into the array pointed to by s not including the terminating null character.
17     (i.e., < maxsize)
18     */
19     size_t rc = 0;
20
21     while ( rc < maxsize )
22     {
23         if ( *format != '%' )
24         {
25             if ( ( *s++ = *format++ ) == '\0' )
26             {
27                 return rc;
28             }
29             else
30             {
31                 ++rc;
32             }
33         }
34         else
35         {
36             char flag = 0;
37             switch ( *++format )
38             {
39                 case 'E':
40                 case 'O':
41                     flag = *format++;
42                     break;
43                 default:
44                     /* EMPTY */
45                     break;
46             }
47             switch( *format++ )
48             {
49                 case 'a':
50                     /* tm_wday abbreviated */
51                     break;
52                 case 'A':
53                     /* tm_wday full */
54                     break;
55                 case 'b':
56                 case 'h':
57                     /* tm_mon abbreviated */
58                     break;
59                 case 'B':
60                     /* tm_mon full */
61                     break;
62                 case 'c':
63                     /* locale's date / time representation, %a %b %e %T %Y for C locale */
64                     /* 'E' for locale's alternative representation */
65                     break;
66                 case 'C':
67                     /* tm_year divided by 100, truncated to decimal (00-99) */
68                     /* 'E' for base year (period) in locale's alternative representation */
69                     break;
70                 case 'd':
71                     /* tm_mday as decimal (01-31) */
72                     /* 'O' for locale's alternative numeric symbols */
73                     break;
74                 case 'D':
75                     /* %m/%d/%y */
76                     break;
77                 case 'e':
78                     /* tm_mday as decimal ( 1-31) */
79                     /* 'O' for locale's alternative numeric symbols */
80                     break;
81                 case 'F':
82                     /* %Y-%m-%d */
83                     break;
84                 case 'g':
85                     /* last 2 digits of the week-based year as decimal (00-99) */
86                     break;
87                 case 'G':
88                     /* week-based year as decimal (e.g. 1997) */
89                     break;
90                 case 'H':
91                     /* tm_hour as 24h decimal (00-23) */
92                     /* 'O' for locale's alternative numeric symbols */
93                     break;
94                 case 'I':
95                     /* tm_hour as 12h decimal (01-12) */
96                     /* 'O' for locale's alternative numeric symbols */
97                     break;
98                 case 'j':
99                     /* tm_yday as decimal (001-366) */
100                     break;
101                 case 'm':
102                     /* tm_mon as decimal (01-12) */
103                     /* 'O' for locale's alternative numeric symbols */
104                     break;
105                 case 'M':
106                     /* tm_min as decimal (00-59) */
107                     /* 'O' for locale's alternative numeric symbols */
108                     break;
109                 case 'n':
110                     /* newline */
111                     break;
112                 case 'p':
113                     /* tm_hour locale's AM/PM designations */
114                     break;
115                 case 'r':
116                     /* tm_hour / tm_min / tm_sec as locale's 12-hour clock time, %I:%M:%S %p for C locale */
117                     break;
118                 case 'R':
119                     /* %H:%M */
120                     break;
121                 case 'S':
122                     /* tm_sec as decimal (00-60) */
123                     /* 'O' for locale's alternative numeric symbols */
124                     break;
125                 case 't':
126                     /* tabulator */
127                     break;
128                 case 'T':
129                     /* %H:%M:%S */
130                     break;
131                 case 'u':
132                     /* tm_wday as decimal (1-7) with Monday == 1 */
133                     /* 'O' for locale's alternative numeric symbols */
134                     break;
135                 case 'U':
136                     /* week number of the year (first Sunday as the first day of week 1) as decimal (00-53) */
137                     /* 'O' for locale's alternative numeric symbols */
138                     break;
139                 case 'V':
140                     /* week number as decimal (01-53) */
141                     /* 'O' for locale's alternative numeric symbols */
142                     break;
143                 case 'w':
144                     /* tm_wday as decimal number (0-6) with Sunday == 0 */
145                     /* 'O' for locale's alternative numeric symbols */
146                     break;
147                 case 'W':
148                     /* week number of the year (first Monday as the first day of week 1) as decimal (00-53) */
149                     /* 'O' for locale's alternative numeric symbols */
150                     break;
151                 case 'x':
152                     /* locale's date representation, %m/%d/%y for C locale */
153                     /* 'E' for locale's alternative representation */
154                     break;
155                 case 'X':
156                     /* locale's time representation, %T for C locale */
157                     /* 'E' for locale's alternative representation */
158                     break;
159                 case 'y':
160                     /* last 2 digits of tm_year as decimal (00-99) */
161                     /* 'E' for offset from %EC (year only) in locale's alternative representation */
162                     /* 'O' for locale's alternative numeric symbols */
163                     break;
164                 case 'Y':
165                     /* tm_year as decimal (e.g. 1997) */
166                     /* 'E' for locale's alternative representation */
167                     break;
168                 case 'z':
169                     /* tm_isdst / UTC offset in ISO8601 format (e.g. -0430 meaning 4 hours 30 minutes behind Greenwich), or no characters */
170                     break;
171                 case 'Z':
172                     /* tm_isdst / locale's time zone name or abbreviation, or no characters */
173                     break;
174                 case '%':
175                     /* '%' character */
176                     break;
177             }
178         }
179     }
180
181     return 0;
182 }
183
184 #endif
185
186 #ifdef TEST
187
188 #include "_PDCLIB_test.h"
189
190 int main( void )
191 {
192     TESTCASE( NO_TESTDRIVER );
193     return TEST_RESULTS;
194 }
195
196 #endif