Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_date_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <ah-date.H>
41
42#include <array>
43#include <stdexcept>
44#include <string>
45#include <vector>
46
47namespace
48{
49
50std::tm checked_localtime(std::time_t value)
51{
52 std::tm *tm_ptr = std::localtime(&value);
53 if (tm_ptr == nullptr)
54 throw std::runtime_error("localtime failed during test");
55 return *tm_ptr;
56}
57
58} // namespace
59
60TEST(AhDateTest, LeapYearDetection)
61{
62 EXPECT_TRUE(Aleph::is_leap_year(2000));
63 EXPECT_FALSE(Aleph::is_leap_year(1900));
64 EXPECT_TRUE(Aleph::is_leap_year(2024));
65 EXPECT_FALSE(Aleph::is_leap_year(2025));
66}
67
68TEST(AhDateTest, DayValidationHandlesMonthLengths)
69{
70 EXPECT_TRUE(Aleph::valid_day(2024, 2, 29));
71 EXPECT_FALSE(Aleph::valid_day(2023, 2, 29));
72 EXPECT_FALSE(Aleph::valid_day(2024, 0, 10));
73 EXPECT_FALSE(Aleph::valid_day(2024, 4, 31));
74 EXPECT_TRUE(Aleph::valid_day(2024, 4, 30));
75}
76
77TEST(AhDateTest, ToTimeTTripleRoundTrip)
78{
79 const std::time_t ts = Aleph::to_time_t(25, 12, 2022);
80 const std::tm tm = checked_localtime(ts);
81
82 EXPECT_EQ(tm.tm_mday, 25);
83 EXPECT_EQ(tm.tm_mon + 1, 12);
84 EXPECT_EQ(tm.tm_year + 1900, 2022);
85}
86
87TEST(AhDateTest, ToTimeTTripleRejectsInvalidDates)
88{
89 EXPECT_THROW(Aleph::to_time_t(31, 11, 2022), std::domain_error);
90 EXPECT_THROW(Aleph::to_time_t(29, 2, 1800), std::domain_error);
91}
92
93TEST(AhDateTest, ToTimeTFromStringValidatesFormat)
94{
95 const std::time_t ts = Aleph::to_time_t("2024-05-10 12:34:56");
96 const std::tm tm = checked_localtime(ts);
97
98 EXPECT_EQ(tm.tm_year + 1900, 2024);
99 EXPECT_EQ(tm.tm_mon + 1, 5);
100 EXPECT_EQ(tm.tm_mday, 10);
101 EXPECT_EQ(tm.tm_hour, 12);
102 EXPECT_EQ(tm.tm_min, 34);
103 EXPECT_EQ(tm.tm_sec, 56);
104
105 EXPECT_THROW(Aleph::to_time_t("2024-05-10T12:34:56", "%Y-%m-%d %H:%M:%S"),
106 std::domain_error);
107}
108
109TEST(AhDateTest, ToDaysRequiresNonNegativeTimestamps)
110{
111 constexpr std::time_t horizon = static_cast<std::time_t>(5 * 24 * 60 * 60);
112 EXPECT_EQ(Aleph::to_days(horizon), 5u);
113 EXPECT_THROW(Aleph::to_days(static_cast<std::time_t>(-1)), std::domain_error);
114}
115
116TEST(AhDateTest, ToStringFormatsAndDetectsOverflow)
117{
118 const std::time_t ts = Aleph::to_time_t("2025-01-01 00:00:00");
119 EXPECT_EQ(Aleph::to_string(ts, "%Y-%m-%d"), "2025-01-01");
120
121 const std::string oversized_format(256, 'X');
122 EXPECT_THROW(Aleph::to_string(ts, oversized_format), std::range_error);
123}
Lightweight helpers for validating and formatting civil dates.
#define TEST(name)
size_t to_days(const time_t t)
Return the number of whole days represented by t.
Definition ah-date.H:131
bool is_leap_year(const size_t yy) noexcept
Return true if the provided year is a leap year.
Definition ah-date.H:65
bool valid_day(const size_t yy, const size_t mm, const size_t dd) noexcept
Check whether a given day exists for the supplied month/year.
Definition ah-date.H:77
time_t to_time_t(const size_t dd, const size_t mm, const size_t yy)
Convert a (day, month, year) triple to a POSIX time_t value.
Definition ah-date.H:92
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140