Thread: How to get current time

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    19

    How to get current time

    Hello, i'm trying to print the current time but i'm getting it like this " Thu Apr 30 07:09:51 2009 "
    I only need the time, i don't need the date, year etc...

    How do i do that??

    This is what i used:
    Code:
        
    time_t t;
        struct tm *local;
        t = time(NULL);
        local = localtime(&t);
    ...........
           fprintf(file_out, "Arrival time: %s\n", asctime(local)   );
    ...........
    Right now i'm only trying to print the current time but what i really need is to be able to work with that time. Like i need to first use it as arrival time then use it to calculate how much time it takes to complete a certain job then finally i will have to print both on screen.

    That's why i only need the time and not the dates etc....
    Can anybody help me?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    this is what u can do determine the time interval between two events.
    Code:
    clock_t start,end;
    start=clock();
    //do something
    end=clock();
    (end-start)/CLOCKS_PER_SEC will give u the required time elapsed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    How about printing out just those elements of the struct tm object you're interested in instead of the whole thing. Just my 2c, but in this case it maybe better to take the difference of the begin and end times (epoch) instead of first converting them to a human-readable form and then taking their difference as the job may rollover to next day and those difference calculations will then be incorrect.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can also use difftime() to take the difference between to time_t values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  2. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM