Skip to content

Commit f463e65

Browse files
committed
localtime_to_utc test
1 parent 9f9276a commit f463e65

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

chrono.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,11 @@ std::optional<local_time_point> local_time_point_from_string(std::string_view st
177177
#ifdef HAVE_CPP20_CHRONO
178178
std::optional<time_point> localtime_to_utc(local_time_point dt, std::optional<choose> choice)
179179
{
180-
if (choice.has_value()) {
181-
return localtime_to_utc(zoned_time(std::chrono::current_zone(), dt, *choice));
182-
} else {
183-
return localtime_to_utc(zoned_time(std::chrono::current_zone(), dt));
184-
}
180+
auto choiseVal = choice.value_or(choose::latest);
181+
return localtime_to_utc(zoned_time(std::chrono::current_zone(), dt, choiseVal), choiseVal);
185182
}
186183

187-
std::optional<time_point> localtime_to_utc(zoned_time zt)
184+
std::optional<time_point> localtime_to_utc(zoned_time zt, choose choice)
188185
{
189186
auto localTimePoint = zt.get_local_time();
190187

@@ -196,8 +193,12 @@ std::optional<time_point> localtime_to_utc(zoned_time zt)
196193
break;
197194
}
198195
case std::chrono::local_info::ambiguous: {
199-
Log::warn("Ambiguous time point, taking the latest option");
200-
utcTime = std::optional<time_point>(i.second.begin);
196+
if (choice == choose::latest) {
197+
utcTime = std::optional<time_point>(i.second.begin - i.second.save);
198+
} else {
199+
assert(choice == choose::earliest);
200+
utcTime = std::optional<time_point>(i.first.end - i.first.save);
201+
}
201202
break;
202203
}
203204
case std::chrono::local_info::nonexistent:

include/infra/chrono.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ inline std::string to_string(std::string_view format, chrono::local_time_point t
189189
}
190190

191191
std::optional<time_point> localtime_to_utc(local_time_point dt, std::optional<choose> choice);
192-
std::optional<time_point> localtime_to_utc(zoned_time tp);
192+
std::optional<time_point> localtime_to_utc(zoned_time tp, choose choice);
193193

194194
class DurationRecorder
195195
{

test/chronotest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ TEST_CASE("ChronoTest.time_point_to_utc_string")
1313
CHECK("2017-02-01" == inf::chrono::to_string("%Y-%m-%d", date));
1414
}
1515

16+
TEST_CASE("ChronoTest.time_point_to_utc_string_ambiguous")
17+
{
18+
auto tp = chrono::local_time_point_from_string("2019-10-27 02:20:00.000", "%Y-%m-%d %H:%M:%S");
19+
REQUIRE(tp.has_value());
20+
21+
{
22+
auto utcTime = chrono::localtime_to_utc(*tp, chrono::choose::latest);
23+
REQUIRE(utcTime.has_value());
24+
25+
chrono::time_point expected = std::chrono::sys_days(2019y / std::chrono::October / 27) + 1h;
26+
CHECK(expected == *utcTime);
27+
}
28+
29+
{
30+
auto utcTime = chrono::localtime_to_utc(*tp, chrono::choose::earliest);
31+
REQUIRE(utcTime.has_value());
32+
33+
chrono::time_point expected = std::chrono::sys_days(2019y / std::chrono::October / 27) + 0h;
34+
CHECK(expected == *utcTime);
35+
}
36+
}
37+
1638
TEST_CASE("ChronoTest.time_point_to_string_custom_format")
1739
{
1840
chrono::date_point date = std::chrono::sys_days(2017y / std::chrono::February / 1);

0 commit comments

Comments
 (0)