1218885Sdim//===- Win32/TimeValue.cpp - Win32 TimeValue Implementation -----*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This file provides the Win32 implementation of the TimeValue class.
11218885Sdim//
12218885Sdim//===----------------------------------------------------------------------===//
13218885Sdim
14218885Sdim#include "Windows.h"
15263508Sdim#include <cctype>
16218885Sdim#include <time.h>
17218885Sdim
18263508Sdimusing namespace llvm;
19263508Sdimusing namespace llvm::sys;
20218885Sdim
21218885Sdim//===----------------------------------------------------------------------===//
22218885Sdim//=== WARNING: Implementation here must contain only Win32 specific code.
23218885Sdim//===----------------------------------------------------------------------===//
24218885Sdim
25218885SdimTimeValue TimeValue::now() {
26218885Sdim  uint64_t ft;
27218885Sdim  GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
28218885Sdim
29218885Sdim  TimeValue t(0, 0);
30218885Sdim  t.fromWin32Time(ft);
31218885Sdim  return t;
32218885Sdim}
33218885Sdim
34218885Sdimstd::string TimeValue::str() const {
35263508Sdim  struct tm *LT;
36218885Sdim#ifdef __MINGW32__
37263508Sdim  // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
38263508Sdim  // for them.
39263508Sdim  time_t OurTime = time_t(this->toEpochTime());
40263508Sdim  LT = ::localtime(&OurTime);
41263508Sdim  assert(LT);
42218885Sdim#else
43263508Sdim  struct tm Storage;
44263508Sdim  __time64_t OurTime = this->toEpochTime();
45263508Sdim  int Error = ::_localtime64_s(&Storage, &OurTime);
46263508Sdim  assert(!Error);
47263508Sdim  LT = &Storage;
48218885Sdim#endif
49218885Sdim
50263508Sdim  char Buffer[25];
51263508Sdim  // FIXME: the windows version of strftime doesn't support %e
52263508Sdim  strftime(Buffer, 25, "%b %d %H:%M %Y", LT);
53263508Sdim  assert((Buffer[3] == ' ' && isdigit(Buffer[5]) && Buffer[6] == ' ') &&
54263508Sdim         "Unexpected format in strftime()!");
55263508Sdim  // Emulate %e on %d to mute '0'.
56263508Sdim  if (Buffer[4] == '0')
57263508Sdim    Buffer[4] = ' ';
58263508Sdim  return std::string(Buffer);
59218885Sdim}
60