BDateTime: Time_T functions return or take a time_t

* They used an unsigned int, which led to overflows when trying to set
them to a time before January 1st, 1970 (local time)
 * Some things use January 1st, 1970, GMT (or UTC) as a reference point.
In my timezone this leads to such a negative date. An example is cookie
expiration dates which are set to this date to expire them immediately.
Spotted by Opera testsuite.
 * This makes the method unuseable for dates after 2036 (signed 32-bit
time_t will overflow then. This gives us just 33 years to switch to a
64-bit time_t. In te meantime, please try using other methods to set the
date and time for BDateTime objects if you need to go this far.
This commit is contained in:
Adrien Destugues
2013-10-11 08:32:26 +02:00
parent a5ac24f00c
commit d05f9e2d3d
2 changed files with 11 additions and 5 deletions
+2 -2
View File
@@ -189,8 +189,8 @@ public:
const BTime& Time() const;
void SetTime(const BTime &time);
int32 Time_t() const;
void SetTime_t(uint32 seconds);
time_t Time_t() const;
void SetTime_t(time_t seconds);
bool operator!=(const BDateTime& dateTime) const;
bool operator==(const BDateTime& dateTime) const;
+9 -3
View File
@@ -1371,7 +1371,7 @@ BDateTime::SetTime(const BTime& time)
1.1.1970 - 00:00:00. If the current date is before 1.1.1970 the function
returns -1;
*/
int32
time_t
BDateTime::Time_t() const
{
BDate date(1970, 1, 1);
@@ -1392,7 +1392,7 @@ BDateTime::Time_t() const
tm_struct.tm_isdst = -1;
// return secs_since_jan1_1970 or -1 on error
return int32(mktime(&tm_struct));
return mktime(&tm_struct);
}
@@ -1401,8 +1401,14 @@ BDateTime::Time_t() const
1.1.1970 - 00:00:00.
*/
void
BDateTime::SetTime_t(uint32 seconds)
BDateTime::SetTime_t(time_t seconds)
{
time_t timePart = seconds % kSecondsPerDay;
if (timePart < 0) {
timePart += kSecondsPerDay;
seconds -= kSecondsPerDay;
}
BTime time;
time.AddSeconds(seconds % kSecondsPerDay);
fTime.SetTime(time);