Reid Spencer | 0d5716e | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 1 | //===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 0d5716e | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Unix specific portion of the TimeValue class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on *all* UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "Unix.h" |
| 20 | |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | using namespace sys; |
| 23 | |
Reid Spencer | 8bf7fba | 2004-11-16 06:22:17 +0000 | [diff] [blame] | 24 | std::string TimeValue::toString() const { |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 25 | char buffer[32]; |
| 26 | |
| 27 | time_t ourTime = time_t(this->toEpochTime()); |
Reid Spencer | 6258272 | 2005-01-14 00:50:50 +0000 | [diff] [blame] | 28 | #ifdef __hpux |
Duraid Madina | 26b1d42 | 2005-05-16 06:59:53 +0000 | [diff] [blame] | 29 | // note that the following line needs -D_REENTRANT on HP-UX to be picked up |
Reid Spencer | 6258272 | 2005-01-14 00:50:50 +0000 | [diff] [blame] | 30 | asctime_r(localtime(&ourTime), buffer); |
| 31 | #else |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 32 | ::asctime_r(::localtime(&ourTime), buffer); |
Reid Spencer | 6258272 | 2005-01-14 00:50:50 +0000 | [diff] [blame] | 33 | #endif |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 34 | |
| 35 | std::string result(buffer); |
| 36 | return result.substr(0,24); |
| 37 | } |
| 38 | |
Reid Spencer | 00b5df4 | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 39 | TimeValue TimeValue::now() { |
| 40 | struct timeval the_time; |
Reid Spencer | 2e83ea5 | 2004-11-15 04:42:44 +0000 | [diff] [blame] | 41 | timerclear(&the_time); |
Reid Spencer | d8509c9 | 2006-08-22 17:38:44 +0000 | [diff] [blame] | 42 | if (0 != ::gettimeofday(&the_time,0)) { |
| 43 | // This is *really* unlikely to occur because the only gettimeofday |
| 44 | // errors concern the timezone parameter which we're passing in as 0. |
| 45 | // In the unlikely case it does happen, just return MinTime, no error |
| 46 | // message needed. |
| 47 | return MinTime; |
| 48 | } |
Reid Spencer | 00b5df4 | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 49 | |
| 50 | return TimeValue( |
| 51 | static_cast<TimeValue::SecondsType>( the_time.tv_sec ), |
| 52 | static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec * |
| 53 | NANOSECONDS_PER_MICROSECOND ) ); |
| 54 | } |
| 55 | |
Reid Spencer | 0d5716e | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 56 | } |