]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/asctime.c
Add support for XPG7 testing.
[thirdparty/glibc.git] / time / asctime.c
CommitLineData
f095bb72 1/* Copyright (C) 1991,1993,1995-1997,2000,2002,2005,2010
576c8451 2 Free Software Foundation, Inc.
fd26970f
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
fd26970f
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
fd26970f 14
41bdb6e2
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
28f540f4 19
933e73fa 20#include "../locale/localeinfo.h"
28f540f4 21#include <errno.h>
576c8451 22#include <limits.h>
28f540f4
RM
23#include <stdio.h>
24#include <time.h>
25
762a2918 26/* This is defined in locale/C-time.c in the GNU libc. */
f095bb72 27extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
41d998a6
GM
28#define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
29#define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
28f540f4 30
23396375 31static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
f095bb72 32static char result[ 3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
23396375 33
28f540f4 34
ce982312
UD
35static char *
36asctime_internal (const struct tm *tp, char *buf, size_t buflen)
23396375 37{
28f540f4
RM
38 if (tp == NULL)
39 {
c4029823 40 __set_errno (EINVAL);
28f540f4
RM
41 return NULL;
42 }
23396375 43
576c8451
UD
44 /* We limit the size of the year which can be printed. Using the %d
45 format specifier used the addition of 1900 would overflow the
46 number and a negative vaue is printed. For some architectures we
47 could in theory use %ld or an evern larger integer format but
48 this would mean the output needs more space. This would not be a
49 problem if the 'asctime_r' interface would be defined sanely and
50 a buffer size would be passed. */
51 if (__builtin_expect (tp->tm_year > INT_MAX - 1900, 0))
52 {
ce982312 53 eoverflow:
576c8451
UD
54 __set_errno (EOVERFLOW);
55 return NULL;
56 }
57
daa22612
UD
58 int n = __snprintf (buf, buflen, format,
59 (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
60 "???" : ab_day_name (tp->tm_wday)),
61 (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
62 "???" : ab_month_name (tp->tm_mon)),
63 tp->tm_mday, tp->tm_hour, tp->tm_min,
64 tp->tm_sec, 1900 + tp->tm_year);
ce982312 65 if (n < 0)
28f540f4 66 return NULL;
ce982312
UD
67 if (n >= buflen)
68 goto eoverflow;
28f540f4 69
23396375 70 return buf;
28f540f4 71}
ce982312
UD
72
73
74/* Like asctime, but write result to the user supplied buffer. The
75 buffer is only guaranteed to be 26 bytes in length. */
76char *
77__asctime_r (const struct tm *tp, char *buf)
78{
79 return asctime_internal (tp, buf, 26);
80}
23396375 81weak_alias (__asctime_r, asctime_r)
ce982312
UD
82
83
84/* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
85 which is the representation of TP in that form. */
86char *
87asctime (const struct tm *tp)
88{
89 return asctime_internal (tp, result, sizeof (result));
90}
91libc_hidden_def (asctime)