]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/utime: un-inline iostream code to reduce header dependencies
authorMax Kellermann <max.kellermann@ionos.com>
Fri, 4 Oct 2024 13:34:00 +0000 (15:34 +0200)
committerMax Kellermann <max.kellermann@ionos.com>
Tue, 5 Aug 2025 08:28:01 +0000 (10:28 +0200)
Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
src/include/utime.cc
src/include/utime.h

index 4f9658ebf787382aa4c6df5f48fd013c018f25cb..507854870fbc8167ab0dfb83c9cb082153912c85 100644 (file)
@@ -17,6 +17,8 @@
 #include "common/strtol.h"
 #include "include/timegm.h"
 
+#include <iomanip> // for std::setw()
+
 #include <errno.h>
 
 void utime_t::dump(ceph::Formatter *f) const
@@ -34,6 +36,127 @@ void utime_t::generate_test_instances(std::list<utime_t*>& 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) {
index 8eb96a25fd9e3379e0d865a698f1b08d4373cc84..56e8d0e69efd656ff95ef2b432b16d8907706669 100644 (file)
@@ -28,6 +28,8 @@
 #include "include/encoding.h" // for WRITE_CLASS_ENCODER()
 #include "include/rados.h" // for struct ceph_timespec
 
+#include <iosfwd>
+
 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);