Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions google/cloud/spanner/date.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "google/cloud/spanner/date.h"
#include "google/cloud/spanner/internal/date.h"
#include <array>

namespace google {
Expand Down Expand Up @@ -59,6 +60,10 @@ Date::Date(std::int64_t year, int month, int day)
}
}

std::ostream& operator<<(std::ostream& os, Date const& date) {
return os << internal::DateToString(date);
}

} // namespace SPANNER_CLIENT_NS
} // namespace spanner
} // namespace cloud
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/spanner/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "google/cloud/spanner/version.h"
#include <cstdint>
#include <ostream>
#include <tuple>

namespace google {
Expand Down Expand Up @@ -46,6 +47,8 @@ class Date {
int day_;
};

std::ostream& operator<<(std::ostream& os, Date const& date);

inline bool operator==(Date const& a, Date const& b) {
return std::make_tuple(a.year(), a.month(), a.day()) ==
std::make_tuple(b.year(), b.month(), b.day());
Expand Down
22 changes: 22 additions & 0 deletions google/cloud/spanner/date_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "google/cloud/spanner/date.h"
#include <gmock/gmock.h>
#include <sstream>
#include <vector>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -78,6 +80,26 @@ TEST(Date, Normalization) {
EXPECT_EQ(Date(2012, 9, 30), Date(2016, -42, 122));
}

TEST(Date, OutputStream) {
struct TestCase {
Date date;
std::string expected;
};

std::vector<TestCase> test_cases = {
{Date(1, 1, 1), "0001-01-01"},
{Date(1970, 1, 1), "1970-01-01"},
{Date(2020, 3, 14), "2020-03-14"},
{Date(9999, 12, 31), "9999-12-31"},
};

for (auto const& tc : test_cases) {
std::ostringstream ss;
ss << tc.date;
EXPECT_EQ(ss.str(), tc.expected);
}
}

} // namespace
} // namespace SPANNER_CLIENT_NS
} // namespace spanner
Expand Down