chromium/base/time/time_exploded_posix.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/time/time.h"

#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 "build/build_config.h"
#include "build/chromecast_buildflags.h"

#if BUILDFLAG(IS_ANDROID) && !defined(__LP64__)
#include <time64.h>
#endif
#if BUILDFLAG(IS_NACL)
#include "base/os_compat_nacl.h"
#endif

namespace {

// This prevents a crash on traversing the environment global and looking up
// the 'TZ' variable in libc. See: crbug.com/390567.
base::Lock* GetSysTimeToTimeStructLock() {}

// Define a system-specific SysTime that wraps either to a time_t or
// a time64_t depending on the host system, and associated convertion.
// See crbug.com/162007
#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)

// The function timegm is not available on 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  // MacOS (and iOS 64-bit), Linux/ChromeOS, or any other POSIX-compliant.

SysTime;

SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) {}

void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {}

#endif  // BUILDFLAG(IS_ANDROID) && !defined(__LP64__)

}  // namespace

namespace base {

void Time::Explode(bool is_local, Exploded* exploded) const {}

// static
bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) {}

}  // namespace base