Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Unix timestamp in C++

Options
  • 16-01-2003 12:06pm
    #1
    Registered Users Posts: 383 ✭✭


    Is it possible to get the unix time in C++?

    I am trying to generate IDs and I usually just use the number of seconds since the epoch, but can't figure out how to do it in C++?

    Any one know?


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    #include <time.h>


    time_t time_var.

    or man time.


  • Registered Users Posts: 1,038 ✭✭✭rob1891


    from cplusplus.com:
    time
    <time.h>
    cplusplus.com
    time_t time ( time_t * timer );

    Get current time.
    Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC from the system clock.


    Parameters.

    timer
    Location where to store the retrieved value. If this is NULL the value is not stored. But it is still returned by the function.
    time_t is generally defined by default to long.

    Return Value.
    Elapsed time in seconds, as described.

    Portability.
    Defined in ANSI-C.

    Example.

    /* time example */
    #include <stdio.h>
    #include <time.h>

    int main ()
    {
    time_t seconds;

    seconds = time (NULL);
    printf ("%ld hours since January 1, 1970", seconds/3600);

    return 0;
    }

    Output:
    266344 hours since January 1, 1970

    See also.
    asctime, gmtime, localtime


  • Registered Users Posts: 383 ✭✭cherrio


    Excellent, that worked perfect. Thanks.


Advertisement