Skip to content

Commit 893c9aa

Browse files
committed
feat universal: remove utils::datetime::date namespace definition
Tests: протестировано CI commit_hash:5f480953683c3526628566bbb6f3b629b4d562fb
1 parent 2ffc772 commit 893c9aa

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

universal/include/userver/utils/datetime/cpp_20_calendar.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,25 @@ USERVER_NAMESPACE_BEGIN
1414
namespace utils::datetime {
1515

1616
// TODO: remove the following aliases
17-
namespace date = std::chrono;
1817
using Days = std::chrono::days;
1918
using DaysTimepoint = std::chrono::sys_days;
2019

2120
/// @brief Calculates the number of days between January 1, 00:00 of two years accounting for leap years.
2221
constexpr std::chrono::days DaysBetweenYears(int from, int to) {
2322
return std::chrono::duration_cast<Days>(
24-
DaysTimepoint{date::year_month_day(date::year(to), date::month(1), date::day(1))} -
25-
DaysTimepoint{date::year_month_day(date::year(from), date::month(1), date::day(1))}
23+
DaysTimepoint{std::chrono::year_month_day(std::chrono::year(to), std::chrono::month(1), std::chrono::day(1))} -
24+
DaysTimepoint{std::chrono::year_month_day(std::chrono::year(from), std::chrono::month(1), std::chrono::day(1))}
2625
);
2726
}
2827

2928
/// @brief Get the number of days in the given month of a given year.
3029
constexpr std::chrono::day DaysInMonth(int month, int year) {
3130
UINVARIANT(month >= 1 && month <= 12, "Month must be between 1 and 12");
32-
return date::year_month_day_last(date::year(year), date::month_day_last(date::month(month))).day();
31+
return std::chrono::year_month_day_last(
32+
std::chrono::year(year),
33+
std::chrono::month_day_last(std::chrono::month(month))
34+
)
35+
.day();
3336
}
3437

3538
} // namespace utils::datetime

universal/src/utils/datetime/cpp_20_calendar_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
USERVER_NAMESPACE_BEGIN
88

9-
namespace date = utils::datetime::date;
9+
namespace date = std::chrono;
1010

1111
TEST(DaysBetweenYears, LeapYear) { EXPECT_EQ(utils::datetime::DaysBetweenYears(2019, 2021).count(), 365 + 366); }
1212
TEST(DaysBetweenYears, YearOne) { EXPECT_EQ(utils::datetime::DaysBetweenYears(1, 1970).count(), 719162); }

universal/src/utils/datetime/date.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ namespace {
1414
const std::string kDateFormat = "%Y-%m-%d";
1515
const auto kUtcTz = cctz::utc_time_zone();
1616

17-
// TODO: replace with C++20 std::chrono::days
18-
constexpr date::days ToDays(int year, int month, int day) noexcept {
19-
const date::year_month_day
20-
ymd{date::year{year}, date::month{static_cast<unsigned>(month)}, date::day{static_cast<unsigned>(day)}};
21-
22-
return date::sys_days{ymd}.time_since_epoch();
17+
constexpr std::chrono::days ToDays(int year, int month, int day) noexcept {
18+
const std::chrono::year_month_day ymd{
19+
std::chrono::year{year},
20+
std::chrono::month{static_cast<unsigned>(month)},
21+
std::chrono::day{static_cast<unsigned>(day)}
22+
};
23+
24+
return std::chrono::sys_days{ymd}.time_since_epoch();
2325
}
2426

2527
} // namespace

0 commit comments

Comments
 (0)