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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [time/] [strftime.c] - Diff between revs 1010 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
/*
/*
 * strftime.c
 * strftime.c
 * Original Author:     G. Haley
 * Original Author:     G. Haley
 *
 *
 * Places characters into the array pointed to by s as controlled by the string
 * Places characters into the array pointed to by s as controlled by the string
 * pointed to by format. If the total number of resulting characters including
 * pointed to by format. If the total number of resulting characters including
 * the terminating null character is not more than maxsize, returns the number
 * the terminating null character is not more than maxsize, returns the number
 * of characters placed into the array pointed to by s (not including the
 * of characters placed into the array pointed to by s (not including the
 * terminating null character); otherwise zero is returned and the contents of
 * terminating null character); otherwise zero is returned and the contents of
 * the array indeterminate.
 * the array indeterminate.
 */
 */
 
 
/*
/*
FUNCTION
FUNCTION
<<strftime>>---flexible calendar time formatter
<<strftime>>---flexible calendar time formatter
 
 
INDEX
INDEX
        strftime
        strftime
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <time.h>
        #include <time.h>
        size_t strftime(char *<[s]>, size_t <[maxsize]>,
        size_t strftime(char *<[s]>, size_t <[maxsize]>,
                        const char *<[format]>, const struct tm *<[timp]>);
                        const char *<[format]>, const struct tm *<[timp]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <time.h>
        #include <time.h>
        size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>)
        size_t strftime(<[s]>, <[maxsize]>, <[format]>, <[timp]>)
        char *<[s]>;
        char *<[s]>;
        size_t <[maxsize]>;
        size_t <[maxsize]>;
        char *<[format]>;
        char *<[format]>;
        struct tm *<[timp]>;
        struct tm *<[timp]>;
 
 
DESCRIPTION
DESCRIPTION
<<strftime>> converts a <<struct tm>> representation of the time (at
<<strftime>> converts a <<struct tm>> representation of the time (at
<[timp]>) into a string, starting at <[s]> and occupying no more than
<[timp]>) into a string, starting at <[s]> and occupying no more than
<[maxsize]> characters.
<[maxsize]> characters.
 
 
You control the format of the output using the string at <[format]>.
You control the format of the output using the string at <[format]>.
<<*<[format]>>> can contain two kinds of specifications: text to be
<<*<[format]>>> can contain two kinds of specifications: text to be
copied literally into the formatted string, and time conversion
copied literally into the formatted string, and time conversion
specifications.  Time conversion specifications are two-character
specifications.  Time conversion specifications are two-character
sequences beginning with `<<%>>' (use `<<%%>>' to include a percent
sequences beginning with `<<%>>' (use `<<%%>>' to include a percent
sign in the output).  Each defined conversion specification selects a
sign in the output).  Each defined conversion specification selects a
field of calendar time data from <<*<[timp]>>>, and converts it to a
field of calendar time data from <<*<[timp]>>>, and converts it to a
string in one of the following ways:
string in one of the following ways:
 
 
o+
o+
o %a
o %a
An abbreviation for the day of the week.
An abbreviation for the day of the week.
 
 
o %A
o %A
The full name for the day of the week.
The full name for the day of the week.
 
 
o %b
o %b
An abbreviation for the month name.
An abbreviation for the month name.
 
 
o %B
o %B
The full name of the month.
The full name of the month.
 
 
o %c
o %c
A string representing the complete date and time, in the form
A string representing the complete date and time, in the form
. Mon Apr 01 13:13:13 1992
. Mon Apr 01 13:13:13 1992
 
 
o %d
o %d
The day of the month, formatted with two digits.
The day of the month, formatted with two digits.
 
 
o %H
o %H
The hour (on a 24-hour clock), formatted with two digits.
The hour (on a 24-hour clock), formatted with two digits.
 
 
o %I
o %I
The hour (on a 12-hour clock), formatted with two digits.
The hour (on a 12-hour clock), formatted with two digits.
 
 
o %j
o %j
The count of days in the year, formatted with three digits
The count of days in the year, formatted with three digits
(from `<<001>>' to `<<366>>').
(from `<<001>>' to `<<366>>').
 
 
o %m
o %m
The month number, formatted with two digits.
The month number, formatted with two digits.
 
 
o %M
o %M
The minute, formatted with two digits.
The minute, formatted with two digits.
 
 
o %p
o %p
Either `<<AM>>' or `<<PM>>' as appropriate.
Either `<<AM>>' or `<<PM>>' as appropriate.
 
 
o %S
o %S
The second, formatted with two digits.
The second, formatted with two digits.
 
 
o %U
o %U
The week number, formatted with two digits (from `<<00>>' to `<<53>>';
The week number, formatted with two digits (from `<<00>>' to `<<53>>';
week number 1 is taken as beginning with the first Sunday in a year).
week number 1 is taken as beginning with the first Sunday in a year).
See also <<%W>>.
See also <<%W>>.
 
 
o %w
o %w
A single digit representing the day of the week: Sunday is day <<0>>.
A single digit representing the day of the week: Sunday is day <<0>>.
 
 
o %W
o %W
Another version of the week number: like `<<%U>>', but counting week 1
Another version of the week number: like `<<%U>>', but counting week 1
as beginning with the first Monday in a year.
as beginning with the first Monday in a year.
 
 
o
o
o %x
o %x
A string representing the complete date, in a format like
A string representing the complete date, in a format like
. Mon Apr 01 1992
. Mon Apr 01 1992
 
 
o %X
o %X
A string representing the full time of day (hours, minutes, and
A string representing the full time of day (hours, minutes, and
seconds), in a format like
seconds), in a format like
. 13:13:13
. 13:13:13
 
 
o %y
o %y
The last two digits of the year.
The last two digits of the year.
 
 
o %Y
o %Y
The full year, formatted with four digits to include the century.
The full year, formatted with four digits to include the century.
 
 
o %Z
o %Z
Defined by ANSI C as eliciting the time zone if available; it is not
Defined by ANSI C as eliciting the time zone if available; it is not
available in this implementation (which accepts `<<%Z>>' but generates
available in this implementation (which accepts `<<%Z>>' but generates
no output for it).
no output for it).
 
 
o %%
o %%
A single character, `<<%>>'.
A single character, `<<%>>'.
o-
o-
 
 
RETURNS
RETURNS
When the formatted time takes up no more than <[maxsize]> characters,
When the formatted time takes up no more than <[maxsize]> characters,
the result is the length of the formatted string.  Otherwise, if the
the result is the length of the formatted string.  Otherwise, if the
formatting operation was abandoned due to lack of room, the result is
formatting operation was abandoned due to lack of room, the result is
<<0>>, and the string starting at <[s]> corresponds to just those
<<0>>, and the string starting at <[s]> corresponds to just those
parts of <<*<[format]>>> that could be completely filled in within the
parts of <<*<[format]>>> that could be completely filled in within the
<[maxsize]> limit.
<[maxsize]> limit.
 
 
PORTABILITY
PORTABILITY
ANSI C requires <<strftime>>, but does not specify the contents of
ANSI C requires <<strftime>>, but does not specify the contents of
<<*<[s]>>> when the formatted string would require more than
<<*<[s]>>> when the formatted string would require more than
<[maxsize]> characters.
<[maxsize]> characters.
 
 
<<strftime>> requires no supporting OS subroutines.
<<strftime>> requires no supporting OS subroutines.
*/
*/
 
 
#include <stddef.h>
#include <stddef.h>
#include <stdio.h>
#include <stdio.h>
#include <time.h>
#include <time.h>
 
 
static _CONST int dname_len[7] =
static _CONST int dname_len[7] =
{6, 6, 7, 9, 8, 6, 8};
{6, 6, 7, 9, 8, 6, 8};
 
 
static _CONST char *_CONST dname[7] =
static _CONST char *_CONST dname[7] =
{"Sunday", "Monday", "Tuesday", "Wednesday",
{"Sunday", "Monday", "Tuesday", "Wednesday",
 "Thursday", "Friday", "Saturday"};
 "Thursday", "Friday", "Saturday"};
 
 
static _CONST int mname_len[12] =
static _CONST int mname_len[12] =
{7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};
{7, 8, 5, 5, 3, 4, 4, 6, 9, 7, 8, 8};
 
 
static _CONST char *_CONST mname[12] =
static _CONST char *_CONST mname[12] =
{"January", "February", "March", "April",
{"January", "February", "March", "April",
 "May", "June", "July", "August", "September", "October", "November",
 "May", "June", "July", "August", "September", "October", "November",
 "December"};
 "December"};
 
 
size_t
size_t
_DEFUN (strftime, (s, maxsize, format, tim_p),
_DEFUN (strftime, (s, maxsize, format, tim_p),
        char *s _AND
        char *s _AND
        size_t maxsize _AND
        size_t maxsize _AND
        _CONST char *format _AND
        _CONST char *format _AND
        _CONST struct tm *tim_p)
        _CONST struct tm *tim_p)
{
{
  size_t count = 0;
  size_t count = 0;
  int i;
  int i;
 
 
  for (;;)
  for (;;)
    {
    {
      while (*format && *format != '%')
      while (*format && *format != '%')
        {
        {
          if (count < maxsize - 1)
          if (count < maxsize - 1)
            s[count++] = *format++;
            s[count++] = *format++;
          else
          else
            return 0;
            return 0;
        }
        }
 
 
      if (*format == '\0')
      if (*format == '\0')
        break;
        break;
 
 
      format++;
      format++;
      switch (*format)
      switch (*format)
        {
        {
        case 'a':
        case 'a':
          for (i = 0; i < 3; i++)
          for (i = 0; i < 3; i++)
            {
            {
              if (count < maxsize - 1)
              if (count < maxsize - 1)
                s[count++] =
                s[count++] =
                  dname[tim_p->tm_wday][i];
                  dname[tim_p->tm_wday][i];
              else
              else
                return 0;
                return 0;
            }
            }
          break;
          break;
        case 'A':
        case 'A':
          for (i = 0; i < dname_len[tim_p->tm_wday]; i++)
          for (i = 0; i < dname_len[tim_p->tm_wday]; i++)
            {
            {
              if (count < maxsize - 1)
              if (count < maxsize - 1)
                s[count++] =
                s[count++] =
                  dname[tim_p->tm_wday][i];
                  dname[tim_p->tm_wday][i];
              else
              else
                return 0;
                return 0;
            }
            }
          break;
          break;
        case 'b':
        case 'b':
          for (i = 0; i < 3; i++)
          for (i = 0; i < 3; i++)
            {
            {
              if (count < maxsize - 1)
              if (count < maxsize - 1)
                s[count++] =
                s[count++] =
                  mname[tim_p->tm_mon][i];
                  mname[tim_p->tm_mon][i];
              else
              else
                return 0;
                return 0;
            }
            }
          break;
          break;
        case 'B':
        case 'B':
          for (i = 0; i < mname_len[tim_p->tm_mon]; i++)
          for (i = 0; i < mname_len[tim_p->tm_mon]; i++)
            {
            {
              if (count < maxsize - 1)
              if (count < maxsize - 1)
                s[count++] =
                s[count++] =
                  mname[tim_p->tm_mon][i];
                  mname[tim_p->tm_mon][i];
              else
              else
                return 0;
                return 0;
            }
            }
          break;
          break;
        case 'c':
        case 'c':
          if (count < maxsize - 24)
          if (count < maxsize - 24)
            {
            {
              for (i = 0; i < 3; i++)
              for (i = 0; i < 3; i++)
                s[count++] =
                s[count++] =
                  dname[tim_p->tm_wday][i];
                  dname[tim_p->tm_wday][i];
              s[count++] = ' ';
              s[count++] = ' ';
              for (i = 0; i < 3; i++)
              for (i = 0; i < 3; i++)
                s[count++] =
                s[count++] =
                  mname[tim_p->tm_mon][i];
                  mname[tim_p->tm_mon][i];
 
 
              sprintf (&s[count],
              sprintf (&s[count],
                       " %.2d %2.2d:%2.2d:%2.2d %.4d",
                       " %.2d %2.2d:%2.2d:%2.2d %.4d",
                       tim_p->tm_mday, tim_p->tm_hour,
                       tim_p->tm_mday, tim_p->tm_hour,
                       tim_p->tm_min,
                       tim_p->tm_min,
                       tim_p->tm_sec, 1900 +
                       tim_p->tm_sec, 1900 +
                       tim_p->tm_year);
                       tim_p->tm_year);
              count += 17;
              count += 17;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'd':
        case 'd':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%.2d",
              sprintf (&s[count], "%.2d",
                       tim_p->tm_mday);
                       tim_p->tm_mday);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'H':
        case 'H':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       tim_p->tm_hour);
                       tim_p->tm_hour);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'I':
        case 'I':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              if (tim_p->tm_hour == 0 ||
              if (tim_p->tm_hour == 0 ||
                  tim_p->tm_hour == 12)
                  tim_p->tm_hour == 12)
                {
                {
                  s[count++] = '1';
                  s[count++] = '1';
                  s[count++] = '2';
                  s[count++] = '2';
                }
                }
              else
              else
                {
                {
                  sprintf (&s[count], "%.2d",
                  sprintf (&s[count], "%.2d",
                           tim_p->tm_hour % 12);
                           tim_p->tm_hour % 12);
                  count += 2;
                  count += 2;
                }
                }
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'j':
        case 'j':
          if (count < maxsize - 3)
          if (count < maxsize - 3)
            {
            {
              sprintf (&s[count], "%.3d",
              sprintf (&s[count], "%.3d",
                       tim_p->tm_yday + 1);
                       tim_p->tm_yday + 1);
              count += 3;
              count += 3;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'm':
        case 'm':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%.2d",
              sprintf (&s[count], "%.2d",
                       tim_p->tm_mon + 1);
                       tim_p->tm_mon + 1);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'M':
        case 'M':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       tim_p->tm_min);
                       tim_p->tm_min);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'p':
        case 'p':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              if (tim_p->tm_hour < 12)
              if (tim_p->tm_hour < 12)
                s[count++] = 'A';
                s[count++] = 'A';
              else
              else
                s[count++] = 'P';
                s[count++] = 'P';
 
 
              s[count++] = 'M';
              s[count++] = 'M';
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'S':
        case 'S':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       tim_p->tm_sec);
                       tim_p->tm_sec);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'U':
        case 'U':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       (tim_p->tm_yday + 7 -
                       (tim_p->tm_yday + 7 -
                        tim_p->tm_wday) / 7);
                        tim_p->tm_wday) / 7);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'w':
        case 'w':
          if (count < maxsize - 1)
          if (count < maxsize - 1)
            {
            {
              sprintf (&s[count], "%1.1d",
              sprintf (&s[count], "%1.1d",
                       tim_p->tm_wday);
                       tim_p->tm_wday);
              count++;
              count++;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'W':
        case 'W':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
              int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       (tim_p->tm_yday + 7 -
                       (tim_p->tm_yday + 7 -
                        wday) / 7);
                        wday) / 7);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'x':
        case 'x':
          if (count < maxsize - 15)
          if (count < maxsize - 15)
            {
            {
              for (i = 0; i < 3; i++)
              for (i = 0; i < 3; i++)
                s[count++] =
                s[count++] =
                  dname[tim_p->tm_wday][i];
                  dname[tim_p->tm_wday][i];
              s[count++] = ' ';
              s[count++] = ' ';
              for (i = 0; i < 3; i++)
              for (i = 0; i < 3; i++)
                s[count++] =
                s[count++] =
                  mname[tim_p->tm_mon][i];
                  mname[tim_p->tm_mon][i];
 
 
              sprintf (&s[count],
              sprintf (&s[count],
                       " %.2d %.4d", tim_p->tm_mday,
                       " %.2d %.4d", tim_p->tm_mday,
                       1900 + tim_p->tm_year);
                       1900 + tim_p->tm_year);
              count += 8;
              count += 8;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'X':
        case 'X':
          if (count < maxsize - 8)
          if (count < maxsize - 8)
            {
            {
              sprintf (&s[count],
              sprintf (&s[count],
                       "%2.2d:%2.2d:%2.2d",
                       "%2.2d:%2.2d:%2.2d",
                       tim_p->tm_hour, tim_p->tm_min,
                       tim_p->tm_hour, tim_p->tm_min,
                       tim_p->tm_sec);
                       tim_p->tm_sec);
              count += 8;
              count += 8;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'y':
        case 'y':
          if (count < maxsize - 2)
          if (count < maxsize - 2)
            {
            {
              /* The year could be greater than 100, so we need the value
              /* The year could be greater than 100, so we need the value
                 modulo 100.  The year could be negative, so we need to
                 modulo 100.  The year could be negative, so we need to
                 correct for a possible negative remainder.  */
                 correct for a possible negative remainder.  */
              sprintf (&s[count], "%2.2d",
              sprintf (&s[count], "%2.2d",
                       (tim_p->tm_year % 100 + 100) % 100);
                       (tim_p->tm_year % 100 + 100) % 100);
              count += 2;
              count += 2;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'Y':
        case 'Y':
          if (count < maxsize - 4)
          if (count < maxsize - 4)
            {
            {
              sprintf (&s[count], "%.4d",
              sprintf (&s[count], "%.4d",
                       1900 + tim_p->tm_year);
                       1900 + tim_p->tm_year);
              count += 4;
              count += 4;
            }
            }
          else
          else
            return 0;
            return 0;
          break;
          break;
        case 'Z':
        case 'Z':
          break;
          break;
        case '%':
        case '%':
          if (count < maxsize - 1)
          if (count < maxsize - 1)
            s[count++] = '%';
            s[count++] = '%';
          else
          else
            return 0;
            return 0;
          break;
          break;
        }
        }
      if (*format)
      if (*format)
        format++;
        format++;
      else
      else
        break;
        break;
    }
    }
  s[count] = '\0';
  s[count] = '\0';
 
 
  return count;
  return count;
}
}
 
 

powered by: WebSVN 2.1.0

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