-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_local_now.cpp
More file actions
90 lines (70 loc) · 3.2 KB
/
test_local_now.cpp
File metadata and controls
90 lines (70 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "compatible_chrono.hpp"
#include <gtest/gtest.h>
#include <iostream>
TEST(local_now_gtest, extract_components_and_ranges)
{
namespace cc = compatible_chrono; // shorten namespace for convenience
namespace ccl = compatible_chrono_local; // shorten namespace for local time utilities
using ms = cc::milliseconds; // alias for milliseconds duration
auto opt = ccl::local_now(); // get local time
if (!opt)
GTEST_SKIP() << "local_now() unavailable; timezone DB may be missing";
auto now_local = *opt;
auto c = ccl::decompose_local(now_local);
std::cout << CHRONO_FORMAT("%F %T", now_local) << '\n'; // print the local time for debugging
EXPECT_GE(c.year, 1970); // reasonable lower bound
EXPECT_GE(c.month, 1u);
EXPECT_LE(c.month, 12u);
EXPECT_GE(c.day, 1u);
EXPECT_LE(c.day, 31u);
EXPECT_LE(c.hour, 23u);
EXPECT_LE(c.minute, 59u);
EXPECT_LE(c.second, 60u); // allow leap-second upper bound
EXPECT_LE(c.millisecond, 999u);
}
TEST(local_now_gtest, span_between_local_times)
{
namespace cc = compatible_chrono; // shorten namespace for convenience
namespace ccl = compatible_chrono_local; // shorten namespace for local time utilities
using ms = cc::milliseconds; // alias for milliseconds duration
auto opt = ccl::local_now();
if (!opt)
GTEST_SKIP() << "local_now() unavailable; timezone DB may be missing";
auto now_local = *opt;
auto other_local = now_local - cc::minutes{ 123 }; // before 123 minutes
auto span = now_local - other_local; // base time - comparing time = span time
auto span_minutes = cc::duration_cast<cc::minutes>(span).count();
EXPECT_EQ(span_minutes, 123); // the difference should be exactly 123 minutes
// also verify milliseconds conversion
auto span_ms = cc::duration_cast<ms>(span).count();
EXPECT_EQ(span_ms, 123 * 60 * 1000); // 123 minutes in milliseconds
}
TEST(local_now_gtest, five_minutes_ago_components_and_difference)
{
namespace cc = compatible_chrono; // shorten namespace for convenience
namespace ccl = compatible_chrono_local; // shorten namespace for local time utilities
using ms = cc::milliseconds; // alias for milliseconds duration
auto opt = ccl::local_now();
if (!opt)
GTEST_SKIP() << "local_now() unavailable; timezone DB may be missing";
auto now_local = *opt;
auto five_min_ago = now_local - cc::minutes{5}; // 5 minutes ago
// difference must be exactly 5 minutes
auto span = now_local - five_min_ago;
auto dmin = cc::duration_cast<cc::minutes>(span).count();
EXPECT_EQ(dmin, 5);
// Decompose both and validate component ranges
auto now_c = ccl::decompose_local(now_local);
auto ago_c = ccl::decompose_local(five_min_ago);
EXPECT_GE(now_c.year, 1970);
EXPECT_GE(now_c.month, 1u);
EXPECT_LE(now_c.month, 12u);
EXPECT_GE(ago_c.year, 1970);
EXPECT_GE(ago_c.month, 1u);
EXPECT_LE(ago_c.month, 12u);
// The minute values should differ by 5 modulo 60, allow hour/day change across midnight
int minute_diff = static_cast<int>(now_c.minute) - static_cast<int>(ago_c.minute);
if (minute_diff < 0)
minute_diff += 60;
EXPECT_EQ(minute_diff, 5);
}