Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:14

0001 #include <ctime>
0002 #include <iomanip>
0003 #include <locale>
0004 #include <ostream>
0005 
0006 #include "FWCore/Utilities/interface/TimeOfDay.h"
0007 
0008 namespace {
0009   int const power[] = {1000 * 1000, 100 * 1000, 10 * 1000, 1000, 100, 10, 1};
0010 }
0011 
0012 namespace edm {
0013 
0014   TimeOfDay::TimeOfDay() : tv_(TimeOfDay::setTime_()) {}
0015 
0016   TimeOfDay::TimeOfDay(struct timeval const& tv) : tv_(tv) {}
0017 
0018   TimeOfDay::TimeOfDay(std::chrono::system_clock::time_point const& tp) {
0019     auto us = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();
0020     tv_.tv_sec = us / 1000000;
0021     tv_.tv_usec = us % 1000000;
0022   }
0023 
0024   timeval TimeOfDay::setTime_() {
0025     timeval tv;
0026     gettimeofday(&tv, nullptr);
0027     return tv;
0028   }
0029 
0030   std::ostream& operator<<(std::ostream& os, TimeOfDay const& tod) {
0031     auto oldflags = os.flags();  // save the stream format flags so they can be restored
0032     auto oldfill = os.fill();    // save the stream fill character so it can be restored
0033     struct tm timebuf;
0034     localtime_r(&tod.tv_.tv_sec, &timebuf);
0035     typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter;
0036     std::time_put<char, Iter> const& tp = std::use_facet<std::time_put<char, Iter> >(std::locale());
0037     int precision = os.precision();
0038     Iter begin(os);
0039     if (precision == 0) {
0040       char const pattern[] = "%d-%b-%Y %H:%M:%S %Z";
0041       tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
0042     } else {
0043       char const pattern[] = "%d-%b-%Y %H:%M:%S.";
0044       tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
0045       precision = std::min(precision, 6);
0046       os << std::setfill('0') << std::setw(precision) << tod.tv_.tv_usec / power[precision] << ' ';
0047       tp.put(begin, os, ' ', &timebuf, 'Z');
0048     }
0049     os.flags(oldflags);
0050     os.fill(oldfill);
0051     return os;
0052   }
0053 
0054 }  // namespace edm