Skip to content

Instantly share code, notes, and snippets.

@ifknot
Created October 17, 2020 15:44
Show Gist options
  • Save ifknot/67e36add28aa8ede3707b8c7500f09d5 to your computer and use it in GitHub Desktop.
Save ifknot/67e36add28aa8ede3707b8c7500f09d5 to your computer and use it in GitHub Desktop.
r-ish date type stream ops
#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