Get any time in milliseconds?

downs default_357-line at yahoo.de
Sun Apr 13 07:44:04 PDT 2008


Benjamin Schulte wrote:
> Hi.
> I'm currently trying to convert my application to linux.
> Now I had a counter in my application that counted in milliseconds the time since the application started. I have to port this to linux.
> 
> On windows I used the PerformanceCounter from the WinAPI (or the GetTickCount method, if the performance-counter is not supported).
> 
> Well, I would like to have it more OS-independent or at least working somehow in linux.
> I tried the std.date, but actually I would have to calculate the ms-value up from the splitted values (hours, days, etc). That would be kinda complicated, cause there are too many special things. Like some month only have 30 days, sometimes 28, etc.
> Then I found the std.c.time clock() method, but realized, that that maybe wraps very early, cause one second is 1000000 and the value is stored in an integer. that would mean that after ~36 minutes the timer would wrap to zero (or minus something?) again. That's not helpful.
> 
> So, now I'm looking for an alternate way to realize what I want. I just want ANY time counter in milliseconds. I can calculate it down to any base I want, but I just need a time~
> 
> Thanks in advance for any help

I quote from tools.time:

  extern(C) {
    struct timeval {
      uint tv_sec;
      int tv_usec;
    }
    int gettimeofday(timeval *, void *);
    d_time µsec() {
      timeval tv = void;
      gettimeofday(&tv, null);
      return tv.tv_sec*1_000_000 + tv.tv_usec;
    }
  }

Contrary to its name:

       and gives the number of seconds and microseconds since the  Epoch  (see
       time(2)).
	--man gettimeofday

DESCRIPTION
       time()  returns  the  time  since  the  Epoch (00:00:00 UTC, January 1,
       1970), measured in seconds.
	--man 2 time

Hope that answers your question.

 --downs



More information about the Digitalmars-d mailing list