1

Possible Duplicate:
How do I get milliseconds since midnight UTC in C?

I'm trying to get the time elapsed in milliseconds between the start of the current day (12:00:00.0000 AM) to the current time. What would I use in time.h to achieve this?

Community
  • 1
  • 1
vertevero
  • 85
  • 3
  • 7
  • Why do you want to do this? Do you actually need the current _time of day_ to this resolution, or are you just timing something? There isn't a standard way in C to get the time of day with this accuracy. – andrewdski May 15 '11 at 01:24
  • I'm trying to code an accurate decimal time clock and if I program it based off of seconds, it skips every now and then as seen here: http://www.minkukel.com/en/time/metric_clock.htm – vertevero May 15 '11 at 01:38

3 Answers3

0

If you mean the time of day since epoch:

struct timeval time;
gettimeofday(&time, NULL);

If you mean the system time:

time_t t;
time(&t);
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • `gettimeofday` is Posix not std C, and may or may not be available. It is not in time.h (at least not on my system). `time` is similar, but is generally at one second resolution, so even less useful. – andrewdski May 15 '11 at 01:22
0

While I realize you asked for milliseconds, this is how you can get that in seconds:

time(NULL)%(24*60*60)
icktoofay
  • 126,289
  • 21
  • 250
  • 231
0

How do I get milliseconds since midnight UTC in C?

This is a simple way:

time_t seconds_since_midnight = time(NULL) % 86400;

To get approximate milliseconds since midnight, multiply seconds_since_midnight by 1000.

If you need more resolution (consider whether you really do), you will have to use another function such as gettimeofday().

Community
  • 1
  • 1
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135