strptime: Fix day-of-week calculation.

The day-of-week calculation used the raw year value without adjusting
for TM_YEAR_BASE, so it was off by one for 300 years out of every 400;
it just happened to be correct for 1901 through 2000.  It also used a
loop where a simple addition would have sufficed.

While here, simplify our version of Gauss's algorithm, and document
that we assume the Gregorian calendar.

MFC after:	1 week
PR:		282916
Reviewed by:	imp, allanjude, philip
Differential Revision:	https://reviews.freebsd.org/D47977

(cherry picked from commit 4285e024baa80f81d13cdcc016fdf0721fe57862)
This commit is contained in:
Dag-Erling Smørgrav 2024-12-09 13:37:45 +01:00
parent 453f6aa428
commit 259dcedc4a
6 changed files with 74 additions and 15 deletions

View file

@ -364,6 +364,8 @@
..
stdlib
..
stdtime
..
string
..
sys

View file

@ -23,7 +23,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\" "
.Dd October 2, 2014
.Dd December 9, 2024
.Dt STRPTIME 3
.Os
.Sh NAME
@ -135,6 +135,11 @@ function has been contributed by Powerdog Industries.
.Pp
This man page was written by
.An J\(:org Wunsch .
.Sh CAVEATS
The
.Fn strptime
function assumes the Gregorian calendar and will produce incorrect
results for dates prior to its introduction.
.Sh BUGS
Both the
.Fa %e

View file

@ -70,17 +70,16 @@ static char * _strptime(const char *, const char *, struct tm *, int *, locale_t
#define FLAG_WDAY (1 << 5)
/*
* Calculate the week day of the first day of a year. Valid for
* the Gregorian calendar, which began Sept 14, 1752 in the UK
* and its colonies. Ref:
* http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
* Gauss's algorithm for the day of the week of the first day of any year
* in the Gregorian calendar.
*/
static int
first_wday_of(int year)
{
return (((2 * (3 - (year / 100) % 4)) + (year % 100) +
((year % 100) / 4) + (isleap(year) ? 6 : 0) + 1) % 7);
return ((1 +
5 * ((year - 1) % 4) +
4 * ((year - 1) % 100) +
6 * ((year - 1) % 400)) % 7);
}
static char *
@ -682,13 +681,8 @@ label:
flags |= FLAG_MDAY;
}
if (!(flags & FLAG_WDAY)) {
i = 0;
wday_offset = first_wday_of(tm->tm_year);
while (i++ <= tm->tm_yday) {
if (wday_offset++ >= 6)
wday_offset = 0;
}
tm->tm_wday = wday_offset;
wday_offset = first_wday_of(tm->tm_year + TM_YEAR_BASE);
tm->tm_wday = (wday_offset + tm->tm_yday) % 7;
flags |= FLAG_WDAY;
}
}

View file

@ -16,6 +16,7 @@ TESTS_SUBDIRS+= rpc
TESTS_SUBDIRS+= setjmp
TESTS_SUBDIRS+= stdio
TESTS_SUBDIRS+= stdlib
TESTS_SUBDIRS+= stdtime
TESTS_SUBDIRS+= string
TESTS_SUBDIRS+= sys
TESTS_SUBDIRS+= termios

View file

@ -0,0 +1,7 @@
.include <bsd.own.mk>
ATF_TESTS_C+= strptime_test
TESTSDIR:= ${TESTSBASE}/${RELDIR:C/libc\/tests/libc/}
.include <bsd.test.mk>

View file

@ -0,0 +1,50 @@
/*-
* Copyright (c) 2024 Dag-Erling Smørgrav
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <time.h>
#include <atf-c.h>
ATF_TC_WITHOUT_HEAD(dayofweek);
ATF_TC_BODY(dayofweek, tc)
{
static const struct {
const char *str;
int wday;
} cases[] = {
{ "1582-12-20", 1 },
{ "1700-03-01", 1 },
{ "1752-09-14", 4 },
{ "1800-12-31", 3 },
{ "1801-01-01", 4 },
{ "1900-12-31", 1 },
{ "1901-01-01", 2 },
{ "2000-12-31", 0 },
{ "2001-01-01", 1 },
{ "2100-12-31", 5 },
{ "2101-01-01", 6 },
{ "2200-12-31", 3 },
{ "2201-01-01", 4 },
{ },
};
struct tm tm;
for (unsigned int i = 0; cases[i].str != NULL; i++) {
if (strptime(cases[i].str, "%Y-%m-%d", &tm) == NULL) {
atf_tc_fail_nonfatal("failed to parse %s",
cases[i].str);
} else if (tm.tm_wday != cases[i].wday) {
atf_tc_fail_nonfatal("expected %d for %s, got %d",
cases[i].wday, cases[i].str, tm.tm_wday);
}
}
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, dayofweek);
return (atf_no_error());
}