423,095 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,095 IT Pros & Developers. It's quick & easy.

Getting current time with milliseconds

P: 19
hi all,

I want to take current time with milli seconds in C. Does it have any header file to include to produce time in milli seconds.

Please mail me if any of u know.

Thank you.
Oct 17 '06 #1
Share this Question
Share on Google+
8 Replies


100+
P: 144
The "time" function in <time.h> will return the number of seconds since the epoch (ie. 00:00:00 on January 1, 1970, Coordinated Universal Time). The type of "time_t" is generally typedefed as "long int" on POSIX compliant systems. Finally, if we recall that a millisecond is 1/1000 of a second we can easily calculate the number of milliseconds since the epoch.

Expand|Select|Wrap|Line Numbers
  1. <time.h>
  2.  
  3. ...
  4.  
  5. time_t msec = time(NULL) * 1000;
  6.  
Oct 17 '06 #2

arne
Expert 100+
P: 315
hi all,

I want to take current time with milli seconds in C. Does it have any header file to include to produce time in milli seconds.

Please mail me if any of u know.

Thank you.
You may try the "gettimeofday" function. It reports the time down to microsecond level. The function is included via <sys/time.h>.
Oct 17 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
If you are using Windows rather than standard C you can call

GetSystemTimeAsFileTime(...)

with returns the current time as the number of 100-nanosecond intervals that have passed since January 1, 1601.
Oct 17 '06 #4

P: 19
If you are using Windows rather than standard C you can call

GetSystemTimeAsFileTime(...)

with returns the current time as the number of 100-nanosecond intervals that have passed since January 1, 1601.
Hi Banfa,

Actually we want to count current time in milliseconds and then has to show such a way that hr/min/sec/msec.

Would u tell me the header files and functions in c required or else an example c program.

Thank you.
Oct 17 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
Would u tell me the header files and functions in c required or else an example c program.
Expand|Select|Wrap|Line Numbers
  1. #include "windows.h"
  2.  
  3. FILETIME now;
  4.  
  5. GetSystemTimeAsFileTime(&now);
  6.  
or if you want it split in to hours etc
Expand|Select|Wrap|Line Numbers
  1. #include "windows.h"
  2.  
  3. SYSTEMTIME now;
  4.  
  5. GetSystemTime(&now);
  6.  
Oct 17 '06 #6

P: 19
Expand|Select|Wrap|Line Numbers
  1. #include "windows.h"
  2.  
  3. FILETIME now;
  4.  
  5. GetSystemTimeAsFileTime(&now);
  6.  
or if you want it split in to hours etc
Expand|Select|Wrap|Line Numbers
  1. #include "windows.h"
  2.  
  3. SYSTEMTIME now;
  4.  
  5. GetSystemTime(&now);
  6.  


hi Banfa,

Thanks for ur best reply. But it doesnot works in linux. I need header files which is included in linux. Would u help me pls....
Oct 24 '06 #7

arne
Expert 100+
P: 315
hi Banfa,

Thanks for ur best reply. But it doesnot works in linux. I need header files which is included in linux. Would u help me pls....
Please refer to my previous post mentioning the gettimeofday() function. It will provide what you need.
Oct 24 '06 #8

P: 1
This will give you the time in the format HH:MM:SS:mmm

Expand|Select|Wrap|Line Numbers
  1. timeval tp;
  2. gettimeofday(&tp, 0);
  3. time_t curtime = tp.tv_sec;
  4. tm *t = localtime(&curtime);
  5. printf("%02d:%02d:%02d:%03d\n", t->tm_hour, t->tm_min, t->tm_sec, tp.tv_usec/1000);
Apr 12 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.