Outputting Current Time in C++

This is a discussion on Outputting Current Time in C++ within the C++ Programming forums, part of the General Programming Boards category; I was wondering how to output the current time in C++... Specifically, I'd like to print the time in this ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Exclamation Outputting Current Time in C++

    I was wondering how to output the current time in C++... Specifically, I'd like to print the time in this format:

    Code:
    02:56:32:231
    //where hh:mm:ss:MMM, hh = hours, mm = mins, ss = seconds, MMM = milliseconds
    I know that there are functions time, ctime, and others, but they are quite confusing. I was wondering if an elite C++ programmer knew the best/easiest way to present the current time of day in that format.

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    So far I have this as a beta:

    Code:
    time_t now;
    struct tm *current;
    now = time(0);
    current = localtime(&now);
    cout << "hour: " << current->tm_hour << endl;
    cout << "mins: " << current->tm_min << endl;
    cout << "sec: " << current->tm_sec << endl;
    which outputs (if the time is 5:53:43PM)

    Code:
    hour: 17
    mins: 53
    sec: 43
    So how would I get the milliseconds?

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    For future reference to anyone who might need this, I have figured it out:

    Code:
    time_t now;
    struct tm *current;
    now = time(0);
    current = localtime(&now);
    cout << "hour: " << current->tm_hour << endl;
    cout << "mins: " << current->tm_min << endl;
    cout << "sec: " << current->tm_sec << endl;
    
    struct timeval detail_time;
    gettimeofday(&detail_time,NULL);
    cout << "milli: " << detail_time.tv_usec/1000 << endl;
    This will output (if the time is 6:05:53PM)

    Code:
    hour: 18
    mins: 5
    sec: 53
    milli: 707
    //sec and millisec WILL change obviously

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    SeeForever: Learn and know that gettimeofday and the structs it works with well my friend; when you get to timewait mutex etc locks (try for x milliseconds to lock this resource; if no luck, give me a timeout signal) you will be glad that you did. What is surprisingly hard is implementing gettimeofday() (so it is POSIX compliant) on other platforms...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Zulu Time to Current Time
    By Caldus in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 09:54 PM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 09:39 AM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 02:21 PM
  4. Current System Time (In Milliseconds)
    By IGAU in forum C Programming
    Replies: 10
    Last Post: 03-30-2004, 06:53 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21