From 7a154f605713563727886f589c80472a0307e129 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 4 Oct 2024 15:34:00 +0200 Subject: [PATCH] include/utime: un-inline iostream code to reduce header dependencies Signed-off-by: Max Kellermann --- src/include/utime.cc | 123 ++++++++++++++++++++++++++++++++++++++++++ src/include/utime.h | 125 +++---------------------------------------- 2 files changed, 130 insertions(+), 118 deletions(-) diff --git a/src/include/utime.cc b/src/include/utime.cc index 4f9658ebf7873..507854870fbc8 100644 --- a/src/include/utime.cc +++ b/src/include/utime.cc @@ -17,6 +17,8 @@ #include "common/strtol.h" #include "include/timegm.h" +#include // for std::setw() + #include void utime_t::dump(ceph::Formatter *f) const @@ -34,6 +36,127 @@ void utime_t::generate_test_instances(std::list& o) o.back()->tv.tv_nsec = static_cast<__u32>((1L << 32) - 1); } +std::ostream& utime_t::gmtime(std::ostream& out, bool legacy_form) const { + out.setf(std::ios::right); + char oldfill = out.fill(); + out.fill('0'); + if (sec() < ((time_t)(60*60*24*365*10))) { + // raw seconds. this looks like a relative time. + out << (long)sec() << "." << std::setw(6) << usec(); + } else { + // this looks like an absolute time. + // conform to http://en.wikipedia.org/wiki/ISO_8601 + struct tm bdt; + time_t tt = sec(); + gmtime_r(&tt, &bdt); + out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' + << '-' << std::setw(2) << (bdt.tm_mon+1) + << '-' << std::setw(2) << bdt.tm_mday; + if (legacy_form) { + out << ' '; + } else { + out << 'T'; + } + out << std::setw(2) << bdt.tm_hour + << ':' << std::setw(2) << bdt.tm_min + << ':' << std::setw(2) << bdt.tm_sec; + out << "." << std::setw(6) << usec(); + out << "Z"; + } + out.fill(oldfill); + out.unsetf(std::ios::right); + return out; +} + +std::ostream& utime_t::gmtime_nsec(std::ostream& out) const { + out.setf(std::ios::right); + char oldfill = out.fill(); + out.fill('0'); + if (sec() < ((time_t)(60*60*24*365*10))) { + // raw seconds. this looks like a relative time. + out << (long)sec() << "." << std::setw(6) << usec(); + } else { + // this looks like an absolute time. + // conform to http://en.wikipedia.org/wiki/ISO_8601 + struct tm bdt; + time_t tt = sec(); + gmtime_r(&tt, &bdt); + out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' + << '-' << std::setw(2) << (bdt.tm_mon+1) + << '-' << std::setw(2) << bdt.tm_mday + << 'T' + << std::setw(2) << bdt.tm_hour + << ':' << std::setw(2) << bdt.tm_min + << ':' << std::setw(2) << bdt.tm_sec; + out << "." << std::setw(9) << nsec(); + out << "Z"; + } + out.fill(oldfill); + out.unsetf(std::ios::right); + return out; +} + +std::ostream& utime_t::asctime(std::ostream& out) const { + out.setf(std::ios::right); + char oldfill = out.fill(); + out.fill('0'); + if (sec() < ((time_t)(60*60*24*365*10))) { + // raw seconds. this looks like a relative time. + out << (long)sec() << "." << std::setw(6) << usec(); + } else { + // this looks like an absolute time. + struct tm bdt; + time_t tt = sec(); + gmtime_r(&tt, &bdt); + + char buf[128]; + asctime_r(&bdt, buf); + int len = strlen(buf); + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + out << buf; + } + out.fill(oldfill); + out.unsetf(std::ios::right); + return out; +} + +std::ostream& utime_t::localtime(std::ostream& out, bool legacy_form) const { + out.setf(std::ios::right); + char oldfill = out.fill(); + out.fill('0'); + if (sec() < ((time_t)(60*60*24*365*10))) { + // raw seconds. this looks like a relative time. + out << (long)sec() << "." << std::setw(6) << usec(); + } else { + // this looks like an absolute time. + // conform to http://en.wikipedia.org/wiki/ISO_8601 + struct tm bdt; + time_t tt = sec(); + localtime_r(&tt, &bdt); + out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' + << '-' << std::setw(2) << (bdt.tm_mon+1) + << '-' << std::setw(2) << bdt.tm_mday; + if (legacy_form) { + out << ' '; + } else { + out << 'T'; + } + out << std::setw(2) << bdt.tm_hour + << ':' << std::setw(2) << bdt.tm_min + << ':' << std::setw(2) << bdt.tm_sec; + out << "." << std::setw(6) << usec(); + if (!legacy_form) { + char buf[32] = { 0 }; + strftime(buf, sizeof(buf), "%z", &bdt); + out << buf; + } + } + out.fill(oldfill); + out.unsetf(std::ios::right); + return out; +} + int utime_t::parse_date(const std::string& date, uint64_t *epoch, uint64_t *nsec, std::string *out_date, std::string *out_time) { diff --git a/src/include/utime.h b/src/include/utime.h index 8eb96a25fd9e3..56e8d0e69efd6 100644 --- a/src/include/utime.h +++ b/src/include/utime.h @@ -28,6 +28,8 @@ #include "include/encoding.h" // for WRITE_CLASS_ENCODER() #include "include/rados.h" // for struct ceph_timespec +#include + namespace ceph { class Formatter; } // -------- @@ -240,128 +242,15 @@ public: } // output - std::ostream& gmtime(std::ostream& out, bool legacy_form=false) const { - out.setf(std::ios::right); - char oldfill = out.fill(); - out.fill('0'); - if (sec() < ((time_t)(60*60*24*365*10))) { - // raw seconds. this looks like a relative time. - out << (long)sec() << "." << std::setw(6) << usec(); - } else { - // this looks like an absolute time. - // conform to http://en.wikipedia.org/wiki/ISO_8601 - struct tm bdt; - time_t tt = sec(); - gmtime_r(&tt, &bdt); - out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' - << '-' << std::setw(2) << (bdt.tm_mon+1) - << '-' << std::setw(2) << bdt.tm_mday; - if (legacy_form) { - out << ' '; - } else { - out << 'T'; - } - out << std::setw(2) << bdt.tm_hour - << ':' << std::setw(2) << bdt.tm_min - << ':' << std::setw(2) << bdt.tm_sec; - out << "." << std::setw(6) << usec(); - out << "Z"; - } - out.fill(oldfill); - out.unsetf(std::ios::right); - return out; - } + std::ostream& gmtime(std::ostream& out, bool legacy_form=false) const; // output - std::ostream& gmtime_nsec(std::ostream& out) const { - out.setf(std::ios::right); - char oldfill = out.fill(); - out.fill('0'); - if (sec() < ((time_t)(60*60*24*365*10))) { - // raw seconds. this looks like a relative time. - out << (long)sec() << "." << std::setw(6) << usec(); - } else { - // this looks like an absolute time. - // conform to http://en.wikipedia.org/wiki/ISO_8601 - struct tm bdt; - time_t tt = sec(); - gmtime_r(&tt, &bdt); - out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' - << '-' << std::setw(2) << (bdt.tm_mon+1) - << '-' << std::setw(2) << bdt.tm_mday - << 'T' - << std::setw(2) << bdt.tm_hour - << ':' << std::setw(2) << bdt.tm_min - << ':' << std::setw(2) << bdt.tm_sec; - out << "." << std::setw(9) << nsec(); - out << "Z"; - } - out.fill(oldfill); - out.unsetf(std::ios::right); - return out; - } + std::ostream& gmtime_nsec(std::ostream& out) const; // output - std::ostream& asctime(std::ostream& out) const { - out.setf(std::ios::right); - char oldfill = out.fill(); - out.fill('0'); - if (sec() < ((time_t)(60*60*24*365*10))) { - // raw seconds. this looks like a relative time. - out << (long)sec() << "." << std::setw(6) << usec(); - } else { - // this looks like an absolute time. - struct tm bdt; - time_t tt = sec(); - gmtime_r(&tt, &bdt); - - char buf[128]; - asctime_r(&bdt, buf); - int len = strlen(buf); - if (buf[len - 1] == '\n') - buf[len - 1] = '\0'; - out << buf; - } - out.fill(oldfill); - out.unsetf(std::ios::right); - return out; - } - - std::ostream& localtime(std::ostream& out, bool legacy_form=false) const { - out.setf(std::ios::right); - char oldfill = out.fill(); - out.fill('0'); - if (sec() < ((time_t)(60*60*24*365*10))) { - // raw seconds. this looks like a relative time. - out << (long)sec() << "." << std::setw(6) << usec(); - } else { - // this looks like an absolute time. - // conform to http://en.wikipedia.org/wiki/ISO_8601 - struct tm bdt; - time_t tt = sec(); - localtime_r(&tt, &bdt); - out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07' - << '-' << std::setw(2) << (bdt.tm_mon+1) - << '-' << std::setw(2) << bdt.tm_mday; - if (legacy_form) { - out << ' '; - } else { - out << 'T'; - } - out << std::setw(2) << bdt.tm_hour - << ':' << std::setw(2) << bdt.tm_min - << ':' << std::setw(2) << bdt.tm_sec; - out << "." << std::setw(6) << usec(); - if (!legacy_form) { - char buf[32] = { 0 }; - strftime(buf, sizeof(buf), "%z", &bdt); - out << buf; - } - } - out.fill(oldfill); - out.unsetf(std::ios::right); - return out; - } + std::ostream& asctime(std::ostream& out) const; + + std::ostream& localtime(std::ostream& out, bool legacy_form=false) const; static int invoke_date(const std::string& date_str, utime_t *result); -- 2.39.5