blob: 80532b0b952489c7a4f3b76010c1b8799670ecae [file] [log] [blame]
Reid Spencer1cc19942004-09-25 05:03:54 +00001//===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer1cc19942004-09-25 05:03:54 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer1cc19942004-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 Spencer0d455992004-11-14 22:10:08 +000021namespace llvm {
22 using namespace sys;
23
Chris Lattnerc521f542009-08-23 22:45:37 +000024std::string TimeValue::str() const {
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000025 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 Spencer0d455992004-11-14 22:10:08 +000032}
33
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000034TimeValue TimeValue::now() {
35 struct timeval the_time;
Reid Spencer315efa12004-11-15 04:42:44 +000036 timerclear(&the_time);
Reid Spencerb1f99352006-08-22 17:38:44 +000037 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. Spencer447762d2010-11-29 18:16:10 +000041 // message needed.
Reid Spencerb1f99352006-08-22 17:38:44 +000042 return MinTime;
43 }
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000044
45 return TimeValue(
Alexey Samsonovf940f0c2013-02-19 11:35:39 +000046 static_cast<TimeValue::SecondsType>( the_time.tv_sec +
47 PosixZeroTimeSeconds ),
Michael J. Spencer447762d2010-11-29 18:16:10 +000048 static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000049 NANOSECONDS_PER_MICROSECOND ) );
50}
51
Reid Spencer1cc19942004-09-25 05:03:54 +000052}