473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 199877
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,065 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,065 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

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

Similar topics

2
by: MarthaR | last post by:
I am trying to add the current time to a field when the user clicks on the toggle button next to the field. I am getting a time of 12:00:00 AM each time I click the button. How do I get a...
18
by: Tim Quon | last post by:
Hi I need to print the current time. I found an example where this is done as followed: long *mytime; time(mytime); printf("time: %s", ascitime(mytime); But this doesn't work and prints...
4
by: Gary Wessle | last post by:
Hi I am not getting current time with this program, what am I doing wrong? #include <ctime> #include <iostream> using namespace std; #define P(x) cout << #x " = " << (x) << "\n";
3
by: Mark Ingram | last post by:
Hi, I'd like to know the best way of checking the current time during a demonstration product. At the minute i store the first run date, then compare that to the system time, but obviously a user...
2
by: Mark Poppers | last post by:
I would like to get the current time(stamp) (including the milliseconds) and assign it into a string variable with a format like: xxxx mytime = getcurrentsystemtime(); string mytime =...
1
by: aramakri | last post by:
I just want to know when a particular function is called.Can anybody help me in getting this time in seconds.
4
by: limnath | last post by:
Dear All, I am using Perl version v5.8.2 built for IBM AIX. I want to get the current time till the resolution of milliseconds. Now I am able to get till seconds using localtime function. Can...
30
by: Ashit Vora | last post by:
Hi, I want to find the current time stamp in micro sec precision. The requirement is, my function is executed in an infinite loop. I measure a value and check if the value computed is what i...
1
by: Neel | last post by:
Hi, I want to find the current time stamp in micro sec precision. The requirement is, my function is executed in an infinite loop. I measure a value and check if the value computed is what i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.