Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 1 | //===- Win32/TimeValue.cpp - Win32 TimeValue Implementation -----*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | dacbf46 | 2004-11-15 04:47:22 +0000 | [diff] [blame] | 10 | // This file provides the Win32 implementation of the TimeValue class. |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 14 | #include "WindowsSupport.h" |
Chandler Carruth | e4c3994 | 2014-04-28 01:57:46 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Format.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 17 | #include <cctype> |
Jeff Cohen | 2b60d39 | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 18 | #include <time.h> |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 19 | |
David Majnemer | 93fdc3f | 2013-10-14 01:17:32 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | using namespace llvm::sys; |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 22 | |
| 23 | //===----------------------------------------------------------------------===// |
Reid Spencer | 364cce4 | 2004-09-28 23:56:20 +0000 | [diff] [blame] | 24 | //=== WARNING: Implementation here must contain only Win32 specific code. |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Reid Spencer | 364cce4 | 2004-09-28 23:56:20 +0000 | [diff] [blame] | 27 | TimeValue TimeValue::now() { |
Jeff Cohen | 2b60d39 | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 28 | uint64_t ft; |
Reid Spencer | 364cce4 | 2004-09-28 23:56:20 +0000 | [diff] [blame] | 29 | GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft)); |
| 30 | |
Jeff Cohen | 2b60d39 | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 31 | TimeValue t(0, 0); |
| 32 | t.fromWin32Time(ft); |
| 33 | return t; |
Reid Spencer | 364cce4 | 2004-09-28 23:56:20 +0000 | [diff] [blame] | 34 | } |
Reid Spencer | 1cc1994 | 2004-09-25 05:03:54 +0000 | [diff] [blame] | 35 | |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 36 | std::string TimeValue::str() const { |
Chandler Carruth | e4c3994 | 2014-04-28 01:57:46 +0000 | [diff] [blame] | 37 | std::string S; |
Rafael Espindola | 1761824 | 2013-07-11 16:11:21 +0000 | [diff] [blame] | 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; |
Rafael Espindola | b1c1c5f | 2013-07-11 15:35:23 +0000 | [diff] [blame] | 47 | __time64_t OurTime = this->toEpochTime(); |
Rafael Espindola | 1761824 | 2013-07-11 16:11:21 +0000 | [diff] [blame] | 48 | int Error = ::_localtime64_s(&Storage, &OurTime); |
Rafael Espindola | b1c1c5f | 2013-07-11 15:35:23 +0000 | [diff] [blame] | 49 | assert(!Error); |
Yaron Keren | 96acdf6 | 2015-04-15 07:45:52 +0000 | [diff] [blame] | 50 | (void)Error; |
Rafael Espindola | 1761824 | 2013-07-11 16:11:21 +0000 | [diff] [blame] | 51 | LT = &Storage; |
| 52 | #endif |
Jeff Cohen | 2b60d39 | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 53 | |
Chandler Carruth | e4c3994 | 2014-04-28 01:57:46 +0000 | [diff] [blame] | 54 | 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 Spencer | 09e9f2a | 2004-11-16 06:22:17 +0000 | [diff] [blame] | 61 | } |