CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/FWCore/Utilities/src/TimeOfDay.cc

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 <time.h>
00006 
00007 namespace {
00008   int const power[] = {1000*1000, 100*1000, 10*1000, 1000, 100, 10, 1};
00009 }
00010 
00011 namespace edm {
00012 
00013   TimeOfDay::TimeOfDay() : tv_(TimeOfDay::setTime_()) {
00014   }
00015 
00016   TimeOfDay::TimeOfDay(struct timeval const& tv) : tv_(tv) {
00017   }
00018 
00019   timeval
00020   TimeOfDay::setTime_() {
00021     timeval tv;
00022     gettimeofday(&tv, 0);
00023     return tv;
00024   }
00025 
00026   std::ostream&
00027   operator<<(std::ostream& os, TimeOfDay const& tod) {
00028     std::ios::fmtflags oldflags = os.flags(); // Save stream formats so they can be left unchanged.
00029     struct tm timebuf;
00030     localtime_r(&tod.tv_.tv_sec, &timebuf);
00031     typedef std::ostreambuf_iterator<char, std::char_traits<char> > Iter;
00032     std::time_put<char, Iter> const& tp = std::use_facet<std::time_put<char, Iter> >(std::locale());
00033     int precision = os.precision();
00034     Iter begin(os);
00035     if(precision == 0) {
00036       char const pattern[] = "%d-%b-%Y %H:%M:%S %Z";
00037       tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
00038     } else {
00039       char const pattern[] = "%d-%b-%Y %H:%M:%S.";
00040       tp.put(begin, os, ' ', &timebuf, pattern, pattern + sizeof(pattern) - 1);
00041       precision = std::min(precision, 6);
00042       os << std::setfill('0') << std::setw(precision) << tod.tv_.tv_usec/power[precision] << ' ';
00043       tp.put(begin, os, ' ', &timebuf, 'Z');
00044     }
00045     os.flags(oldflags);
00046     return os;
00047   }
00048 }