blob: b90b4f1da008693ed2c371fad9ec14f96303fbcb [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
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000014#include "WindowsSupport.h"
Chandler Carruthe4c39942014-04-28 01:57:46 +000015#include "llvm/Support/Format.h"
16#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000017#include <cctype>
Jeff Cohen2b60d392004-12-14 05:26:43 +000018#include <time.h>
Reid Spencer1cc19942004-09-25 05:03:54 +000019
David Majnemer93fdc3f2013-10-14 01:17:32 +000020using namespace llvm;
21using namespace llvm::sys;
Reid Spencer1cc19942004-09-25 05:03:54 +000022
23//===----------------------------------------------------------------------===//
Reid Spencer364cce42004-09-28 23:56:20 +000024//=== WARNING: Implementation here must contain only Win32 specific code.
Reid Spencer1cc19942004-09-25 05:03:54 +000025//===----------------------------------------------------------------------===//
26
Reid Spencer364cce42004-09-28 23:56:20 +000027TimeValue TimeValue::now() {
Jeff Cohen2b60d392004-12-14 05:26:43 +000028 uint64_t ft;
Reid Spencer364cce42004-09-28 23:56:20 +000029 GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
30
Jeff Cohen2b60d392004-12-14 05:26:43 +000031 TimeValue t(0, 0);
32 t.fromWin32Time(ft);
33 return t;
Reid Spencer364cce42004-09-28 23:56:20 +000034}
Reid Spencer1cc19942004-09-25 05:03:54 +000035
Chris Lattnerc521f542009-08-23 22:45:37 +000036std::string TimeValue::str() const {
Chandler Carruthe4c39942014-04-28 01:57:46 +000037 std::string S;
Rafael Espindola17618242013-07-11 16:11:21 +000038 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;
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000047 __time64_t OurTime = this->toEpochTime();
Rafael Espindola17618242013-07-11 16:11:21 +000048 int Error = ::_localtime64_s(&Storage, &OurTime);
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000049 assert(!Error);
Yaron Keren96acdf62015-04-15 07:45:52 +000050 (void)Error;
Rafael Espindola17618242013-07-11 16:11:21 +000051 LT = &Storage;
52#endif
Jeff Cohen2b60d392004-12-14 05:26:43 +000053
Chandler Carruthe4c39942014-04-28 01:57:46 +000054 char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
55 strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", LT);
56 raw_string_ostream OS(S);
57 OS << format("%s.%.9u", static_cast<const char *>(Buffer),
58 this->nanoseconds());
59 OS.flush();
60 return S;
Reid Spencer09e9f2a2004-11-16 06:22:17 +000061}