Files
radiantwave/tests/test_date_format.cpp
T
NeilClack 59b499037b Kiosk: show all user-visible times as 12-hour AM/PM
Add datetime::formatHm12h/formatTime12h and use them for the Home footer
clock, the Timezone page's local-time sample, and the log viewer
timestamps. Storage and parsing remain 24-hour HH:MM. Extends the
date-format tests with the midnight/noon edge cases.
2026-09-29 08:43:59 -05:00

118 lines
3.4 KiB
C++

#include "date_format.h"
#include <gtest/gtest.h>
#include <cstdlib>
#include <ctime>
using namespace radiantwave::datetime;
class DateFormatTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
setenv("TZ", "UTC", 1);
tzset();
}
};
TEST_F(DateFormatTest, DefaultFormatConstant) {
EXPECT_STREQ(DEFAULT_DATE_FORMAT, "%m/%d/%Y");
}
TEST_F(DateFormatTest, FormatsKnownTimestamp) {
std::tm tm = {};
tm.tm_year = 2026 - 1900;
tm.tm_mon = 8 - 1;
tm.tm_mday = 4;
tm.tm_hour = 14;
tm.tm_min = 23;
tm.tm_sec = 45;
tm.tm_isdst = -1;
time_t t = mktime(&tm);
ASSERT_NE(t, static_cast<time_t>(-1));
EXPECT_EQ(formatDate(t, "%m/%d/%Y"), "08/04/2026");
EXPECT_EQ(formatDate(t, "%d/%m/%Y"), "04/08/2026");
EXPECT_EQ(formatDate(t, "%Y-%m-%d"), "2026-08-04");
EXPECT_EQ(formatDate(t, "%m.%d.%Y"), "08.04.2026");
EXPECT_EQ(formatDate(t, "%d.%m.%Y"), "04.08.2026");
EXPECT_EQ(formatDate(t, "%B %d, %Y"), "August 04, 2026");
EXPECT_EQ(formatDate(t, "%d %B %Y"), "04 August 2026");
}
TEST_F(DateFormatTest, FormatsTime) {
std::tm tm = {};
tm.tm_year = 2026 - 1900;
tm.tm_mon = 8 - 1;
tm.tm_mday = 4;
tm.tm_hour = 14;
tm.tm_min = 23;
tm.tm_sec = 45;
tm.tm_isdst = -1;
time_t t = mktime(&tm);
ASSERT_NE(t, static_cast<time_t>(-1));
EXPECT_EQ(formatTime(t), "14:23:45");
}
TEST_F(DateFormatTest, OptionsIncludeDefault) {
auto options = getDateFormatOptions();
ASSERT_FALSE(options.empty());
EXPECT_EQ(options[0].pattern, std::string(DEFAULT_DATE_FORMAT));
EXPECT_EQ(options[0].name, "Month/Day/Year");
}
TEST_F(DateFormatTest, Converts24HourTo12HourLabels) {
EXPECT_EQ(formatHm12h("00:00"), "12:00 AM");
EXPECT_EQ(formatHm12h("00:05"), "12:05 AM");
EXPECT_EQ(formatHm12h("11:59"), "11:59 AM");
EXPECT_EQ(formatHm12h("12:00"), "12:00 PM");
EXPECT_EQ(formatHm12h("12:30"), "12:30 PM");
EXPECT_EQ(formatHm12h("13:00"), "1:00 PM");
EXPECT_EQ(formatHm12h("23:59"), "11:59 PM");
}
TEST_F(DateFormatTest, LeavesMalformedInputUnchanged) {
EXPECT_EQ(formatHm12h(""), "");
EXPECT_EQ(formatHm12h("7:05"), "7:05");
EXPECT_EQ(formatHm12h("24:00"), "24:00");
EXPECT_EQ(formatHm12h("12:60"), "12:60");
EXPECT_EQ(formatHm12h("ab:cd"), "ab:cd");
}
TEST_F(DateFormatTest, Formats12HourClock) {
std::tm morning = {};
morning.tm_year = 2026 - 1900;
morning.tm_mon = 8 - 1;
morning.tm_mday = 4;
morning.tm_hour = 7;
morning.tm_min = 5;
morning.tm_sec = 0;
morning.tm_isdst = -1;
time_t tMorning = mktime(&morning);
ASSERT_NE(tMorning, static_cast<time_t>(-1));
EXPECT_EQ(formatTime12h(tMorning), "7:05 AM");
std::tm midnight = {};
midnight.tm_year = 2026 - 1900;
midnight.tm_mon = 8 - 1;
midnight.tm_mday = 4;
midnight.tm_hour = 0;
midnight.tm_min = 0;
midnight.tm_sec = 0;
midnight.tm_isdst = -1;
time_t tMidnight = mktime(&midnight);
ASSERT_NE(tMidnight, static_cast<time_t>(-1));
EXPECT_EQ(formatTime12h(tMidnight), "12:00 AM");
std::tm evening = {};
evening.tm_year = 2026 - 1900;
evening.tm_mon = 8 - 1;
evening.tm_mday = 4;
evening.tm_hour = 19;
evening.tm_min = 5;
evening.tm_sec = 0;
evening.tm_isdst = -1;
time_t tEvening = mktime(&evening);
ASSERT_NE(tEvening, static_cast<time_t>(-1));
EXPECT_EQ(formatTime12h(tEvening), "7:05 PM");
}