OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [time/] [strftime.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
/*
2
 * strftime.c
3
 * Original Author:     G. Haley
4
 *
5
 * Places characters into the array pointed to by s as controlled by the string
6
 * pointed to by format. If the total number of resulting characters including
7
 * the terminating null character is not more than maxsize, returns the number
8
 * of characters placed into the array pointed to by s (not including the
9
 * terminating null character); otherwise zero is returned and the contents of
10
 * the array indeterminate.
11
 */
12
 
13
/*
14
FUNCTION
15
<<strftime>>---flexible calendar time formatter
16
 
17
INDEX
18
        strftime
19
 
20
ANSI_SYNOPSIS
21
        #include <time.h>
22
        size_t strftime(char *<[s]>, size_t <[maxsize]>,
23
                        const char *<[format]>, const struct tm *<[timp]>);
24
 
25
TRAD_SYNOPSIS
26
        #include <time.h>
27
        size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>)
28
        char *<[s]>;
29
        size_t <[maxsize]>;
30
        char *<[format]>;
31
        struct tm *<[timp]>;
32
 
33
DESCRIPTION
34
<<strftime>> converts a <<struct tm>> representation of the time (at
35
<[timp]>) into a string, starting at <[s]> and occupying no more than
36
<[maxsize]> characters.
37
 
38
You control the format of the output using the string at <[format]>.
39
<<*<[format]>>> can contain two kinds of specifications: text to be
40
copied literally into the formatted string, and time conversion
41
specifications.  Time conversion specifications are two-character
42
sequences beginning with `<<%>>' (use `<<%%>>' to include a percent
43
sign in the output).  Each defined conversion specification selects a
44
field of calendar time data from <<*<[timp]>>>, and converts it to a
45
string in one of the following ways:
46
 
47
o+
48
o %a
49
An abbreviation for the day of the week.
50
 
51
o %A
52
The full name for the day of the week.
53
 
54
o %b
55
An abbreviation for the month name.
56
 
57
o %B
58
The full name of the month.
59
 
60
o %c
61
A string representing the complete date and time, in the form
62
. Mon Apr 01 13:13:13 1992
63
 
64
o %d
65
The day of the month, formatted with two digits.
66
 
67
o %H
68
The hour (on a 24-hour clock), formatted with two digits.
69
 
70
o %I
71
The hour (on a 12-hour clock), formatted with two digits.
72
 
73
o %j
74
The count of days in the year, formatted with three digits
75
(from `<<001>>' to `<<366>>').
76
 
77
o %m
78
The month number, formatted with two digits.
79
 
80
o %M
81
The minute, formatted with two digits.
82
 
83
o %p
84
Either `<<AM>>' or `<<PM>>' as appropriate.
85
 
86
o %S
87
The second, formatted with two digits.
88
 
89
o %U
90
The week number, formatted with two digits (from `<<00>>' to `<<53>>';
91
week number 1 is taken as beginning with the first Sunday in a year).
92
See also <<%W>>.
93
 
94
o %w
95
A single digit representing the day of the week: Sunday is day <<0>>.
96
 
97
o %W
98
Another version of the week number: like `<<%U>>', but counting week 1
99
as beginning with the first Monday in a year.
100
 
101
o
102
o %x
103
A string representing the complete date, in a format like
104
. Mon Apr 01 1992
105
 
106
o %X
107
A string representing the full time of day (hours, minutes, and
108
seconds), in a format like
109
. 13:13:13
110
 
111
o %y
112
The last two digits of the year.
113
 
114
o %Y
115
The full year, formatted with four digits to include the century.
116
 
117
o %Z
118
Defined by ANSI C as eliciting the time zone if available; it is not
119
available in this implementation (which accepts `<<%Z>>' but generates
120
no output for it).
121
 
122
o %%
123
A single character, `<<%>>'.
124
o-
125
 
126
RETURNS
127
When the formatted time takes up no more than <[maxsize]> characters,
128
the result is the length of the formatted string.  Otherwise, if the
129
formatting operation was abandoned due to lack of room, the result is
130
<<0>>, and the string starting at <[s]> corresponds to just those
131
parts of <<*<[format]>>> that could be completely filled in within the
132
<[maxsize]> limit.
133
 
134
PORTABILITY
135
ANSI C requires <<strftime>>, but does not specify the contents of
136
<<*<[s]>>> when the formatted string would require more than
137
<[maxsize]> characters.
138
 
139
<<strftime>> requires no supporting OS subroutines.
140
*/
141
 
142
#include <stddef.h>
143
#include <stdio.h>
144
#include <time.h>
145
 
146
static _CONST int dname_len[7] =
147
{6, 6, 7, 9, 8, 6, 8};
148
 
149
static _CONST char *_CONST dname[7] =
150
{"Sunday", "Monday", "Tuesday", "Wednesday",
151
 "Thursday", "Friday", "Saturday"};
152
 
153
static _CONST int mname_len[12] =
154
{7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};
155
 
156
static _CONST char *_CONST mname[12] =
157
{"January", "February", "March", "April",
158
 "May", "June", "July", "August", "September", "October", "November",
159
 "December"};
160
 
161
size_t
162
_DEFUN (strftime, (s, maxsize, format, tim_p),
163
        char *s _AND
164
        size_t maxsize _AND
165
        _CONST char *format _AND
166
        _CONST struct tm *tim_p)
167
{
168
  size_t count = 0;
169
  int i;
170
 
171
  for (;;)
172
    {
173
      while (*format && *format != '%')
174
        {
175
          if (count < maxsize - 1)
176
            s[count++] = *format++;
177
          else
178
            return 0;
179
        }
180
 
181
      if (*format == '\0')
182
        break;
183
 
184
      format++;
185
      switch (*format)
186
        {
187
        case 'a':
188
          for (i = 0; i < 3; i++)
189
            {
190
              if (count < maxsize - 1)
191
                s[count++] =
192
                  dname[tim_p->tm_wday][i];
193
              else
194
                return 0;
195
            }
196
          break;
197
        case 'A':
198
          for (i = 0; i < dname_len[tim_p->tm_wday]; i++)
199
            {
200
              if (count < maxsize - 1)
201
                s[count++] =
202
                  dname[tim_p->tm_wday][i];
203
              else
204
                return 0;
205
            }
206
          break;
207
        case 'b':
208
          for (i = 0; i < 3; i++)
209
            {
210
              if (count < maxsize - 1)
211
                s[count++] =
212
                  mname[tim_p->tm_mon][i];
213
              else
214
                return 0;
215
            }
216
          break;
217
        case 'B':
218
          for (i = 0; i < mname_len[tim_p->tm_mon]; i++)
219
            {
220
              if (count < maxsize - 1)
221
                s[count++] =
222
                  mname[tim_p->tm_mon][i];
223
              else
224
                return 0;
225
            }
226
          break;
227
        case 'c':
228
          if (count < maxsize - 24)
229
            {
230
              for (i = 0; i < 3; i++)
231
                s[count++] =
232
                  dname[tim_p->tm_wday][i];
233
              s[count++] = ' ';
234
              for (i = 0; i < 3; i++)
235
                s[count++] =
236
                  mname[tim_p->tm_mon][i];
237
 
238
              sprintf (&s[count],
239
                       " %.2d %2.2d:%2.2d:%2.2d %.4d",
240
                       tim_p->tm_mday, tim_p->tm_hour,
241
                       tim_p->tm_min,
242
                       tim_p->tm_sec, 1900 +
243
                       tim_p->tm_year);
244
              count += 17;
245
            }
246
          else
247
            return 0;
248
          break;
249
        case 'd':
250
          if (count < maxsize - 2)
251
            {
252
              sprintf (&s[count], "%.2d",
253
                       tim_p->tm_mday);
254
              count += 2;
255
            }
256
          else
257
            return 0;
258
          break;
259
        case 'H':
260
          if (count < maxsize - 2)
261
            {
262
              sprintf (&s[count], "%2.2d",
263
                       tim_p->tm_hour);
264
              count += 2;
265
            }
266
          else
267
            return 0;
268
          break;
269
        case 'I':
270
          if (count < maxsize - 2)
271
            {
272
              if (tim_p->tm_hour == 0 ||
273
                  tim_p->tm_hour == 12)
274
                {
275
                  s[count++] = '1';
276
                  s[count++] = '2';
277
                }
278
              else
279
                {
280
                  sprintf (&s[count], "%.2d",
281
                           tim_p->tm_hour % 12);
282
                  count += 2;
283
                }
284
            }
285
          else
286
            return 0;
287
          break;
288
        case 'j':
289
          if (count < maxsize - 3)
290
            {
291
              sprintf (&s[count], "%.3d",
292
                       tim_p->tm_yday + 1);
293
              count += 3;
294
            }
295
          else
296
            return 0;
297
          break;
298
        case 'm':
299
          if (count < maxsize - 2)
300
            {
301
              sprintf (&s[count], "%.2d",
302
                       tim_p->tm_mon + 1);
303
              count += 2;
304
            }
305
          else
306
            return 0;
307
          break;
308
        case 'M':
309
          if (count < maxsize - 2)
310
            {
311
              sprintf (&s[count], "%2.2d",
312
                       tim_p->tm_min);
313
              count += 2;
314
            }
315
          else
316
            return 0;
317
          break;
318
        case 'p':
319
          if (count < maxsize - 2)
320
            {
321
              if (tim_p->tm_hour < 12)
322
                s[count++] = 'A';
323
              else
324
                s[count++] = 'P';
325
 
326
              s[count++] = 'M';
327
            }
328
          else
329
            return 0;
330
          break;
331
        case 'S':
332
          if (count < maxsize - 2)
333
            {
334
              sprintf (&s[count], "%2.2d",
335
                       tim_p->tm_sec);
336
              count += 2;
337
            }
338
          else
339
            return 0;
340
          break;
341
        case 'U':
342
          if (count < maxsize - 2)
343
            {
344
              sprintf (&s[count], "%2.2d",
345
                       (tim_p->tm_yday + 7 -
346
                        tim_p->tm_wday) / 7);
347
              count += 2;
348
            }
349
          else
350
            return 0;
351
          break;
352
        case 'w':
353
          if (count < maxsize - 1)
354
            {
355
              sprintf (&s[count], "%1.1d",
356
                       tim_p->tm_wday);
357
              count++;
358
            }
359
          else
360
            return 0;
361
          break;
362
        case 'W':
363
          if (count < maxsize - 2)
364
            {
365
              int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
366
              sprintf (&s[count], "%2.2d",
367
                       (tim_p->tm_yday + 7 -
368
                        wday) / 7);
369
              count += 2;
370
            }
371
          else
372
            return 0;
373
          break;
374
        case 'x':
375
          if (count < maxsize - 15)
376
            {
377
              for (i = 0; i < 3; i++)
378
                s[count++] =
379
                  dname[tim_p->tm_wday][i];
380
              s[count++] = ' ';
381
              for (i = 0; i < 3; i++)
382
                s[count++] =
383
                  mname[tim_p->tm_mon][i];
384
 
385
              sprintf (&s[count],
386
                       " %.2d %.4d", tim_p->tm_mday,
387
                       1900 + tim_p->tm_year);
388
              count += 8;
389
            }
390
          else
391
            return 0;
392
          break;
393
        case 'X':
394
          if (count < maxsize - 8)
395
            {
396
              sprintf (&s[count],
397
                       "%2.2d:%2.2d:%2.2d",
398
                       tim_p->tm_hour, tim_p->tm_min,
399
                       tim_p->tm_sec);
400
              count += 8;
401
            }
402
          else
403
            return 0;
404
          break;
405
        case 'y':
406
          if (count < maxsize - 2)
407
            {
408
              /* The year could be greater than 100, so we need the value
409
                 modulo 100.  The year could be negative, so we need to
410
                 correct for a possible negative remainder.  */
411
              sprintf (&s[count], "%2.2d",
412
                       (tim_p->tm_year % 100 + 100) % 100);
413
              count += 2;
414
            }
415
          else
416
            return 0;
417
          break;
418
        case 'Y':
419
          if (count < maxsize - 4)
420
            {
421
              sprintf (&s[count], "%.4d",
422
                       1900 + tim_p->tm_year);
423
              count += 4;
424
            }
425
          else
426
            return 0;
427
          break;
428
        case 'Z':
429
          break;
430
        case '%':
431
          if (count < maxsize - 1)
432
            s[count++] = '%';
433
          else
434
            return 0;
435
          break;
436
        }
437
      if (*format)
438
        format++;
439
      else
440
        break;
441
    }
442
  s[count] = '\0';
443
 
444
  return count;
445
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.