c++ - math with ctime and time_t -


does know how todo math ctime? need able time in sec in "time_t" (like does) , subtract set number of seconds before inputting time_t ctime time , date.

so calculating date of many sec ago.

time_t basic representation of date , time type time_t. value of time_t variable number of seconds since january 1, 1970, call unix epoch. best way internally represent start , end times event because easy compare these values. struct tm while time_t represents date , time single number, struct tm represents struct lot of numbers:

struct tm {   int tm_sec;            /* seconds.    [0-60] (1 leap second) */   int tm_min;            /* minutes.    [0-59] */   int tm_hour;            /* hours.    [0-23] */   int tm_mday;            /* day.        [1-31] */   int tm_mon;            /* month.    [0-11] */   int tm_year;            /* year    - 1900.  */   int tm_wday;            /* day of week.    [0-6] */   int tm_yday;            /* days in year.[0-365]    */   int tm_isdst;            /* dst.        [-1/0/1]*/ }; 

conversion can convert time_t value struct tm value using localtime function:

struct tm starttm;     time_t start;      /* ... */      starttm = *localtime(&start); 

so,you can subtract subtract set number of seconds this

starttm.tm_sec -= somesecond; 

add convert time_t this

struct tm starttm;     time_t start;      /* ... */      start = mktime(&starttm); 

and use ctime fun convert date

ctime(&start) 

hope can helpful!


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -