Blame time/asctime.c

Packit Service 82fcde
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include "../locale/localeinfo.h"
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <limits.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <time.h>
Packit Service 82fcde
Packit Service 82fcde
/* This is defined in locale/C-time.c in the GNU libc.  */
Packit Service 82fcde
extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
Packit Service 82fcde
#define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
Packit Service 82fcde
#define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
Packit Service 82fcde
Packit Service 82fcde
static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
Packit Service 82fcde
static char result[		 3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
static char *
Packit Service 82fcde
asctime_internal (const struct tm *tp, char *buf, size_t buflen)
Packit Service 82fcde
{
Packit Service 82fcde
  if (tp == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return NULL;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* We limit the size of the year which can be printed.  Using the %d
Packit Service 82fcde
     format specifier used the addition of 1900 would overflow the
Packit Service 82fcde
     number and a negative vaue is printed.  For some architectures we
Packit Service 82fcde
     could in theory use %ld or an evern larger integer format but
Packit Service 82fcde
     this would mean the output needs more space.  This would not be a
Packit Service 82fcde
     problem if the 'asctime_r' interface would be defined sanely and
Packit Service 82fcde
     a buffer size would be passed.  */
Packit Service 82fcde
  if (__glibc_unlikely (tp->tm_year > INT_MAX - 1900))
Packit Service 82fcde
    {
Packit Service 82fcde
    eoverflow:
Packit Service 82fcde
      __set_errno (EOVERFLOW);
Packit Service 82fcde
      return NULL;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  int n = __snprintf (buf, buflen, format,
Packit Service 82fcde
		      (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
Packit Service 82fcde
		       "???" : ab_day_name (tp->tm_wday)),
Packit Service 82fcde
		      (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
Packit Service 82fcde
		       "???" : ab_month_name (tp->tm_mon)),
Packit Service 82fcde
		      tp->tm_mday, tp->tm_hour, tp->tm_min,
Packit Service 82fcde
		      tp->tm_sec, 1900 + tp->tm_year);
Packit Service 82fcde
  if (n < 0)
Packit Service 82fcde
    return NULL;
Packit Service 82fcde
  if (n >= buflen)
Packit Service 82fcde
    goto eoverflow;
Packit Service 82fcde
Packit Service 82fcde
  return buf;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* Like asctime, but write result to the user supplied buffer.  The
Packit Service 82fcde
   buffer is only guaranteed to be 26 bytes in length.  */
Packit Service 82fcde
char *
Packit Service 82fcde
__asctime_r (const struct tm *tp, char *buf)
Packit Service 82fcde
{
Packit Service 82fcde
  return asctime_internal (tp, buf, 26);
Packit Service 82fcde
}
Packit Service 82fcde
weak_alias (__asctime_r, asctime_r)
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
Packit Service 82fcde
   which is the representation of TP in that form.  */
Packit Service 82fcde
char *
Packit Service 82fcde
asctime (const struct tm *tp)
Packit Service 82fcde
{
Packit Service 82fcde
  return asctime_internal (tp, result, sizeof (result));
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_def (asctime)