26 Commits

Author SHA1 Message Date
Rich Felker
61fb81e395 fix strftime %y for negative years
commit 583ea83541dcc6481c7a1bd1a9b485526bad84a1 fixed the case where
tm_year is negative but the resulting year (offset by 1900) was still
positive, which is always the case for time_t values that fit in 32
bits, but not for arbitrary inputs.

based on an earlier patch by Julien Ramseier which was overlooked at
the time the previous fix was applied.
2017-01-02 17:30:40 -05:00
Szabolcs Nagy
583ea83541 fix strftime %y for negative tm_year 2016-10-06 18:41:45 -04:00
Rich Felker
8d93cb5775 fix strftime handling of out-of-range struct tm fields
strftime results are unspecified in this case, but should not invoke
undefined behaviour.

tm_wday, tm_yday, tm_mon and tm_year fields were used in signed int
arithmetic that could overflow.

based on patch by Szabolcs Nagy.
2015-10-14 18:58:15 -04:00
Natanael Copa
c13f2af1fe match historical behavior for tm_gmtoff member of struct tm
tm_gmtoff is a nonstandard field, but on historical systems which have
this field, it stores the offset of the local time zone from GMT or
UTC. this is the opposite of the POSIX extern long timezone object and
the offsets used in POSIX-form TZ strings, which represent the offset
from local time to UTC. previously we were storing these negated
offsets in tm_gmtoff too.

programs which only used this field indirectly via strftime were not
affected since strftime performed the negation for presentation.
however, some programs and libraries accesse tm_gmtoff directly and
were obtaining negated time zone offsets.
2015-08-14 00:47:46 +00:00
Rich Felker
4c48501ee2 properly pass current locale to *_l functions when used internally
this change is presently non-functional since the callees do not yet
use their locale argument for anything.
2014-07-02 21:46:41 -04:00
Szabolcs Nagy
ac0acd569e fix strftime %s not to zero pad with default width=2 2014-05-08 19:04:48 +02:00
Rich Felker
f63b8c8c45 fix off-by-one length failure in strftime/wcsftime and improve error behavior
these functions were spuriously failing in the case where the buffer
size was exactly the number of bytes/characters to be written,
including null termination. since these functions do not have defined
error conditions other than buffer size, a reasonable application may
fail to check the return value when the format string and buffer size
are known to be valid; such an application could then attempt to use a
non-terminated buffer.

in addition to fixing the bug, I have changed the error handling
behavior so that these functions always null-terminate the output
except in the case where the buffer size is zero, and so that they
always write as many characters as possible before failing, rather
than dropping whole fields that do not fit. this actually simplifies
the logic somewhat anyway.
2013-11-26 20:01:21 -05:00
Rich Felker
242a4bb440 add the %s (seconds since the epoch) format to strftime
this is a nonstandard extension but will be required in the next
version of POSIX, and it's widely used/useful in shell scripts
utilizing the date utility.
2013-08-25 02:02:15 -04:00
Rich Felker
2828a130b1 fix strftime regression in %e format
%e pads with spaces instead of zeros.
2013-08-24 14:35:17 -04:00
Rich Felker
d78be392e1 fix strftime handling of time zone data
this may need further revision in the future, since POSIX is rather
unclear on the requirements, and is designed around the assumption of
POSIX TZ specifiers which are not sufficiently powerful to represent
real-world timezones (this is why zoneinfo support was added).

the basic issue is that strftime gets the string and numeric offset
for the timezone from the extra fields in struct tm, which are
initialized when calling localtime/gmtime/etc. however, a conforming
application might have created its own struct tm without initializing
these fields, in which case using __tm_zone (a pointer) could crash.
other zoneinfo-based implementations simply check for a null pointer,
but otherwise can still crash of the field contains junk.

simply ignoring __tm_zone and using tzname[] would "work" but would
give incorrect results in time zones with more complex rules. I feel
like this would lower the quality of implementation.

instead, simply validate __tm_zone: unless it points to one of the
zone name strings managed by the timezone system, assume it's invalid.

this commit also fixes several other minor bugs with formatting:
tm_isdst being negative is required to suppress printing of the zone
formats, and %z was using the wrong format specifiers since the type
of val was changed, resulting in bogus output.
2013-08-24 12:59:02 -04:00
Rich Felker
8b491f1499 fix missing string.h in strftime.c (needed by new strftime code)
this bug was masked by local experimental CFLAGS in my config.mak.
2013-08-23 08:11:43 -04:00
Rich Felker
fc48ceee77 add strftime and wcsftime field widths
at present, since POSIX requires %F to behave as %+4Y-%m-%d and ISO C
requires %F to behave as %Y-%m-%d, the default behavior for %Y has
been changed to match %+4Y. this seems to be the only way to conform
to the requirements of both standards, and it does not affect years
prior to the year 10000. depending on the outcome of interpretations
from the standards bodies, this may be adjusted at some point.
2013-08-22 22:36:19 -04:00
Rich Felker
33413cdd25 simplify strftime and fix integer overflows
use a long long value so that even with offsets, values cannot
overflow. instead of using different format strings for different
numeric formats, simply use a per-format width and %0*lld for all of
them.

this width specifier is not for use with strftime field widths; that
will be a separate step in the caller.
2013-08-22 19:44:02 -04:00
Rich Felker
87e133b352 strftime cleanup: avoid recomputing strlen when it's known 2013-08-22 19:36:30 -04:00
Rich Felker
45849d3dcb more strftime refactoring
make __strftime_fmt_1 return a string (possibly in the caller-provided
temp buffer) rather than writing into the output buffer. this approach
makes more sense when padding to a minimum field width might be
required, and it's also closer to what wcsftime wants.
2013-08-22 19:27:36 -04:00
Rich Felker
f5e4efc4bd begin refactoring strftime to make adding field widths easier 2013-08-22 19:02:52 -04:00
Rich Felker
1ae4bc4280 fix semantically incorrect use of LC_GLOBAL_LOCALE
LC_GLOBAL_LOCALE refers to the global locale, controlled by setlocale,
not the thread-local locale in effect which these functions should be
using. neither LC_GLOBAL_LOCALE nor 0 has an argument to the *_l
functions has behavior defined by the standard, but 0 is a more
logical choice for requesting the callee to lookup the current locale.
in the future I may move the current locale lookup the the caller (the
non-_l-suffixed wrapper).

at this point, all of the locale logic is dummied out, so no harm was
done, but it should at least avoid misleading usage.
2013-07-28 03:41:01 -04:00
Rich Felker
d53b1f820c reorder strftime to eliminate the incorrect indention level
this change is in preparation for possibly adding support for the
field width and padding specifiers added in POSIX 2008.
2013-07-27 17:47:03 -04:00
Rich Felker
87be54a135 rework langinfo code for ABI compat and for use by time code 2013-07-24 18:52:02 -04:00
Rich Felker
0a37d99547 move strftime_l into strftime.c and add __-prefixed version
the latter is both for ABI purposes, and to facilitate eventually
adding LC_TIME support. it's also nice to eliminate an extra source
file.
2013-07-24 17:58:31 -04:00
Rich Felker
1cc81f5cb0 the big time handling overhaul
this commit has two major user-visible parts: zoneinfo-format time
zones are now supported, and overflow handling is intended to be
complete in the sense that all functions return a correct result if
and only if the result fits in the destination type, and otherwise
return an error. also, some noticable bugs in the way DST detection
and normalization worked have been fixed, and performance may be
better than before, but it has not been tested.
2013-07-17 05:24:50 -04:00
Rich Felker
aea7919032 implement week-based-year year numbers in strftime
in the process, I refactored the week-number code so it can be used by
the week-based-year formats to determine year adjustments at the
boundary values. this also improves indention/code readability.
2013-06-28 12:38:42 -04:00
Rich Felker
062446a85d fix breakage in last commit to strftime due to missing INT_MAX
that's what I get for changing a hard-coded threshold to a proper
non-magic-number without testing.
2013-06-28 12:12:55 -04:00
Rich Felker
c5faf1bf09 implement week numbers and half of the week-based-year logic for strftime
output for plain week numbers (%U and %W) has been sanity-checked, and
output for the week-based-year week numbers (%V) has been checked
extensively against known-good data for the full non-negative range of
32-bit time_t.

year numbers for week-based years (%g and %G) are not yet implemented.
2013-06-28 12:03:58 -04:00
Rich Felker
400c5e5c83 use restrict everywhere it's required by c99 and/or posix 2008
to deal with the fact that the public headers may be used with pre-c99
compilers, __restrict is used in place of restrict, and defined
appropriately for any supported compiler. we also avoid the form
[restrict] since older versions of gcc rejected it due to a bug in the
original c99 standard, and instead use the form *restrict.
2012-09-06 22:44:55 -04:00
Rich Felker
0b44a0315b initial check-in, version 0.5.0 2011-02-12 00:22:29 -05:00