diff --git a/headers/os/support/DateTime.h b/headers/os/support/DateTime.h index 8ee9122652..e6e3db255c 100644 --- a/headers/os/support/DateTime.h +++ b/headers/os/support/DateTime.h @@ -91,6 +91,8 @@ public: BDate(); BDate(const BDate& other); BDate(int32 year, int32 month, int32 day); + BDate(time_t time, + time_type type = B_LOCAL_TIME); BDate(const BMessage* archive); ~BDate(); diff --git a/src/kits/support/DateTime.cpp b/src/kits/support/DateTime.cpp index 1852d35ad6..b7f4ab0b76 100644 --- a/src/kits/support/DateTime.cpp +++ b/src/kits/support/DateTime.cpp @@ -486,6 +486,23 @@ BDate::BDate(int32 year, int32 month, int32 day) } +BDate::BDate(time_t time, time_type type) +{ + struct tm result; + struct tm* timeinfo; + + if (type == B_GMT_TIME) + timeinfo = gmtime_r(&time, &result); + else + timeinfo = localtime_r(&time, &result); + + if (timeinfo != NULL) { + _SetDate(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, + timeinfo->tm_mday); + } +} + + /*! Constructs a new BDate object from the provided archive. */ @@ -586,21 +603,7 @@ BDate::IsValid(int32 year, int32 month, int32 day) BDate BDate::CurrentDate(time_type type) { - time_t timer; - struct tm result; - struct tm* timeinfo; - - time(&timer); - - if (type == B_GMT_TIME) - timeinfo = gmtime_r(&timer, &result); - else - timeinfo = localtime_r(&timer, &result); - - if (timeinfo == NULL) - return BDate(); - - return BDate(timeinfo->tm_year + 1900, timeinfo->tm_mon +1, timeinfo->tm_mday); + return BDate(time(NULL), type); }