blob: 98b07d6e4479f0c15d28db13a5a9108c3c5d5498 [file] [log] [blame]
Reid Spencer1cc19942004-09-25 05:03:54 +00001//===- Win32/TimeValue.cpp - Win32 TimeValue Implementation -----*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer1cc19942004-09-25 05:03:54 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer1cc19942004-09-25 05:03:54 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencerdacbf462004-11-15 04:47:22 +000010// This file provides the Win32 implementation of the TimeValue class.
Reid Spencer1cc19942004-09-25 05:03:54 +000011//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "Windows.h"
Will Dietz981af002013-10-12 00:55:57 +000015#include <cctype>
Jeff Cohen2b60d392004-12-14 05:26:43 +000016#include <time.h>
Reid Spencer1cc19942004-09-25 05:03:54 +000017
David Majnemer93fdc3f2013-10-14 01:17:32 +000018using namespace llvm;
19using namespace llvm::sys;
Reid Spencer1cc19942004-09-25 05:03:54 +000020
21//===----------------------------------------------------------------------===//
Reid Spencer364cce42004-09-28 23:56:20 +000022//=== WARNING: Implementation here must contain only Win32 specific code.
Reid Spencer1cc19942004-09-25 05:03:54 +000023//===----------------------------------------------------------------------===//
24
Reid Spencer364cce42004-09-28 23:56:20 +000025TimeValue TimeValue::now() {
Jeff Cohen2b60d392004-12-14 05:26:43 +000026 uint64_t ft;
Reid Spencer364cce42004-09-28 23:56:20 +000027 GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
28
Jeff Cohen2b60d392004-12-14 05:26:43 +000029 TimeValue t(0, 0);
30 t.fromWin32Time(ft);
31 return t;
Reid Spencer364cce42004-09-28 23:56:20 +000032}
Reid Spencer1cc19942004-09-25 05:03:54 +000033
Chris Lattnerc521f542009-08-23 22:45:37 +000034std::string TimeValue::str() const {
Rafael Espindola17618242013-07-11 16:11:21 +000035 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;
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000044 __time64_t OurTime = this->toEpochTime();
Rafael Espindola17618242013-07-11 16:11:21 +000045 int Error = ::_localtime64_s(&Storage, &OurTime);
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000046 assert(!Error);
Rafael Espindola17618242013-07-11 16:11:21 +000047 LT = &Storage;
48#endif
Jeff Cohen2b60d392004-12-14 05:26:43 +000049
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000050 char Buffer[25];
51 // FIXME: the windows version of strftime doesn't support %e
Rafael Espindola17618242013-07-11 16:11:21 +000052 strftime(Buffer, 25, "%b %d %H:%M %Y", LT);
David Majnemer93fdc3f2013-10-14 01:17:32 +000053 assert((Buffer[3] == ' ' && isdigit(Buffer[5]) && Buffer[6] == ' ') &&
NAKAMURA Takumi40bd28a2013-07-12 02:13:03 +000054 "Unexpected format in strftime()!");
55 // Emulate %e on %d to mute '0'.
56 if (Buffer[4] == '0')
57 Buffer[4] = ' ';
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000058 return std::string(Buffer);
Reid Spencer09e9f2a2004-11-16 06:22:17 +000059}