about summary refs log tree commit diff
path: root/src/time/strftime.c
Commit message (Collapse)AuthorAgeFilesLines
* overhaul internally-public declarations using wrapper headersRich Felker2018-09-121-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | commits leading up to this one have moved the vast majority of libc-internal interface declarations to appropriate internal headers, allowing them to be type-checked and setting the stage to limit their visibility. the ones that have not yet been moved are mostly namespace-protected aliases for standard/public interfaces, which exist to facilitate implementing plain C functions in terms of POSIX functionality, or C or POSIX functionality in terms of extensions that are not standardized. some don't quite fit this description, but are "internally public" interfacs between subsystems of libc. rather than create a number of newly-named headers to declare these functions, and having to add explicit include directives for them to every source file where they're needed, I have introduced a method of wrapping the corresponding public headers. parallel to the public headers in $(srcdir)/include, we now have wrappers in $(srcdir)/src/include that come earlier in the include path order. they include the public header they're wrapping, then add declarations for namespace-protected versions of the same interfaces and any "internally public" interfaces for the subsystem they correspond to. along these lines, the wrapper for features.h is now responsible for the definition of the hidden, weak, and weak_alias macros. this means source files will no longer need to include any special headers to access these features. over time, it is my expectation that the scope of what is "internally public" will expand, reducing the number of source files which need to include *_impl.h and related headers down to those which are actually implementing the corresponding subsystems, not just using them.
* move __tm_to_tzname declaration to time_impl.h with related functionsRich Felker2018-09-121-1/+0
| | | | | | this function was added later for strftime use and the existence of time_impl.h as the appropriate place for it seems to have been overlooked.
* fix type-mismatched declarations of __nl_langinfo_l in source filesRich Felker2018-09-121-1/+1
| | | | | obviously the type "should be" const, but it inherited non-const from the standard nl_langinfo_l.
* fix sign of strftime %z output with offsets <1 hour west of UTCRich Felker2018-08-071-3/+2
| | | | | | | | the sign character produced came from the sign of tm_gmtoff/3600 as an integer division, which is zero for negative offsets smaller in magnitude than 3600. instead of printing the hours and minutes as separate fields, print them as a single value of the form hours*100+minutes, which naturally has the correct sign.
* strftime: fix underlying format string in %z formatDaniel Sabogal2018-06-261-1/+1
| | | | the expression (tm->__tm_gmtoff)/3600 has type long. use %+.2ld instead.
* adjust strftime + modifier to match apparent intent of POSIXRich Felker2018-02-061-6/+12
| | | | | | | | | | | | | | | | it's unclear from the specification whether the word "consumes" in "consumes more than four bytes to represent a year" refers just to significant places or includes leading zeros due to field width padding. however the examples in the rationale indicate that the latter was the intent. in particular, the year 270 is shown being formatted by %+5Y as +0270 rather than 00270. previously '+' prefixing was implemented just by comparing the year against 10000. instead, count the number of significant digits and padding bytes to be added, and use the total to determine whether to apply the '+' prefix. based on testing by Dennis Wölfing.
* fix strftime field widths with %F format and zero yearRich Felker2018-02-051-1/+2
| | | | | | | | | the code to strip initial sign and leading zeros inadvertently stripped all the zeros and the subsequent '-' separating the month. instead, only strip sign characters from the very first position, and only strip zeros when they are followed by another digit. based on testing by Dennis Wölfing.
* implement strftime padding specifier extensionsTimo Teräs2017-12-111-8/+14
| | | | | | | | | | | | | notes added by maintainer: the '-' specifier allows default padding to be suppressed, and '_' allows padding with spaces instead of the default (zeros). these extensions seem to be included in several other implementations including FreeBSD and derivatives, and Solaris. while portable software should not depend on them, time format strings are often exposed to the user for configurable time display. reportedly some python programs also use and depend on them.
* fix strftime %y for negative yearsRich Felker2017-01-021-2/+2
| | | | | | | | | | 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.
* fix strftime %y for negative tm_yearSzabolcs Nagy2016-10-061-0/+1
|
* fix strftime handling of out-of-range struct tm fieldsRich Felker2015-10-141-8/+12
| | | | | | | | | | 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.
* match historical behavior for tm_gmtoff member of struct tmNatanael Copa2015-08-141-2/+2
| | | | | | | | | | | | | | 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.
* properly pass current locale to *_l functions when used internallyRich Felker2014-07-021-1/+2
| | | | | this change is presently non-functional since the callees do not yet use their locale argument for anything.
* fix strftime %s not to zero pad with default width=2Szabolcs Nagy2014-05-081-0/+1
|
* fix off-by-one length failure in strftime/wcsftime and improve error behaviorRich Felker2013-11-261-6/+8
| | | | | | | | | | | | | | | | | 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.
* add the %s (seconds since the epoch) format to strftimeRich Felker2013-08-251-0/+4
| | | | | | 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.
* fix strftime regression in %e formatRich Felker2013-08-241-2/+2
| | | | %e pads with spaces instead of zeros.
* fix strftime handling of time zone dataRich Felker2013-08-241-3/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* fix missing string.h in strftime.c (needed by new strftime code)Rich Felker2013-08-231-0/+1
| | | | this bug was masked by local experimental CFLAGS in my config.mak.
* add strftime and wcsftime field widthsRich Felker2013-08-221-3/+32
| | | | | | | | | 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.
* simplify strftime and fix integer overflowsRich Felker2013-08-221-28/+12
| | | | | | | | | | 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.
* strftime cleanup: avoid recomputing strlen when it's knownRich Felker2013-08-221-10/+16
|
* more strftime refactoringRich Felker2013-08-221-23/+25
| | | | | | | 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.
* begin refactoring strftime to make adding field widths easierRich Felker2013-08-221-151/+161
|
* fix semantically incorrect use of LC_GLOBAL_LOCALERich Felker2013-07-281-1/+1
| | | | | | | | | | | | | 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.
* reorder strftime to eliminate the incorrect indention levelRich Felker2013-07-271-5/+5
| | | | | this change is in preparation for possibly adding support for the field width and padding specifiers added in POSIX 2008.
* rework langinfo code for ABI compat and for use by time codeRich Felker2013-07-241-4/+4
|
* move strftime_l into strftime.c and add __-prefixed versionRich Felker2013-07-241-1/+10
| | | | | | 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.
* the big time handling overhaulRich Felker2013-07-171-6/+2
| | | | | | | | | | 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.
* implement week-based-year year numbers in strftimeRich Felker2013-06-281-27/+34
| | | | | | 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.
* fix breakage in last commit to strftime due to missing INT_MAXRich Felker2013-06-281-0/+1
| | | | | that's what I get for changing a hard-coded threshold to a proper non-magic-number without testing.
* implement week numbers and half of the week-based-year logic for strftimeRich Felker2013-06-281-3/+38
| | | | | | | | | 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.
* use restrict everywhere it's required by c99 and/or posix 2008Rich Felker2012-09-061-1/+1
| | | | | | | | 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.
* initial check-in, version 0.5.0 v0.5.0Rich Felker2011-02-121-0/+172