From d05f9e2d3d13931ef7a10bdc065b385dc341746d Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Thu, 10 Oct 2013 17:20:19 +0200 Subject: [PATCH] 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. --- headers/os/support/DateTime.h | 4 ++-- src/kits/support/DateTime.cpp | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/headers/os/support/DateTime.h b/headers/os/support/DateTime.h index d9ba24dae1..17e1c65052 100644 --- a/headers/os/support/DateTime.h +++ b/headers/os/support/DateTime.h @@ -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; diff --git a/src/kits/support/DateTime.cpp b/src/kits/support/DateTime.cpp index 0d7c197255..5778ccf5b9 100644 --- a/src/kits/support/DateTime.cpp +++ b/src/kits/support/DateTime.cpp @@ -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);