Thread: Current System Time (In Milliseconds)

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    12

    Current System Time (In Milliseconds)

    Please don't kill me if this is blindingly obvious, but I'm about to go crazy, after searching for absolutely ages on the Internet.

    I want to get the current system time and write it to a file as a string. The problem is that time() seems to be rounded to the closest second, and that's not specific enough for my needs, I really need milliseconds too.

    Can anyone help? I'm really stuck. This is all I have:
    Code:
    time_t rawtime; // Setup variable
    time(&rawtime); // Get time
    printf("%d", rawtime); // Test output

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Standard C only goes down to seconds. You'll need a compile specific function to do what you're asking.

    Try a few previous threads:
    http://cboard.cprogramming.com/search.php?searchid=670
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    Thanks for your reply, but I can't seem to find anything useful from there... Most of them are referring to timing events, which is not what I'm looking for. I'm quite happy to compile in whatever extra code is necessary. I read that it's possible to use <sys/time.h> to accomplish this task, but I don't have that library.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What compiler / OS are you running then?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    check your include directory and read inside of likely files (or use search features of os) to check out structure definitions that contain millisecond fields, and then find functions that deal with them

    whew that was a long post
    .sect signature

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    Windows XP (but preferrably, this source could be distributed and compiled anywhere)...
    I'm using LCC-Win32 to develop.

  7. #7
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    ftime

    found in timeb.h

    in your include/sys directory

    also lcc help documents
    .sect signature

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by IGAU
    I read that it's possible to use <sys/time.h> to accomplish this task, but I don't have that library.
    Do you have <sys/timeb.h>?

    Things in here allow access with millisecond granularity.

    (Yes, folks, I know that it's not standard, but lots of systems do have it, and I thought it's worth a shot.)

    Dave
    Last edited by Dave Evans; 03-30-2004 at 06:30 PM.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    I do have <sys/timeb.h>, so I'm looking at ftime(), but I don't really understand it's application. I'm not an advanced programmer in any respects... Sorry to sound like a newbie or whatever, but can someone expand on it's use in plain English a bit for me?
    Last edited by IGAU; 03-30-2004 at 06:29 PM.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by IGAU
    I do have <sys/timeb.h>, so I'm looking at ftime(), but I don't really understand it's application. I'm not an advanced programmer in any respects... Sorry to sound like a newbie or whatever, but can someone expand on it's use in plain English a bit for me?

    How about plain C:


    Code:
    #include <stdio.h>
    #include <sys/timeb.h>
    
    int main()
    {
      struct timeb tmb;
    
      ftime(&tmb);
      printf("tmb.time     = %ld (seconds)\n", tmb.time);
      printf("tmb.millitm  = %d (mlliseconds)\n", tmb.millitm);
    
      return 0;
    }
    Dave
    Last edited by Dave Evans; 03-30-2004 at 06:43 PM.

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    12
    Ooh thanks, that makes a lot more sense now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. getting system time with better than a second resolution
    By sigfriedmcwild in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2006, 08:05 AM
  3. GetSystemTime and my current time
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 09-20-2005, 06:57 PM
  4. Extracting System Date and Time
    By AQWst in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2004, 09:47 PM
  5. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM