468,490 Members | 2,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

Getting current time with milliseconds

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
8 181792
tyreld
144 100+
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
315 Expert 100+
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
9,055 Expert Mod 8TB
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
Harika
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
9,055 Expert Mod 8TB
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
Harika
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
315 Expert 100+
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
Incyc
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.

Similar topics

18 posts views Thread by Tim Quon | last post: by
4 posts views Thread by Gary Wessle | last post: by
30 posts views Thread by Ashit Vora | last post: by
1 post views Thread by Neel | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.