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 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | #include <time.h> |
Reid Spencer | 00b5df4 | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 22 | #include <sys/time.h> |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | using namespace sys; |
| 26 | |
| 27 | |
Reid Spencer | 8bf7fba | 2004-11-16 06:22:17 +0000 | [diff] [blame] | 28 | std::string TimeValue::toString() const { |
Reid Spencer | d351871 | 2004-11-14 22:10:08 +0000 | [diff] [blame] | 29 | char buffer[32]; |
| 30 | |
| 31 | time_t ourTime = time_t(this->toEpochTime()); |
| 32 | ::asctime_r(::localtime(&ourTime), buffer); |
| 33 | |
| 34 | std::string result(buffer); |
| 35 | return result.substr(0,24); |
| 36 | } |
| 37 | |
Reid Spencer | 00b5df4 | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 38 | TimeValue TimeValue::now() { |
| 39 | struct timeval the_time; |
Reid Spencer | 2e83ea5 | 2004-11-15 04:42:44 +0000 | [diff] [blame] | 40 | timerclear(&the_time); |
Reid Spencer | 00b5df4 | 2004-11-15 04:36:35 +0000 | [diff] [blame] | 41 | if (0 != ::gettimeofday(&the_time,0)) |
| 42 | ThrowErrno("Couldn't obtain time of day"); |
| 43 | |
| 44 | return TimeValue( |
| 45 | static_cast<TimeValue::SecondsType>( the_time.tv_sec ), |
| 46 | static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec * |
| 47 | NANOSECONDS_PER_MICROSECOND ) ); |
| 48 | } |
| 49 | |
Reid Spencer | 0d5716e | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 50 | } |
| 51 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |