Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Unix timestamp in C++

  • 16-01-2003 12:06PM
    #1
    Registered Users, Registered Users 2 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,563 ✭✭✭Typedef


    #include <time.h>


    time_t time_var.

    or man time.


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 383 ✭✭cherrio


    Excellent, that worked perfect. Thanks.


Advertisement