Created
October 17, 2020 15:44
-
-
Save ifknot/67e36add28aa8ede3707b8c7500f09d5 to your computer and use it in GitHub Desktop.
r-ish date type stream ops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "r_date.h" | |
#include <iomanip> | |
#include <cassert> | |
#include <algorithm> | |
std::ostream& operator<<(std::ostream& os, const R::r_date& date) { | |
os << std::put_time(&date.tm, date.format.c_str()); | |
return os; | |
} | |
bool operator == (const R::r_date& lhs, const R::r_date& rhs) { | |
auto t1 = std::mktime(const_cast<tm*>(&lhs.tm)); | |
auto t2 = std::mktime(const_cast<tm*>(&rhs.tm)); | |
// -1 if time cannot be represented as std::time_t | |
assert(t1 != -1 && t2 != -1); | |
return t1 == t2; | |
} | |
bool operator < (const R::r_date& lhs, const R::r_date& rhs) { | |
auto t1 = std::mktime(const_cast<tm*>(&lhs.tm)); | |
auto t2 = std::mktime(const_cast<tm*>(&rhs.tm)); | |
// -1 if time cannot be represented as std::time_t | |
assert(t1 != -1 && t2 != -1); | |
return std::difftime(t2, t1) > 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment