Thread: time.h ctime and milliseconds

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    time.h ctime and milliseconds

    Hi Guys,

    I am trying to access the system time using th eheader file time.h and then using ctime(). This is great for the system date and time in seconds and works ok. But I want the time in milliseconds. Does anyone know how to do this or where I am going wrong.

    This is my code:

    Code:
    #include <time.h>
    
    #include <stdio.h>
    
     
    
     
    
    int main(void)
    
    {
    
       time_t lt;
    
     
    
       
    
       char c;
    
       while(c !='z')
    
       {
    
       lt=time(NULL);
    
     
    
        printf(ctime(&lt));
    
     
    
     
    
       printf("\nPress Any key to Continue");
    
       while((c=getchar()) != '\n')
    
       {
    
       printf("/n still not equal");
    
       }
    
        }
    
     
    
     
    
       return(0);
    
     
    
    }
    Thanks

    Andy.

  2. #2
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    I think you can use the gettimeofday() function or GetSystemTime() if you're on a windows system.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you have DJGPP you can type "time" and right-click on it to search the help for "time". I think you might also try utime() or something (DJGPP specific).

    Why do you want to know the millisecond?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    1
    On a POSIX system, see:

    clock_t clock()

    which returns the number of microseconds since the process was started. (Not milliseconds)

    On Win32, see:
    DWORD GetTickCount()

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try reading Forum rule number 5
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed