1 //===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific portion of the TimeValue class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on *all* UNIX variants.
17 //===----------------------------------------------------------------------===//
24 std::string TimeValue::str() const {
25 time_t OurTime = time_t(this->toEpochTime());
27 struct tm *LT = ::localtime_r(&OurTime, &Storage);
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);
36 TimeValue TimeValue::now() {
37 struct timeval the_time;
38 timerclear(&the_time);
39 if (0 != ::gettimeofday(&the_time,nullptr)) {
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
48 static_cast<TimeValue::SecondsType>( the_time.tv_sec +
49 PosixZeroTimeSeconds ),
50 static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
51 NANOSECONDS_PER_MICROSECOND ) );