xref: /aosp_15_r20/external/llvm/lib/Support/Windows/TimeValue.inc (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker//===- Win32/TimeValue.cpp - Win32 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 provides the Win32 implementation of the TimeValue class.
11*9880d681SAndroid Build Coastguard Worker//
12*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker#include "WindowsSupport.h"
15*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/Format.h"
16*9880d681SAndroid Build Coastguard Worker#include "llvm/Support/raw_ostream.h"
17*9880d681SAndroid Build Coastguard Worker#include <cctype>
18*9880d681SAndroid Build Coastguard Worker#include <time.h>
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Workerusing namespace llvm;
21*9880d681SAndroid Build Coastguard Workerusing namespace llvm::sys;
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
24*9880d681SAndroid Build Coastguard Worker//=== WARNING: Implementation here must contain only Win32 specific code.
25*9880d681SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard WorkerTimeValue TimeValue::now() {
28*9880d681SAndroid Build Coastguard Worker  uint64_t ft;
29*9880d681SAndroid Build Coastguard Worker  GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker  TimeValue t(0, 0);
32*9880d681SAndroid Build Coastguard Worker  t.fromWin32Time(ft);
33*9880d681SAndroid Build Coastguard Worker  return t;
34*9880d681SAndroid Build Coastguard Worker}
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Workerstd::string TimeValue::str() const {
37*9880d681SAndroid Build Coastguard Worker  std::string S;
38*9880d681SAndroid Build Coastguard Worker  struct tm *LT;
39*9880d681SAndroid Build Coastguard Worker#ifdef __MINGW32__
40*9880d681SAndroid Build Coastguard Worker  // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
41*9880d681SAndroid Build Coastguard Worker  // for them.
42*9880d681SAndroid Build Coastguard Worker  time_t OurTime = time_t(this->toEpochTime());
43*9880d681SAndroid Build Coastguard Worker  LT = ::localtime(&OurTime);
44*9880d681SAndroid Build Coastguard Worker  assert(LT);
45*9880d681SAndroid Build Coastguard Worker#else
46*9880d681SAndroid Build Coastguard Worker  struct tm Storage;
47*9880d681SAndroid Build Coastguard Worker  __time64_t OurTime = this->toEpochTime();
48*9880d681SAndroid Build Coastguard Worker  int Error = ::_localtime64_s(&Storage, &OurTime);
49*9880d681SAndroid Build Coastguard Worker  assert(!Error);
50*9880d681SAndroid Build Coastguard Worker  (void)Error;
51*9880d681SAndroid Build Coastguard Worker  LT = &Storage;
52*9880d681SAndroid Build Coastguard Worker#endif
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker  char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
55*9880d681SAndroid Build Coastguard Worker  strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", LT);
56*9880d681SAndroid Build Coastguard Worker  raw_string_ostream OS(S);
57*9880d681SAndroid Build Coastguard Worker  OS << format("%s.%.9u", static_cast<const char *>(Buffer),
58*9880d681SAndroid Build Coastguard Worker               this->nanoseconds());
59*9880d681SAndroid Build Coastguard Worker  OS.flush();
60*9880d681SAndroid Build Coastguard Worker  return S;
61*9880d681SAndroid Build Coastguard Worker}
62