473,385 Members | 1,309 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,385 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 199807
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.