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   char buffer[32];
00026 
00027   time_t ourTime = time_t(this->toEpochTime());
00028 #ifdef __hpux
00029 // note that the following line needs -D_REENTRANT on HP-UX to be picked up
00030   asctime_r(localtime(&ourTime), buffer);
00031 #else
00032   ::asctime_r(::localtime(&ourTime), buffer);
00033 #endif
00034 
00035   std::string result(buffer);
00036   return result.substr(0,24);
00037 }
00038 
00039 TimeValue TimeValue::now() {
00040   struct timeval the_time;
00041   timerclear(&the_time);
00042   if (0 != ::gettimeofday(&the_time,0)) {
00043     // This is *really* unlikely to occur because the only gettimeofday
00044     // errors concern the timezone parameter which we're passing in as 0.
00045     // In the unlikely case it does happen, just return MinTime, no error
00046     // message needed.
00047     return MinTime;
00048   }
00049 
00050   return TimeValue(
00051     static_cast<TimeValue::SecondsType>( the_time.tv_sec +
00052       PosixZeroTimeSeconds ),
00053     static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
00054       NANOSECONDS_PER_MICROSECOND ) );
00055 }
00056 
00057 }