Display System Time

How do you display the system time? Is there a function, e.g. time.h?
I'm using RedHat Linux and writing code in C. I need to constantly display 9using a loop) the system time and maybe date in a standard format.

Thanks,
George

Comments

  • Hi,

    As far as I know there are functions [b]time()[/b] and [b]ctime()[/b] in C/C++.

    Here are some examples of how to use them.

    [code]
    //Program uses ctime to convert a time_t variable into a human readable
    //string in form Fri Jan 28 00:00:00 2000



    #include
    #include
    int main()
    {
    time_t hold_time;
    hold_time=time(NULL);
    cout<<"The date is: "<<ctime(&hold_time);

    return 0;
    }
    [/code]

    [code]

    //Program will use time_t to store number of seconds since 00:00:00 GMT Jan.
    //1, 1970

    #include <time.h>
    #include

    int main()
    {
    time_t hold_time;
    hold_time=time(NULL);

    cout<<"The number of elapsed seconds since Jan. 1, 1970 is "<<hold_time;

    return 0;
    }
    [/code]

    Hope this helps,

  • Thank you very much. Immediately after I posted my message I found time.h and a very good Unix site which provided useful examples also.
    Again, I appreciate your help.
  • : Thank you very much. Immediately after I posted my message I found time.h and a very good Unix site which provided useful examples also.
    : Again, I appreciate your help.
    :
    Heres another example. Maybe a little bit overkill, but ...

    use the system(); func to call time in the system.
    Now the problem with system is that it just returns a value if succeded or not. So you have to pipe the output to a textfile and read it.

    system("time > dateTextFile");

    I know this is a very complicated way to solve the problem, but maybe you will have some use of the system command some other time ;-)
    Is there someone out there who has a good way of catching the output of system() ???




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories