blob: 5cf5a9d44ed6374e97bbfff7d1f53934d544d208 [file] [log] [blame]
Reid Spencer0d5716e2004-09-25 05:03:54 +00001//===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00002//
Reid Spencer0d5716e2004-09-25 05:03:54 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00007//
Reid Spencer0d5716e2004-09-25 05:03:54 +00008//===----------------------------------------------------------------------===//
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 Spencerd3518712004-11-14 22:10:08 +000021namespace llvm {
22 using namespace sys;
23
Chris Lattner74382b72009-08-23 22:45:37 +000024std::string TimeValue::str() const {
Reid Spencerd3518712004-11-14 22:10:08 +000025 char buffer[32];
26
27 time_t ourTime = time_t(this->toEpochTime());
Reid Spencer62582722005-01-14 00:50:50 +000028#ifdef __hpux
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000029// note that the following line needs -D_REENTRANT on HP-UX to be picked up
Reid Spencer62582722005-01-14 00:50:50 +000030 asctime_r(localtime(&ourTime), buffer);
31#else
Reid Spencerd3518712004-11-14 22:10:08 +000032 ::asctime_r(::localtime(&ourTime), buffer);
Reid Spencer62582722005-01-14 00:50:50 +000033#endif
Reid Spencerd3518712004-11-14 22:10:08 +000034
35 std::string result(buffer);
36 return result.substr(0,24);
37}
38
Reid Spencer00b5df42004-11-15 04:36:35 +000039TimeValue TimeValue::now() {
40 struct timeval the_time;
Reid Spencer2e83ea52004-11-15 04:42:44 +000041 timerclear(&the_time);
Reid Spencerd8509c92006-08-22 17:38:44 +000042 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
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000046 // message needed.
Reid Spencerd8509c92006-08-22 17:38:44 +000047 return MinTime;
48 }
Reid Spencer00b5df42004-11-15 04:36:35 +000049
50 return TimeValue(
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000051 static_cast<TimeValue::SecondsType>( the_time.tv_sec + PosixZeroTime.seconds_ ),
52 static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
Reid Spencer00b5df42004-11-15 04:36:35 +000053 NANOSECONDS_PER_MICROSECOND ) );
54}
55
Reid Spencer0d5716e2004-09-25 05:03:54 +000056}