#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <limits>
#include "base/no_destructor.h"
#include "base/numerics/safe_math.h"
#include "base/synchronization/lock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#if BUILDFLAG(IS_ANDROID) && !defined(__LP64__)
#include <time64.h>
#endif
namespace {
base::Lock* GetSysTimeToTimeStructLock() {
static base::NoDestructor<base::Lock> lock;
return lock.get();
}
#if BUILDFLAG(IS_ANDROID) && !defined(__LP64__)
typedef time64_t SysTime;
SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
if (is_local) {
return mktime64(timestruct);
} else {
return timegm64(timestruct);
}
}
void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
if (is_local) {
localtime64_r(&t, timestruct);
} else {
gmtime64_r(&t, timestruct);
}
}
#elif BUILDFLAG(IS_AIX)
time_t aix_timegm(struct tm* tm) {
time_t ret;
char* tz;
tz = getenv("TZ");
if (tz) {
tz = strdup(tz);
}
setenv("TZ", "GMT0", 1);
tzset();
ret = mktime(tm);
if (tz) {
setenv("TZ", tz, 1);
free(tz);
} else {
unsetenv("TZ");
}
tzset();
return ret;
}
typedef time_t SysTime;
SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
if (is_local) {
return mktime(timestruct);
} else {
return aix_timegm(timestruct);
}
}
void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
if (is_local) {
localtime_r(&t, timestruct);
} else {
gmtime_r(&t, timestruct);
}
}
#else
typedef time_t SysTime;
SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
return is_local ? mktime(timestruct) : timegm(timestruct);
}
void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {
base::AutoLock locked(*GetSysTimeToTimeStructLock());
if (is_local) {
localtime_r(&t, timestruct);
} else {
gmtime_r(&t, timestruct);
}
}
#endif
}
namespace base {
void Time::Explode(bool is_local, Exploded* exploded) const {
const int64_t millis_since_unix_epoch =
ToRoundedDownMillisecondsSinceUnixEpoch();
if (sizeof(SysTime) < 8) {
#if !BUILDFLAG(IS_CASTOS) && !BUILDFLAG(IS_CAST_ANDROID)
ExplodeUsingIcu(millis_since_unix_epoch, is_local, exploded);
return;
#endif
}
auto seconds = base::checked_cast<SysTime>(millis_since_unix_epoch /
Time::kMillisecondsPerSecond);
int64_t millisecond = millis_since_unix_epoch % Time::kMillisecondsPerSecond;
if (millisecond < 0) {
--seconds;
millisecond += Time::kMillisecondsPerSecond;
}
struct tm timestruct;
SysTimeToTimeStruct(seconds, ×truct, is_local);
exploded->year = timestruct.tm_year + 1900;
exploded->month = timestruct.tm_mon + 1;
exploded->day_of_week = timestruct.tm_wday;
exploded->day_of_month = timestruct.tm_mday;
exploded->hour = timestruct.tm_hour;
exploded->minute = timestruct.tm_min;
exploded->second = timestruct.tm_sec;
exploded->millisecond = static_cast<int>(millisecond);
}
bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {
CheckedNumeric<int> month = exploded.month;
month--;
CheckedNumeric<int> year = exploded.year;
year -= 1900;
if (!month.IsValid() || !year.IsValid()) {
*time = Time(0);
return false;
}
struct tm timestruct;
timestruct.tm_sec = exploded.second;
timestruct.tm_min = exploded.minute;
timestruct.tm_hour = exploded.hour;
timestruct.tm_mday = exploded.day_of_month;
timestruct.tm_mon = month.ValueOrDie();
timestruct.tm_year = year.ValueOrDie();
timestruct.tm_wday = exploded.day_of_week;
timestruct.tm_yday = 0;
timestruct.tm_isdst = -1;
#if !BUILDFLAG(IS_SOLARIS) && !BUILDFLAG(IS_AIX)
timestruct.tm_gmtoff = 0;
timestruct.tm_zone = nullptr;
#endif
int64_t seconds;
struct tm timestruct0 = timestruct;
seconds = SysTimeFromTimeStruct(×truct, is_local);
if (seconds == -1) {
timestruct = timestruct0;
timestruct.tm_isdst = 0;
int64_t seconds_isdst0 = SysTimeFromTimeStruct(×truct, is_local);
timestruct = timestruct0;
timestruct.tm_isdst = 1;
int64_t seconds_isdst1 = SysTimeFromTimeStruct(×truct, is_local);
if (seconds_isdst0 < 0) {
seconds = seconds_isdst1;
} else if (seconds_isdst1 < 0) {
seconds = seconds_isdst0;
} else {
seconds = std::min(seconds_isdst0, seconds_isdst1);
}
}
int64_t milliseconds = 0;
if (seconds == -1 && (exploded.year < 1969 || exploded.year > 1970)) {
const int64_t min_seconds = (sizeof(SysTime) < sizeof(int64_t))
? std::numeric_limits<SysTime>::min()
: std::numeric_limits<int32_t>::min();
const int64_t max_seconds = (sizeof(SysTime) < sizeof(int64_t))
? std::numeric_limits<SysTime>::max()
: std::numeric_limits<int32_t>::max();
if (exploded.year < 1969) {
milliseconds = min_seconds * kMillisecondsPerSecond;
} else {
milliseconds = max_seconds * kMillisecondsPerSecond;
milliseconds += (kMillisecondsPerSecond - 1);
}
} else {
CheckedNumeric<int64_t> checked_millis = seconds;
checked_millis *= kMillisecondsPerSecond;
checked_millis += exploded.millisecond;
if (!checked_millis.IsValid()) {
*time = Time(0);
return false;
}
milliseconds = checked_millis.ValueOrDie();
}
Time converted_time;
if (!FromMillisecondsSinceUnixEpoch(milliseconds, &converted_time)) {
*time = base::Time(0);
return false;
}
Time::Exploded to_exploded;
if (!is_local) {
converted_time.UTCExplode(&to_exploded);
} else {
converted_time.LocalExplode(&to_exploded);
}
if (ExplodedMostlyEquals(to_exploded, exploded)) {
*time = converted_time;
return true;
}
*time = Time(0);
return false;
}
}