blob: 042e0dacc346ccfbab772cc3d5a9991fe37765be [file] [log] [blame]
Reid Spencer1cc19942004-09-25 05:03:54 +00001//===- Unix/TimeValue.cpp - Unix 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//
10// This file implements the Unix specific portion of the TimeValue class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
16//=== is guaranteed to work on *all* UNIX variants.
17//===----------------------------------------------------------------------===//
18
19#include "Unix.h"
20
Reid Spencer0d455992004-11-14 22:10:08 +000021namespace llvm {
22 using namespace sys;
23
Chris Lattnerc521f542009-08-23 22:45:37 +000024std::string TimeValue::str() const {
Rafael Espindolab1c1c5f2013-07-11 15:35:23 +000025 time_t OurTime = time_t(this->toEpochTime());
26 struct tm Storage;
27 struct tm *LT = ::localtime_r(&OurTime, &Storage);
28 assert(LT);
Chandler Carruth20c56932014-04-27 23:59:25 +000029 char Buffer1[sizeof("YYYY-MM-DD HH:MM:SS")];
30 strftime(Buffer1, sizeof(Buffer1), "%Y-%m-%d %H:%M:%S", LT);
31 char Buffer2[sizeof("YYYY-MM-DD HH:MM:SS.MMMUUUNNN")];
32 snprintf(Buffer2, sizeof(Buffer2), "%s.%.9u", Buffer1, this->nanoseconds());
33 return std::string(Buffer2);
Reid Spencer0d455992004-11-14 22:10:08 +000034}
35
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000036TimeValue TimeValue::now() {
37 struct timeval the_time;
Reid Spencer315efa12004-11-15 04:42:44 +000038 timerclear(&the_time);
Craig Toppere73658d2014-04-28 04:05:08 +000039 if (0 != ::gettimeofday(&the_time,nullptr)) {
Reid Spencerb1f99352006-08-22 17:38:44 +000040 // This is *really* unlikely to occur because the only gettimeofday
41 // errors concern the timezone parameter which we're passing in as 0.
42 // In the unlikely case it does happen, just return MinTime, no error
Michael J. Spencer447762d2010-11-29 18:16:10 +000043 // message needed.
Chris Bieneman5e7f44c2014-08-29 01:05:12 +000044 return MinTime();
Reid Spencerb1f99352006-08-22 17:38:44 +000045 }
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000046
47 return TimeValue(
Alexey Samsonovf940f0c2013-02-19 11:35:39 +000048 static_cast<TimeValue::SecondsType>( the_time.tv_sec +
49 PosixZeroTimeSeconds ),
Michael J. Spencer447762d2010-11-29 18:16:10 +000050 static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec *
Reid Spencerbfb7b3e2004-11-15 04:36:35 +000051 NANOSECONDS_PER_MICROSECOND ) );
52}
53
Alexander Kornienkof00654e2015-06-23 09:49:53 +000054}