TimeValue.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "WindowsSupport.h"
  14. #include "llvm/Support/Format.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cctype>
  17. #include <time.h>
  18. using namespace llvm;
  19. using namespace llvm::sys;
  20. //===----------------------------------------------------------------------===//
  21. //=== WARNING: Implementation here must contain only Win32 specific code.
  22. //===----------------------------------------------------------------------===//
  23. TimeValue TimeValue::now() {
  24. uint64_t ft;
  25. GetSystemTimeAsFileTime(reinterpret_cast<FILETIME *>(&ft));
  26. TimeValue t(0, 0);
  27. t.fromWin32Time(ft);
  28. return t;
  29. }
  30. std::string TimeValue::str() const {
  31. std::string S;
  32. struct tm *LT;
  33. #ifdef __MINGW32__
  34. // Old versions of mingw don't have _localtime64_s. Remove this once we drop support
  35. // for them.
  36. time_t OurTime = time_t(this->toEpochTime());
  37. LT = ::localtime(&OurTime);
  38. assert(LT);
  39. #else
  40. struct tm Storage;
  41. __time64_t OurTime = this->toEpochTime();
  42. int Error = ::_localtime64_s(&Storage, &OurTime);
  43. assert(!Error);
  44. (void)Error;
  45. LT = &Storage;
  46. #endif
  47. char Buffer[sizeof("YYYY-MM-DD HH:MM:SS")];
  48. strftime(Buffer, sizeof(Buffer), "%Y-%m-%d %H:%M:%S", LT);
  49. raw_string_ostream OS(S);
  50. OS << format("%s.%.9u", static_cast<const char *>(Buffer),
  51. this->nanoseconds());
  52. OS.flush();
  53. return S;
  54. }