TimeValue.inc revision 249423
1178476Sjb//===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
2178476Sjb//
3178476Sjb//                     The LLVM Compiler Infrastructure
4178476Sjb//
5178476Sjb// This file is distributed under the University of Illinois Open Source
6178476Sjb// License. See LICENSE.TXT for details.
7178476Sjb//
8178476Sjb//===----------------------------------------------------------------------===//
9178476Sjb//
10178476Sjb// This file implements the Unix specific portion of the TimeValue class.
11178476Sjb//
12178476Sjb//===----------------------------------------------------------------------===//
13178476Sjb
14178476Sjb//===----------------------------------------------------------------------===//
15178476Sjb//=== WARNING: Implementation here must contain only generic UNIX code that
16178476Sjb//===          is guaranteed to work on *all* UNIX variants.
17178476Sjb//===----------------------------------------------------------------------===//
18178476Sjb
19178476Sjb#include "Unix.h"
20178476Sjb
21178476Sjbnamespace llvm {
22178476Sjb  using namespace sys;
23178476Sjb
24178476Sjbstd::string TimeValue::str() const {
25178476Sjb  char buffer[32];
26178476Sjb
27178476Sjb  time_t ourTime = time_t(this->toEpochTime());
28178476Sjb#ifdef __hpux
29178476Sjb// note that the following line needs -D_REENTRANT on HP-UX to be picked up
30178476Sjb  asctime_r(localtime(&ourTime), buffer);
31178476Sjb#else
32178476Sjb  ::asctime_r(::localtime(&ourTime), buffer);
33178476Sjb#endif
34178476Sjb
35178476Sjb  std::string result(buffer);
36178476Sjb  return result.substr(0,24);
37178476Sjb}
38178476Sjb
39178476SjbTimeValue TimeValue::now() {
40178476Sjb  struct timeval the_time;
41178476Sjb  timerclear(&the_time);
42178476Sjb  if (0 != ::gettimeofday(&the_time,0)) {
43178476Sjb    // This is *really* unlikely to occur because the only gettimeofday
44178476Sjb    // errors concern the timezone parameter which we're passing in as 0.
45178476Sjb    // In the unlikely case it does happen, just return MinTime, no error
46    // message needed.
47    return MinTime;
48  }
49
50  return TimeValue(
51    static_cast<TimeValue::SecondsType>( the_time.tv_sec +
52      PosixZeroTimeSeconds ),
53    static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
54      NANOSECONDS_PER_MICROSECOND ) );
55}
56
57}
58