Solved

Display time in milliseconds in C++/C linux /unix

Posted on 2013-12-08
5
10,015 Views
Last Modified: 2013-12-12
Hi guys,

I need help on how to display time in milliseconds  in c/c++ under linux ?
0
Comment
Question by:bachra04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:sarabande
ID: 39705505
you would use the gettimeofday function:

struct timeval  tv = { 0 };
gettimeofday(&tv, NULL);

double mill = (tv.tv_usec) / 1000 ; 

Open in new window


Sara
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 39706212
If you need a bit more accuracy, use 'clock_gettime()' (http://linux.die.net/man/2/clock_gettime). You'll get a

struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

Open in new window


See e.g. http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/ ("Profiling Code Using clock_gettime"):

#include <time.h>

// ...

	struct timespec gettime_now;

	clock_gettime(CLOCK_REALTIME, &gettime_now);

Open in new window

0
 
LVL 2

Author Comment

by:bachra04
ID: 39706398
The problem I have seen is how to printf the value, I'm seeing weird values
267.0000
948.0000

then
15.0000


etc ...
I'm expecting that time of day will be increasing
0
 
LVL 86

Expert Comment

by:jkr
ID: 39706480
For both system calls? The man page to gettimeofday (http://linux.die.net/man/2/gettimeofday) states:

POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead. [...] The time returned by gettimeofday() is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the system time). If you need a monotonically increasing clock, see clock_gettime(2).

(even though I wouldn't expect such a weird behaviour)
0
 
LVL 34

Accepted Solution

by:
sarabande earned 250 total points
ID: 39706670
gettimeofday and clock_gettime have same accuracy. it is for Linux systems normally better than 1 millisecond (while for windows it is 15 ms). higher accuracy you only get by using high performance counters which is hardware of your mainboard if available and has a own api.

the numbers you showed are milliseconds expressed as doubles. if you use an int at left side of the calculation, the results should print correctly.

int mill = (int)(tv.tv_usec/ 1000);

Open in new window


if you want the seconds as hours, minutes and seconds you may do:

int hours = (int)(tv.tv_sec/3600);
int minutes = (int)((tv.tv_sec%3600)/60);
int seconds = (int)(tv.tv_sec%60);

Open in new window


to print it properly you may include <sstream> and <iomanip> headers and do like

std::ostringstream oss;
oss << std::right << std::setw(2) << std::setfill('0') << hours << ':'
       << std::right << std::setw(2) << std::setfill('0') << minutes << ':'
       << std::right << std::setw(2) << std::setfill('0') << seconds << '.'
       << std::right << std::setw(3) << std::setfill('0') << mill;
std::cout << oss.str();

Open in new window


Sara
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question