TimeValue.inc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===- Unix/TimeValue.cpp - Unix 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 implements the Unix specific portion of the TimeValue class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //===----------------------------------------------------------------------===//
  14. //=== WARNING: Implementation here must contain only generic UNIX code that
  15. //=== is guaranteed to work on *all* UNIX variants.
  16. //===----------------------------------------------------------------------===//
  17. #include "Unix.h"
  18. namespace llvm {
  19. using namespace sys;
  20. std::string TimeValue::str() const {
  21. time_t OurTime = time_t(this->toEpochTime());
  22. struct tm Storage;
  23. struct tm *LT = ::localtime_r(&OurTime, &Storage);
  24. assert(LT);
  25. char Buffer1[sizeof("YYYY-MM-DD HH:MM:SS")];
  26. strftime(Buffer1, sizeof(Buffer1), "%Y-%m-%d %H:%M:%S", LT);
  27. char Buffer2[sizeof("YYYY-MM-DD HH:MM:SS.MMMUUUNNN")];
  28. snprintf(Buffer2, sizeof(Buffer2), "%s.%.9u", Buffer1, this->nanoseconds());
  29. return std::string(Buffer2);
  30. }
  31. TimeValue TimeValue::now() {
  32. struct timeval the_time;
  33. timerclear(&the_time);
  34. if (0 != ::gettimeofday(&the_time,nullptr)) {
  35. // This is *really* unlikely to occur because the only gettimeofday
  36. // errors concern the timezone parameter which we're passing in as 0.
  37. // In the unlikely case it does happen, just return MinTime, no error
  38. // message needed.
  39. return MinTime();
  40. }
  41. return TimeValue(
  42. static_cast<TimeValue::SecondsType>( the_time.tv_sec +
  43. PosixZeroTimeSeconds ),
  44. static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
  45. NANOSECONDS_PER_MICROSECOND ) );
  46. }
  47. }