1//===- Win32/TimeValue.cpp - Win32 TimeValue Implementation -----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 implementation of the TimeValue class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "WindowsSupport.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cctype>
18#include <time.h>
19
20using namespace llvm;
21using namespace llvm::sys;
22
23//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only Win32 specific code.
25//===----------------------------------------------------------------------===//
26
27TimeValue TimeValue::now() {
28  uint64_t ft;
29  GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
30
31  TimeValue t(0, 0);
32  t.fromWin32Time(ft);
33  return t;
34}
35
36std::string TimeValue::str() const {
37  std::string S;
38  struct tm *LT;
39#ifdef __MINGW32__
40  // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
41  // for them.
42  time_t OurTime = time_t(this->toEpochTime());
43  LT = ::localtime(&OurTime);
44  assert(LT);
45#else
46  struct tm Storage;
47  __time64_t OurTime = this->toEpochTime();
48  int Error = ::_localtime64_s(&Storage, &OurTime);
49  assert(!Error);
50  LT = &Storage;
51#endif
52
53  char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
54  strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", LT);
55  raw_string_ostream OS(S);
56  OS << format("%s.%.9u", static_cast<const char *>(Buffer),
57               this->nanoseconds());
58  OS.flush();
59  return S;
60}
61