time-t

How to calculate time differences in C++ with time_t before the epoch?

无人久伴 提交于 2019-12-06 04:01:14
问题 What I would like to do with my simple program is to calculate a difference in seconds between two dates. time_t referenceDate; time_t dateNow = time(0); struct tm referenceDateComponent = {0}; referenceDateComponent.tm_hour = 0; referenceDateComponent.tm_min = 0; referenceDateComponent.tm_sec = 0; referenceDateComponent.tm_year = 89; referenceDateComponent.tm_mon = 11; referenceDateComponent.tm_mday = 31; referenceDate = mktime(&referenceDateComponent); long seconds = difftime(dateNow,

Convert double to time_t

筅森魡賤 提交于 2019-12-06 00:00:31
问题 I have a double containing seconds. I would like to convert this into a time_t . I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand? 回答1: The type of std::time_t is unspecified. Although not defined, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time. So, just a safe casting between them could be fine. Also be carefull about portability

Convert date and time numbers to time_t AND specify the timezone

ε祈祈猫儿з 提交于 2019-12-04 20:29:08
问题 I have the following integers: int y, mon, d, h, min, s; Their values are: 2012 , 06 , 27 , 12 , 47 , 53 respectively. I want to represent the date time of "2012/06/27 12:47:53 UTC" if I have selected 'UTC' somewhere else in my application, or "2012/06/27 12:47:53 AEST" if I have selected 'AEST' somewhere else in my application. I want to convert this into a time_t , and here's the code that I am current using to do so: struct tm timeinfo; timeinfo.tm_year = year - 1900; timeinfo.tm_mon = mon

How to stop time from running backwards on Linux?

孤者浪人 提交于 2019-12-04 15:36:04
问题 Here's a little test I've written to verify that time does indeed only run forwards in Linux. #include <time.h> #include <sys/time.h> bool timeGoesForwardTest2() { timeval tv1, tv2; double startTime = getTimeSeconds(); // my function while ( getTimeSeconds() - startTime < 5 ) { gettimeofday( &tv1, NULL ); gettimeofday( &tv2, NULL ); if ( tv2.tv_usec == tv1.tv_usec && tv2.tv_sec == tv1.tv_sec ) { continue; // Equal times are allowed. } // tv2 should be greater than tv1 if ( !( tv2.tv_usec>tv1

What is the difference between clock_t, time_t and struct tm?

这一生的挚爱 提交于 2019-12-04 09:05:38
问题 What is the difference between clock_t, time_t and struct tm? struct tm looks like this: struct tm{ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; But how does clock_t and time_t look like? 回答1: time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time. clock_t is a

Convert double to time_t

我的未来我决定 提交于 2019-12-04 04:05:27
I have a double containing seconds. I would like to convert this into a time_t . I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand? The type of std::time_t is unspecified. Although not defined, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time. So, just a safe casting between them could be fine. Also be carefull about portability (because it's type is not specified in the standard) and consider about the values than can not fit while casting

Convert date and time numbers to time_t AND specify the timezone

天大地大妈咪最大 提交于 2019-12-03 13:08:18
I have the following integers: int y, mon, d, h, min, s; Their values are: 2012 , 06 , 27 , 12 , 47 , 53 respectively. I want to represent the date time of "2012/06/27 12:47:53 UTC" if I have selected 'UTC' somewhere else in my application, or "2012/06/27 12:47:53 AEST" if I have selected 'AEST' somewhere else in my application. I want to convert this into a time_t , and here's the code that I am current using to do so: struct tm timeinfo; timeinfo.tm_year = year - 1900; timeinfo.tm_mon = mon - 1; timeinfo.tm_mday = day; timeinfo.tm_hour = hour; timeinfo.tm_min = min; timeinfo.tm_sec = sec; /

What is the difference between clock_t, time_t and struct tm?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:56:00
What is the difference between clock_t, time_t and struct tm? struct tm looks like this: struct tm{ int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; But how does clock_t and time_t look like? time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time. clock_t is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly

easy way to add 1 month to a time_t in C/C++

左心房为你撑大大i 提交于 2019-12-01 05:21:49
I have some code that uses the Oracle function add_months to increment a Date by X number of months. I now need to re-implement the same logic in a C / C++ function. For reasons I don't want/need to go into I can't simply issue a query to oracle to get the new date. Does anyone know of a simple and reliable way of adding X number of months to a time_t? Some examples of the types of calculations are shown below. 30/01/2009 + 1 month = 28/02/2009 31/01/2009 + 1 month = 28/02/2009 27/02/2009 + 1 month = 27/03/2009 28/02/2009 + 1 month = 31/03/2009 31/01/2009 + 50 months = 31/03/2013 Method

easy way to add 1 month to a time_t in C/C++

ⅰ亾dé卋堺 提交于 2019-12-01 02:54:25
问题 I have some code that uses the Oracle function add_months to increment a Date by X number of months. I now need to re-implement the same logic in a C / C++ function. For reasons I don't want/need to go into I can't simply issue a query to oracle to get the new date. Does anyone know of a simple and reliable way of adding X number of months to a time_t? Some examples of the types of calculations are shown below. 30/01/2009 + 1 month = 28/02/2009 31/01/2009 + 1 month = 28/02/2009 27/02/2009 + 1