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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 39 lampret
/*
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
              sprintf (&s[count], "%2.2d",
366
                       (tim_p->tm_yday + ((8 -
367
                                           tim_p->tm_wday) % 7)) / 7);
368
              count += 2;
369
            }
370
          else
371
            return 0;
372
          break;
373
        case 'x':
374
          if (count < maxsize - 15)
375
            {
376
              for (i = 0; i < 3; i++)
377
                s[count++] =
378
                  dname[tim_p->tm_wday][i];
379
              s[count++] = ' ';
380
              for (i = 0; i < 3; i++)
381
                s[count++] =
382
                  mname[tim_p->tm_mon][i];
383
 
384
              sprintf (&s[count],
385
                       " %.2d %.4d", tim_p->tm_mday,
386
                       1900 + tim_p->tm_year);
387
              count += 8;
388
            }
389
          else
390
            return 0;
391
          break;
392
        case 'X':
393
          if (count < maxsize - 8)
394
            {
395
              sprintf (&s[count],
396
                       "%2.2d:%2.2d:%2.2d",
397
                       tim_p->tm_hour, tim_p->tm_min,
398
                       tim_p->tm_sec);
399
              count += 8;
400
            }
401
          else
402
            return 0;
403
          break;
404
        case 'y':
405
          if (count < maxsize - 2)
406
            {
407 56 joel
              /* The year could be greater than 100, so we need the value
408
                 modulo 100.  The year could be negative, so we need to
409
                 correct for a possible negative remainder.  */
410 39 lampret
              sprintf (&s[count], "%2.2d",
411 56 joel
                       (tim_p->tm_year % 100 + 100) % 100);
412 39 lampret
              count += 2;
413
            }
414
          else
415
            return 0;
416
          break;
417
        case 'Y':
418
          if (count < maxsize - 4)
419
            {
420
              sprintf (&s[count], "%.4d",
421
                       1900 + tim_p->tm_year);
422
              count += 4;
423
            }
424
          else
425
            return 0;
426
          break;
427
        case 'Z':
428
          break;
429
        case '%':
430
          if (count < maxsize - 1)
431
            s[count++] = '%';
432
          else
433
            return 0;
434
          break;
435
        }
436
      if (*format)
437
        format++;
438
      else
439
        break;
440
    }
441
  s[count] = '\0';
442
 
443
  return count;
444
}

powered by: WebSVN 2.1.0

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