find how many seconds past since 1/1/1970

后端 未结 3 1620
栀梦
栀梦 2021-02-19 06:14

I am looking for a function in C++ that calculates how many seconds have past from 1/1/1970 until today.

相关标签:
3条回答
  • 2021-02-19 06:47

    See man mktime:

    #include <time.h>
    
    time_t secsSinceEpoch = mktime(localtime(NULL));
    
    0 讨论(0)
  • 2021-02-19 06:48
    #include <time.h>
    
    time_t seconds_past_epoch = time(0);
    

    Available on most operating systems.

    0 讨论(0)
  • 2021-02-19 06:50

    time_t time(void) time_t time(time_t *ptr)

    include: time.h

    Returns the number of seconds that have passed since midnight, 1st January 1970 GMT (or pm, 31st December 1969 EST). If the parameter is not NULL, the same value is stored in the location pointed to. Follow this link for information on the time_t type. The value returned may be used as a reliable measure of elapsed time, and may be passed to ctime() or conversion into a human-readable string.

    Example:

    time_t t1=time(NULL);
    do_something_long();
    time_t t2=time(NULL);
    printf("%d seconds elapsed\n", t2-t1);
    

    time_t values are produced from the clock by time. time_t values are produced from y,m,d,h,m,s parts by mktime and timegm. time_t values are analysed into y,m,d,h,m,s by localtime and gmtime. time_t values are converted to readable strings by ctime.

    0 讨论(0)
提交回复
热议问题