Solved

milli (or micro) second timing

Posted on 2001-06-22
17
624 Views
Last Modified: 2012-05-04
I'm looking to timestamp information in a program I'm working on. The timestamp will be compared to the current time to determine how much time has gone by. What I'm looking for is something like time(&tloc), only on the scale of milli or micro seconds. Like time gives me the number of seconds since 00:00:00 1970. Is there something that will give me the number of milliseconds since 00:00:00 1980? or since the system has been booted? Some kinda of fixed point for the system so that it can tell how much time has passed on a much finer scale than seconds.
0
Comment
Question by:phirephly
  • 9
  • 4
  • 3
  • +1
17 Comments
 

Author Comment

by:phirephly
ID: 6219885
oh yes, and there will probably be many timestamps floating around and some might get lost, so something like a process timer or the like won't work. It has to give me a number.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6219933
What OS/compiler are you using?

======
Werner
0
 

Author Comment

by:phirephly
ID: 6220046
Unixes, and gcc, but if there's something hat's even more universal, I'd like that. thanks!
0
 
LVL 30

Expert Comment

by:Axter
ID: 6220050
Use POSIX functions.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6220059
getitimer() is a POSIX function that should work with gcc on unix and linux.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6220087
I don't have any example code for getitimer function, but I do have it for setitimer function.
See the following code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
static bool Sleep_Timer_Handler_Set;
//***********************************************************************
//
// PROCEDURE:                    Sleep_Timer_Handler() function
//
// AUTHOR:                         David Maisonave (dmaisona@csc.com)(david@axter.com)    
//
// DESCRIPTION:             Function needed for Sleep() function.
//                                   Used in sigaction()
//
// INPUTS:                  N/A
//
// OUPUTS:                  N/A
//
// SPECIAL CONSIDERATIONS:  Needs static bool Sleep_Timer_Handler_Set variable
//
//***********************************************************************
//
static void Sleep_Timer_Handler(int signo)
{
     Sleep_Timer_Handler_Set=true;
}

//***********************************************************************
//
// PROCEDURE:                    POSIX version of Sleep() function                                  
//
// AUTHOR:                         David Maisonave (dmaisona@csc.com)(david@axter.com)    
//
// DESCRIPTION:             Emulates the Windows Sleep() function using
//                                   POSIX functions.  Sleep() function works like a pause() function
//
// INPUTS:                  Quantity of milliseconds
//
// OUPUTS:                  Always returns zero
//
// SPECIAL CONSIDERATIONS:  Needs above Sleep_Timer_Handler() function
//                                   and static bool Sleep_Timer_Handler_Set variable
//***********************************************************************
//
DWORD Sleep(DWORD dwMilliseconds)
{
     struct sigaction new_sigalrm;
     new_sigalrm.sa_handler = Sleep_Timer_Handler;
     sigemptyset(&new_sigalrm.sa_mask);
     new_sigalrm.sa_flags = 0;
     sigaction(SIGALRM,&new_sigalrm,NULL);
     Sleep_Timer_Handler_Set=false;
     struct itimerval StartTime;
     StartTime.it_interval.tv_sec = 0;
     StartTime.it_interval.tv_usec = 0;
     StartTime.it_value.tv_sec = dwMilliseconds/1000;
     StartTime.it_value.tv_usec = dwMilliseconds%1000;
     struct itimerval OldTime;
     setitimer(ITIMER_REAL,&StartTime,&OldTime);
     while(Sleep_Timer_Handler_Set==false);
     return 0;
}
0
 
LVL 11

Expert Comment

by:griessh
ID: 6220095
Good choice Axter, I tempted to suggest gettimeofday(). But I don't know how universal that is.

======
Werner
0
 
LVL 30

Expert Comment

by:Axter
ID: 6220186
I was looking up the gettimeofday() function to see if it was part of the POSIX standards, and when I realized that getitimer() is not part of the POSIX standard either.

The correct POSIX standard function is timer_gettime()
This function is in nanoseconds, so it's a better timer.
Here's the list for POSIX timer functions:

#include <signal.h>
#include <time.h>

  int timer_create (
          clockid_t clock_id,
          struct sigevent *evp,
          timer_t *timerid );

  int timer_gettime (
  timer_t timerid,
  struct itimerspec *value);

  int timer_settime (
  timer_t timerid,
  int flags,
  const struct itimerspec *value,
  struct itimerspec *ovalue);

  int timer_delete (
  timer_t timerid);

  int timer_getoverrun (
          timer_t timerid );
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Expert Comment

by:newmang
ID: 6220534
If your system supports it you could use gethrtime and sethrvtime to resolve to nanoseconds - its available on Solaris, don't know about others.

Brea in mind that any timer is only as good as the hardware platform it runs on. For example, in some versions of Windows the timing resolution is only good to 55 ms so even though the function promises times of the millisecond resolution in real life you can only really get times in 55ms chunks.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6220550
>>gethrtime
That's not a standard function.  Only Solaris supports it, and it's not part of the POSIX or C standards.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6225951
phirephly,
Did you forget about this question?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6225956
Hi phirephly:
If you have your answer, could you award it.

That way your question can be removed from the unanswered list.

Thanks.

0
 

Author Comment

by:phirephly
ID: 6228400
I'm sorry. I tried posting a reply a bit ago, but the website went down without taking it :(  Then over the last couple days, I haven't been able to get on. I hope this post makes it up there. that timer_gettime() and stuff isn't what I'm looking for unless I'm not seeing something. That'll time a function, but how would I get a time stamp out of that?  I want to be able to write out a timestamp to something that can be compared to the current one later. I'll have a lot of these stamps moving around and not all of 'em will return. I just wanna be able to read off the time that's been stamped, subtract it from the now time and go from there. timer_ relies on a reliative start and stop time, I want a solid time that can be output to the screen, written to a file, or whatever I wanna do with it.
0
 
LVL 11

Accepted Solution

by:
griessh earned 50 total points
ID: 6228588
Then why don't you like my gettimeofday() function? Have you tried to read about it? It gives you the miliseconds and seconds since 1. January 1970 an dyou can subtract, store them as unique timestamps ...
That's what a timestamp is!

======
Werner
0
 
LVL 30

Expert Comment

by:Axter
ID: 6229677
Hi phirephly,
I think griessh posted the answer you are looking for.
Could you please post some feedback.

Thanks
0
 

Author Comment

by:phirephly
ID: 6230142
alright... if it does secs and msecs, that's what I'm looking for. I would have liked to test it out first, but guess that's my problem I didn't get on it quickly enough. thanks. bye
0
 
LVL 30

Expert Comment

by:Axter
ID: 6230181
phirephly,
There's no hurry.  
All you have have to do is let the expert know that you're going to tested, and that you'll get back to him/her after you tested out.
Most experts understand this, and it's a common practice.
If you still have problems with the posted method, just ask the expert on this same post question.
Most experts will follow through with any suggest method.

The more feedback you give the expert the better are your chances that you get exactly what you're looking for, or the next best thing.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now