xref: /aosp_15_r20/external/llvm/lib/Support/Unix/TimeValue.inc (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker//===- Unix/TimeValue.cpp - Unix TimeValue Implementation -------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker//
3*9880d681SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker//
5*9880d681SAndroid Build Coastguard Worker// This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker// License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker//
8*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker//
10*9880d681SAndroid Build Coastguard Worker// This file implements the Unix specific portion of the TimeValue class.
11*9880d681SAndroid Build Coastguard Worker//
12*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker//=== WARNING: Implementation here must contain only generic UNIX code that
16*9880d681SAndroid Build Coastguard Worker//===          is guaranteed to work on *all* UNIX variants.
17*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker#include "Unix.h"
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Workernamespace llvm {
22*9880d681SAndroid Build Coastguard Worker  using namespace sys;
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Workerstd::string TimeValue::str() const {
25*9880d681SAndroid Build Coastguard Worker  time_t OurTime = time_t(this->toEpochTime());
26*9880d681SAndroid Build Coastguard Worker  struct tm Storage;
27*9880d681SAndroid Build Coastguard Worker  struct tm *LT = ::localtime_r(&OurTime, &Storage);
28*9880d681SAndroid Build Coastguard Worker  assert(LT);
29*9880d681SAndroid Build Coastguard Worker  char Buffer1[sizeof("YYYY-MM-DD HH:MM:SS")];
30*9880d681SAndroid Build Coastguard Worker  strftime(Buffer1, sizeof(Buffer1), "%Y-%m-%d %H:%M:%S", LT);
31*9880d681SAndroid Build Coastguard Worker  char Buffer2[sizeof("YYYY-MM-DD HH:MM:SS.MMMUUUNNN")];
32*9880d681SAndroid Build Coastguard Worker  snprintf(Buffer2, sizeof(Buffer2), "%s.%.9u", Buffer1, this->nanoseconds());
33*9880d681SAndroid Build Coastguard Worker  return std::string(Buffer2);
34*9880d681SAndroid Build Coastguard Worker}
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard WorkerTimeValue TimeValue::now() {
37*9880d681SAndroid Build Coastguard Worker  struct timeval the_time;
38*9880d681SAndroid Build Coastguard Worker  timerclear(&the_time);
39*9880d681SAndroid Build Coastguard Worker  if (0 != ::gettimeofday(&the_time,nullptr)) {
40*9880d681SAndroid Build Coastguard Worker    // This is *really* unlikely to occur because the only gettimeofday
41*9880d681SAndroid Build Coastguard Worker    // errors concern the timezone parameter which we're passing in as 0.
42*9880d681SAndroid Build Coastguard Worker    // In the unlikely case it does happen, just return MinTime, no error
43*9880d681SAndroid Build Coastguard Worker    // message needed.
44*9880d681SAndroid Build Coastguard Worker    return MinTime();
45*9880d681SAndroid Build Coastguard Worker  }
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker  return TimeValue(
48*9880d681SAndroid Build Coastguard Worker    static_cast<TimeValue::SecondsType>( the_time.tv_sec +
49*9880d681SAndroid Build Coastguard Worker      PosixZeroTimeSeconds ),
50*9880d681SAndroid Build Coastguard Worker    static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
51*9880d681SAndroid Build Coastguard Worker      NANOSECONDS_PER_MICROSECOND ) );
52*9880d681SAndroid Build Coastguard Worker}
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker}
55