LLVM API Documentation

Unix/TimeValue.inc
Go to the documentation of this file.
00001 //===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Unix specific portion of the TimeValue class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 //===----------------------------------------------------------------------===//
00015 //=== WARNING: Implementation here must contain only generic UNIX code that
00016 //===          is guaranteed to work on *all* UNIX variants.
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "Unix.h"
00020 
00021 namespace llvm {
00022   using namespace sys;
00023 
00024 std::string TimeValue::str() const {
00025   time_t OurTime = time_t(this->toEpochTime());
00026   struct tm Storage;
00027   struct tm *LT = ::localtime_r(&OurTime, &Storage);
00028   assert(LT);
00029   char Buffer1[sizeof("YYYY-MM-DD HH:MM:SS")];
00030   strftime(Buffer1, sizeof(Buffer1), "%Y-%m-%d %H:%M:%S", LT);
00031   char Buffer2[sizeof("YYYY-MM-DD HH:MM:SS.MMMUUUNNN")];
00032   snprintf(Buffer2, sizeof(Buffer2), "%s.%.9u", Buffer1, this->nanoseconds());
00033   return std::string(Buffer2);
00034 }
00035 
00036 TimeValue TimeValue::now() {
00037   struct timeval the_time;
00038   timerclear(&the_time);
00039   if (0 != ::gettimeofday(&the_time,nullptr)) {
00040     // This is *really* unlikely to occur because the only gettimeofday
00041     // errors concern the timezone parameter which we're passing in as 0.
00042     // In the unlikely case it does happen, just return MinTime, no error
00043     // message needed.
00044     return MinTime();
00045   }
00046 
00047   return TimeValue(
00048     static_cast<TimeValue::SecondsType>( the_time.tv_sec +
00049       PosixZeroTimeSeconds ),
00050     static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
00051       NANOSECONDS_PER_MICROSECOND ) );
00052 }
00053 
00054 }