Skip to content

Commit

Permalink
Add max()/min() factories for all civil_time types.
Browse files Browse the repository at this point in the history
  • Loading branch information
devbww committed Apr 6, 2016
1 parent 94299ab commit 582cab4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/civil_time_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ class civil_time {
preserves_data<U, T>* = nullptr) noexcept
: civil_time(ct.f_) {}

// Factories for the maximum/minimum representable civil_time.
static civil_time max() {
return civil_time(std::numeric_limits<int>::max(), 12, 31, 23, 59, 59);
}
static civil_time min() {
return civil_time(std::numeric_limits<int>::min(), 1, 1, 0, 0, 0);
}

// Field accessors.
CONSTEXPR_M int year() const noexcept { return f_.y; }
CONSTEXPR_M int month() const noexcept { return f_.m; }
Expand Down
19 changes: 19 additions & 0 deletions src/civil_time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,25 @@ TEST(CivilTime, FieldsConstructionLimits) {
Format(civil_second(1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
}

TEST(CivilTime, RangeLimits) {
const int kIntMax = std::numeric_limits<int>::max();
const int kIntMin = std::numeric_limits<int>::min();

EXPECT_EQ(civil_year(kIntMax), civil_year::max());
EXPECT_EQ(civil_month(kIntMax, 12), civil_month::max());
EXPECT_EQ(civil_day(kIntMax, 12, 31), civil_day::max());
EXPECT_EQ(civil_hour(kIntMax, 12, 31, 23), civil_hour::max());
EXPECT_EQ(civil_minute(kIntMax, 12, 31, 23, 59), civil_minute::max());
EXPECT_EQ(civil_second(kIntMax, 12, 31, 23, 59, 59), civil_second::max());

EXPECT_EQ(civil_year(kIntMin), civil_year::min());
EXPECT_EQ(civil_month(kIntMin, 1), civil_month::min());
EXPECT_EQ(civil_day(kIntMin, 1, 1), civil_day::min());
EXPECT_EQ(civil_hour(kIntMin, 1, 1, 0), civil_hour::min());
EXPECT_EQ(civil_minute(kIntMin, 1, 1, 0, 0), civil_minute::min());
EXPECT_EQ(civil_second(kIntMin, 1, 1, 0, 0, 0), civil_second::min());
}

TEST(CivilTime, ImplicitCrossAlignment) {
civil_year year(2015);
civil_month month = year;
Expand Down

0 comments on commit 582cab4

Please sign in to comment.