)
{
static const char* pfmt[2] = {
- "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03u",
+ "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03u",
"%08lx.%08lx [%s, %s %2d %4d %2d:%02d:%02d.%03u UTC]"
};
if (!tm) {
/*
* get a replacement, but always in UTC, using
- * caljulian()
+ * ntpcal_time_to_date()
*/
struct calendar jd;
ntpcal_time_to_date(&jd, &sec);
/*
* ymd2yd - compute the date in the year from y/m/d
+ *
+ * A thin wrapper around a more general calendar function.
*/
#include <config.h>
-#include "ntp_fp.h"
-#include "ntp_unixtime.h"
#include "ntp_stdlib.h"
-
-/*
- * Tables to compute the day of year from yyyymmdd timecode.
- * Viva la leap.
- */
-static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+#include "ntp_calendar.h"
int
ymd2yd(
int y,
int m,
- int d
- )
+ int d)
{
- int i, *t;
-
- if (m < 1 || m > 12 || d < 1)
- return (-1);
-
- if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))
- t = day2tab; /* leap year */
- else
- t = day1tab; /* not a leap year */
- if (d > t[m - 1])
- return (-1);
- for (i = 0; i < m - 1; i++)
- d += t[i];
- return d;
+ /*
+ * convert y/m/d to elapsed calendar units, convert that to
+ * elapsed days since the start of the given year and convert
+ * back to unity-based day in year.
+ *
+ * This does no further error checking, since the underlying
+ * function is assumed to work out how to handle the data.
+ */
+ return ntpcal_edate_to_yeardays(y-1, m-1, d-1) + 1;
}
#include "libntptest.h"
/*
- * Currently calyearstart() uses the caljulian() functions, which
- * in turn uses the current system time to wrap a timestamp around
- * the current year.
- *
- * Ideally, some sort of mock around caljulian() should be provided
- * so that tests doesn't depend on the current system time.
+ * calyearstart uses a pivot time, which defaults to the current system
+ * time if not given. Since this is not a good idea in an regression
+ * test, we use 2020-01-01 for the pivot.
*/
+static const time_t hold = 1577836800; // 2020-01-01 00:00:00
+
class calyearstartTest : public libntptest {
};
TEST_F(calyearstartTest, NoWrapInDateRange) {
- const u_long input = 3486372600UL; // 2010-06-24 12:50:00.
- const u_long expected = 3471292800UL; // 2010-01-01 00:00:00
+ const u_int32 input = 3486372600UL; // 2010-06-24 12:50:00.
+ const u_int32 expected = 3471292800UL; // 2010-01-01 00:00:00
- EXPECT_EQ(expected, calyearstart(input));
+ EXPECT_EQ(expected, calyearstart(input, &hold));
}
TEST_F(calyearstartTest, NoWrapInDateRangeLeapYear) {
- const u_long input = 3549528000UL; // 2012-06-24 12:00:00
- const u_long expected = 3534364800UL; // 2012-01-01 00:00:00
+ const u_int32 input = 3549528000UL; // 2012-06-24 12:00:00
+ const u_int32 expected = 3534364800UL; // 2012-01-01 00:00:00
- EXPECT_EQ(expected, calyearstart(input));
+ EXPECT_EQ(expected, calyearstart(input, &hold));
}
TEST_F(calyearstartTest, WrapInDateRange) {
- const u_long input = 19904UL; // 2036-02-07 12:00:00
- const u_long expected = 4291747200UL; // 2036-01-01 00:00:00
+ const u_int32 input = 19904UL; // 2036-02-07 12:00:00
+ const u_int32 expected = 4291747200UL; // 2036-01-01 00:00:00
- EXPECT_EQ(expected, calyearstart(input));
+ EXPECT_EQ(expected, calyearstart(input, &hold));
}