Back to index

glibc  2.9
asctime.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,1993,1995-1997,2000,2002,2005
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include "../locale/localeinfo.h"
00021 #include <errno.h>
00022 #include <limits.h>
00023 #include <stdio.h>
00024 #include <time.h>
00025 
00026 /* This is defined in locale/C-time.c in the GNU libc.  */
00027 extern const struct locale_data _nl_C_LC_TIME attribute_hidden;
00028 #define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
00029 #define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
00030 
00031 static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
00032 static char result[           3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
00033 
00034 
00035 static char *
00036 asctime_internal (const struct tm *tp, char *buf, size_t buflen)
00037 {
00038   if (tp == NULL)
00039     {
00040       __set_errno (EINVAL);
00041       return NULL;
00042     }
00043 
00044   /* We limit the size of the year which can be printed.  Using the %d
00045      format specifier used the addition of 1900 would overflow the
00046      number and a negative vaue is printed.  For some architectures we
00047      could in theory use %ld or an evern larger integer format but
00048      this would mean the output needs more space.  This would not be a
00049      problem if the 'asctime_r' interface would be defined sanely and
00050      a buffer size would be passed.  */
00051   if (__builtin_expect (tp->tm_year > INT_MAX - 1900, 0))
00052     {
00053     eoverflow:
00054       __set_errno (EOVERFLOW);
00055       return NULL;
00056     }
00057 
00058   int n = __snprintf (buf, buflen, format,
00059                     (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
00060                      "???" : ab_day_name (tp->tm_wday)),
00061                     (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
00062                      "???" : ab_month_name (tp->tm_mon)),
00063                     tp->tm_mday, tp->tm_hour, tp->tm_min,
00064                     tp->tm_sec, 1900 + tp->tm_year);
00065   if (n < 0)
00066     return NULL;
00067   if (n >= buflen)
00068     goto eoverflow;
00069 
00070   return buf;
00071 }
00072 
00073 
00074 /* Like asctime, but write result to the user supplied buffer.  The
00075    buffer is only guaranteed to be 26 bytes in length.  */
00076 char *
00077 __asctime_r (const struct tm *tp, char *buf)
00078 {
00079   return asctime_internal (tp, buf, 26);
00080 }
00081 weak_alias (__asctime_r, asctime_r)
00082 
00083 
00084 /* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
00085    which is the representation of TP in that form.  */
00086 char *
00087 asctime (const struct tm *tp)
00088 {
00089   return asctime_internal (tp, result, sizeof (result));
00090 }
00091 libc_hidden_def (asctime)