2

I used this piece of code to get timestamp in microsecond in c/c++. but it doesn't look like microsecond. also i don't know if there is any way to format it.

timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
unsigned long micro = curTime.tv_usec*(uint64_t)1000000+curTime.tv_usec;

char buffer [80];
//localtime is not thread safe
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec));

char currentTime[84] = "";
char currentTime2[80] = "";
sprintf(currentTime, "%s:%3d", buffer, milli);
sprintf(currentTime2, "%s:%Lu", buffer, micro); 
printf("time %s, hptime %s\n", currentTime, currentTime2);

and what is the right format to output it? Thank you!

bbc
  • 1,061
  • 4
  • 13
  • 21
  • 1
    What is the significance of the line `unsigned long micro = curTime.tv_usec*(uint64_t)1000000+curTime.tv_usec;`? Would it not be better as `unsigned long micro = curTime.tv_sec*(uint64_t)1000000+curTime.tv_usec;`? – abiessu Mar 05 '14 at 16:25
  • I guess I didn't understand the code, it seems only unsigned long micro = curTime.tv_usec is needed. – bbc Mar 05 '14 at 16:32
  • Read also [time(7)](http://man7.org/linux/man-pages/man7/time.7.html) at least on Linux – Basile Starynkevitch Sep 29 '17 at 15:52
  • Related: [Getting an accurate execution time in C++ (micro seconds)](https://stackoverflow.com/questions/21856025/getting-an-accurate-execution-time-in-c-micro-seconds). – Gabriel Staples Dec 17 '20 at 23:48

4 Answers4

4

Something a bit shorter to try (C++):

using namespace std::chrono;
__int64 microseconds_since_epoch = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
C--
  • 16,393
  • 6
  • 53
  • 60
Lorien Brune
  • 830
  • 10
  • 16
2

The typical printing format for sub-second times uses the decimal indicator (. in many locales) and so 59 and some seconds might look like 59.00013.

The micro variable you created takes the current microsecond count, multiplies it by 1000000 then adds the current microsecond count again; I expect that you intend to either use the microsecond count alone, or together with the count of seconds:

unsigned long micro = curTime.tv_usec*(uint64_t)1000000+curTime.tv_usec;

should be written as

unsigned long micro = curTime.tv_sec*(uint64_t)1000000+curTime.tv_usec;

to get seconds and microseconds together in the same number.

To write this into your output, you might consider changing the line

sprintf(currentTime2, "%s:%Lu", buffer, micro);

to

sprintf(currentTime2, "%s.%Lu", buffer, curTime.tv_usec);

Using the altered micro definition, you can also output

sprintf(currentSeconds, "%.6f", micro / 1000000);
abiessu
  • 1,907
  • 1
  • 18
  • 16
2

Using Howard Hinnant's free, open-source, header-only date/time library:

#include "date/date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std;
    using namespace std::chrono;
    cout << floor<microseconds>(system_clock::now()) << '\n';
}

which just output for me:

2017-09-29 15:46:27.793195
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
0

If you use visual studio (Windows environment),
How about to using WINDOWS API : GetLocalTime()

  char* lptszBuffer = new char[100]; 
  SYSTEMTIME sysTime;
  GetLocalTime( &sysTime );
  sprintf_s(lptszBuffer, 100, "[%02d:%02d:%02d.%03d]", sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);

result format : [11:27:00.027]

Til
  • 5,150
  • 13
  • 26
  • 34
Jake Lee
  • 1
  • 2