Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 1 | //===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 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 | 0d45599 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | using namespace sys; |
| 23 | |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 24 | std::string TimeValue::str() const { |
Rafael Espindola | b1c1c5f | 2013-07-11 15:35:23 +0000 | [diff] [blame^] | 25 | time_t OurTime = time_t(this->toEpochTime()); |
| 26 | struct tm Storage; |
| 27 | struct tm *LT = ::localtime_r(&OurTime, &Storage); |
| 28 | assert(LT); |
| 29 | char Buffer[25]; |
| 30 | strftime(Buffer, 25, "%b %e %H:%M %Y", LT); |
| 31 | return std::string(Buffer); |
Reid Spencer | 0d45599 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 34 | TimeValue TimeValue::now() { |
| 35 | struct timeval the_time; |
Reid Spencer | 315efa1 | 2004-11-15 04:42:44 +0000 | [diff] [blame] | 36 | timerclear(&the_time); |
Reid Spencer | b1f9935 | 2006-08-22 17:38:44 +0000 | [diff] [blame] | 37 | if (0 != ::gettimeofday(&the_time,0)) { |
| 38 | // This is *really* unlikely to occur because the only gettimeofday |
| 39 | // errors concern the timezone parameter which we're passing in as 0. |
| 40 | // In the unlikely case it does happen, just return MinTime, no error |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 41 | // message needed. |
Reid Spencer | b1f9935 | 2006-08-22 17:38:44 +0000 | [diff] [blame] | 42 | return MinTime; |
| 43 | } |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 44 | |
| 45 | return TimeValue( |
Alexey Samsonov | f940f0c | 2013-02-19 11:35:39 +0000 | [diff] [blame] | 46 | static_cast<TimeValue::SecondsType>( the_time.tv_sec + |
| 47 | PosixZeroTimeSeconds ), |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 48 | static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec * |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 49 | NANOSECONDS_PER_MICROSECOND ) ); |
| 50 | } |
| 51 | |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 52 | } |