LLVM API Documentation

Windows/TimeValue.inc
Go to the documentation of this file.
00001 //===- Win32/TimeValue.cpp - Win32 TimeValue Implementation -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides the Win32 implementation of the TimeValue class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "WindowsSupport.h"
00015 #include "llvm/Support/Format.h"
00016 #include "llvm/Support/raw_ostream.h"
00017 #include <cctype>
00018 #include <time.h>
00019 
00020 using namespace llvm;
00021 using namespace llvm::sys;
00022 
00023 //===----------------------------------------------------------------------===//
00024 //=== WARNING: Implementation here must contain only Win32 specific code.
00025 //===----------------------------------------------------------------------===//
00026 
00027 TimeValue TimeValue::now() {
00028   uint64_t ft;
00029   GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
00030 
00031   TimeValue t(0, 0);
00032   t.fromWin32Time(ft);
00033   return t;
00034 }
00035 
00036 std::string TimeValue::str() const {
00037   std::string S;
00038   struct tm *LT;
00039 #ifdef __MINGW32__
00040   // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
00041   // for them.
00042   time_t OurTime = time_t(this->toEpochTime());
00043   LT = ::localtime(&OurTime);
00044   assert(LT);
00045 #else
00046   struct tm Storage;
00047   __time64_t OurTime = this->toEpochTime();
00048   int Error = ::_localtime64_s(&Storage, &OurTime);
00049   assert(!Error);
00050   LT = &Storage;
00051 #endif
00052 
00053   char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
00054   strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", LT);
00055   raw_string_ostream OS(S);
00056   OS << format("%s.%.9u", static_cast<const char *>(Buffer),
00057                this->nanoseconds());
00058   OS.flush();
00059   return S;
00060 }