views:

138

answers:

2

I want to get the system time including fractional part of the seconds. Is it possible in standard c (ANSI C)? If not then tell me some libraries for window OS so that I make it possible. In Linux I have the following code with work fine for me.

 #include <sys/time.h>                                                                
 #include <time.h>                                                                    
 #include <stdlib.h>                                                                  
 #include <stdio.h>                                                                  

 int main(void)                                                                       
 {                                                                                    
   char buffer[30];                                                                   
   struct timeval tv;                                                                 

   time_t curtime;                                                                    



   gettimeofday(&tv, NULL);                                                           
   curtime=tv.tv_sec;                                                                 

   strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));                           
   printf("%s%ld\n",buffer,tv.tv_usec);                                               

   return 0;                                                                          


   }

Output is

12-25-2009 11:09:18.35443541

Kindly help me, how is it possible for window OS. IF ANSI C doesn't allow me.

+1  A: 

Like this:

#include <wtypes.h>
int main()
{
    SYSTEMTIME t;
    GetSystemTime(&t);
    int year = t.wYear;
    int month = t.wMonth;
    int day = t.wDay;
    int hour = t.wHour;
    int minute = t.wMinute;
    int second = t.wSecond;
    int second_fraction = t.wMilliseconds;
   return 0;
}

Couple of points about performance if it is important to you:

1) gettimeofday() is quite fast and if is used in a few threads it doesn't cause worsenig of performance

2) Inside localtime() there is a call to pthread_mutex_lock() (probably because it needs some system settings like daytime). So when you use it extensively in a multithreaded application there might performance problems

skwllsp
@skwllsp:- Kindly tell me the ref. or header file which i have to include for SYSTEMTIME and GetSystemTime....!
Arman
#include <wtypes.h>
skwllsp
Thanks Skwllsp, I have tried your code but its doesn't give me accurate result.
Arman
This code `int second_fraction = t.wMilliseconds/1000;` should not divide by a thousand. That wlll cause it to always be zero. Just use wMilliseconds directly.
wallyk
@wallyk 2. Right, I just use code from my program where it was assigned to `double`. I will fix it.
skwllsp
+1  A: 

Standard C does not provide sub-second resolution timing.

POSIX does provide sub-second resolution timing - in fact, a number of different ways of doing it, including gettimeofday() which you show.

Jonathan Leffler