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); |
Chandler Carruth | 20c5693 | 2014-04-27 23:59:25 +0000 | [diff] [blame] | 29 | char Buffer1[sizeof("YYYY-MM-DD HH:MM:SS")]; |
| 30 | strftime(Buffer1, sizeof(Buffer1), "%Y-%m-%d %H:%M:%S", LT); |
| 31 | char Buffer2[sizeof("YYYY-MM-DD HH:MM:SS.MMMUUUNNN")]; |
| 32 | snprintf(Buffer2, sizeof(Buffer2), "%s.%.9u", Buffer1, this->nanoseconds()); |
| 33 | return std::string(Buffer2); |
Reid Spencer | 0d45599 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 36 | TimeValue TimeValue::now() { |
| 37 | struct timeval the_time; |
Reid Spencer | 315efa1 | 2004-11-15 04:42:44 +0000 | [diff] [blame] | 38 | timerclear(&the_time); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 39 | if (0 != ::gettimeofday(&the_time,nullptr)) { |
Reid Spencer | b1f9935 | 2006-08-22 17:38:44 +0000 | [diff] [blame] | 40 | // This is *really* unlikely to occur because the only gettimeofday |
| 41 | // errors concern the timezone parameter which we're passing in as 0. |
| 42 | // 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] | 43 | // message needed. |
Chris Bieneman | 5e7f44c | 2014-08-29 01:05:12 +0000 | [diff] [blame] | 44 | return MinTime(); |
Reid Spencer | b1f9935 | 2006-08-22 17:38:44 +0000 | [diff] [blame] | 45 | } |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 46 | |
| 47 | return TimeValue( |
Alexey Samsonov | f940f0c | 2013-02-19 11:35:39 +0000 | [diff] [blame] | 48 | static_cast<TimeValue::SecondsType>( the_time.tv_sec + |
| 49 | PosixZeroTimeSeconds ), |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 50 | static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec * |
Reid Spencer | bfb7b3e | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 51 | NANOSECONDS_PER_MICROSECOND ) ); |
| 52 | } |
| 53 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 54 | } |