98b07d6e4479f0c15d28db13a5a9108c3c5d5498
[oota-llvm.git] / lib / Support / Windows / TimeValue.inc
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 "Windows.h"
15 #include <cctype>
16 #include <time.h>
17
18 using namespace llvm;
19 using namespace llvm::sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code.
23 //===----------------------------------------------------------------------===//
24
25 TimeValue TimeValue::now() {
26   uint64_t ft;
27   GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
28
29   TimeValue t(0, 0);
30   t.fromWin32Time(ft);
31   return t;
32 }
33
34 std::string TimeValue::str() const {
35   struct tm *LT;
36 #ifdef __MINGW32__
37   // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
38   // for them.
39   time_t OurTime = time_t(this->toEpochTime());
40   LT = ::localtime(&OurTime);
41   assert(LT);
42 #else
43   struct tm Storage;
44   __time64_t OurTime = this->toEpochTime();
45   int Error = ::_localtime64_s(&Storage, &OurTime);
46   assert(!Error);
47   LT = &Storage;
48 #endif
49
50   char Buffer[25];
51   // FIXME: the windows version of strftime doesn't support %e
52   strftime(Buffer, 25, "%b %d %H:%M %Y", LT);
53   assert((Buffer[3] == ' ' && isdigit(Buffer[5]) && Buffer[6] == ' ') &&
54          "Unexpected format in strftime()!");
55   // Emulate %e on %d to mute '0'.
56   if (Buffer[4] == '0')
57     Buffer[4] = ' ';
58   return std::string(Buffer);
59 }