Go to the documentation of this file.00001 #include "FWCore/Utilities/interface/TimeOfDay.h"
00002 #include <iomanip>
00003 #include <locale>
00004 #include <ostream>
00005 #include <sys/time.h>
00006 #include <time.h>
00007
00008 namespace {
00009 int const power[] = {1000*1000, 100*1000, 10*1000, 1000, 100, 10, 1};
00010 }
00011
00012 namespace edm {
00013
00014 TimeOfDay::TimeOfDay() : tv_(TimeOfDay::setTime_()) {
00015 }
00016
00017 TimeOfDay::TimeOfDay(struct timeval const& tv) : tv_(tv) {
00018 }
00019
00020 timeval
00021 TimeOfDay::setTime_() {
00022 timeval tv;
00023 gettimeofday(&tv, 0);
00024 return tv;
00025 }
00026
00027 std::ostream&
00028 operator<<(std::ostream& os, TimeOfDay const& tod) {
00029 std::ios::fmtflags oldflags = os.flags();
00030 struct tm timebuf;
00031 localtime_r(&tod.tv_.tv_sec, &timebuf);
00032 typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter;
00033 std::time_put<char, Iter> const& tp = std::use_facet<std::time_put<char, Iter> >(std::locale());
00034 int precision = os.precision();
00035 Iter begin(os);
00036 if (precision == 0) {
00037 char const pattern[] = "%d-%b-%Y %H:%M:%S %Z";
00038 tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
00039 } else {
00040 char const pattern[] = "%d-%b-%Y %H:%M:%S.";
00041 tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
00042 precision = std::min(precision, 6);
00043 os << std::setfill('0') << std::setw(precision) << tod.tv_.tv_usec/power[precision] << ' ';
00044 tp.put(begin, os, ' ', &timebuf, 'Z');
00045 }
00046 os.flags(oldflags);
00047 return os;
00048 }
00049 }