2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2022-05-31 22:28:43 +02:00
|
|
|
#include <limits>
|
2022-01-25 20:40:24 +01:00
|
|
|
#include <tuple>
|
2017-01-23 18:36:54 +01:00
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/internal/cef_time_wrappers.h"
|
|
|
|
#include "cef/include/internal/cef_types_wrappers.h"
|
|
|
|
#include "cef/libcef/common/time_util.h"
|
|
|
|
|
2022-05-31 22:28:43 +02:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2021-06-11 18:37:08 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// From MSDN, FILETIME "Contains a 64-bit value representing the number of
|
|
|
|
// 100-nanosecond intervals since January 1, 1601 (UTC)." This value must
|
|
|
|
// be less than 0x8000000000000000. Otherwise, the function
|
|
|
|
// FileTimeToSystemTime fails.
|
|
|
|
// From base/time/time_win.cc:
|
|
|
|
bool CanConvertToFileTime(int64_t us) {
|
|
|
|
return us >= 0 && us <= (std::numeric_limits<int64_t>::max() / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2022-01-24 18:58:02 +01:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2021-06-11 18:37:08 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void cef_time_to_basetime(const cef_time_t& cef_time, base::Time& time) {
|
|
|
|
base::Time::Exploded exploded;
|
|
|
|
exploded.year = cef_time.year;
|
|
|
|
exploded.month = cef_time.month;
|
|
|
|
exploded.day_of_week = cef_time.day_of_week;
|
|
|
|
exploded.day_of_month = cef_time.day_of_month;
|
|
|
|
exploded.hour = cef_time.hour;
|
|
|
|
exploded.minute = cef_time.minute;
|
|
|
|
exploded.second = cef_time.second;
|
|
|
|
exploded.millisecond = cef_time.millisecond;
|
2022-01-25 20:40:24 +01:00
|
|
|
std::ignore = base::Time::FromUTCExploded(exploded, &time);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cef_time_from_basetime(const base::Time& time, cef_time_t& cef_time) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-06-30 20:32:48 +02:00
|
|
|
int64_t t = time.ToDeltaSinceWindowsEpoch().InMicroseconds();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!CanConvertToFileTime(t)) {
|
2020-06-30 20:32:48 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-06-30 20:32:48 +02:00
|
|
|
#endif
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
base::Time::Exploded exploded;
|
|
|
|
time.UTCExplode(&exploded);
|
|
|
|
cef_time.year = exploded.year;
|
|
|
|
cef_time.month = exploded.month;
|
|
|
|
cef_time.day_of_week = exploded.day_of_week;
|
|
|
|
cef_time.day_of_month = exploded.day_of_month;
|
|
|
|
cef_time.hour = exploded.hour;
|
|
|
|
cef_time.minute = exploded.minute;
|
|
|
|
cef_time.second = exploded.second;
|
|
|
|
cef_time.millisecond = exploded.millisecond;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_to_timet(const cef_time_t* cef_time, time_t* time) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time || !time) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
base::Time base_time;
|
|
|
|
cef_time_to_basetime(*cef_time, base_time);
|
|
|
|
*time = base_time.ToTimeT();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_from_timet(time_t time, cef_time_t* cef_time) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
base::Time base_time = base::Time::FromTimeT(time);
|
|
|
|
cef_time_from_basetime(base_time, *cef_time);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_to_doublet(const cef_time_t* cef_time, double* time) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time || !time) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
base::Time base_time;
|
|
|
|
cef_time_to_basetime(*cef_time, base_time);
|
2023-11-21 20:17:55 +01:00
|
|
|
*time = base_time.InSecondsFSinceUnixEpoch();
|
2012-04-03 03:34:16 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_from_doublet(double time, cef_time_t* cef_time) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-11-21 20:17:55 +01:00
|
|
|
base::Time base_time = base::Time::FromSecondsSinceUnixEpoch(time);
|
2012-04-03 03:34:16 +02:00
|
|
|
cef_time_from_basetime(base_time, *cef_time);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_now(cef_time_t* cef_time) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time) {
|
2012-10-26 22:42:41 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
base::Time base_time = base::Time::Now();
|
|
|
|
cef_time_from_basetime(base_time, *cef_time);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-31 22:28:43 +02:00
|
|
|
CEF_EXPORT cef_basetime_t cef_basetime_now() {
|
|
|
|
return CefBaseTime(base::Time::Now());
|
|
|
|
}
|
|
|
|
|
2012-10-26 22:42:41 +02:00
|
|
|
CEF_EXPORT int cef_time_delta(const cef_time_t* cef_time1,
|
|
|
|
const cef_time_t* cef_time2,
|
|
|
|
long long* delta) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_time1 || !cef_time2 || !delta) {
|
2012-10-26 22:42:41 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-10-26 22:42:41 +02:00
|
|
|
|
|
|
|
base::Time base_time1, base_time2;
|
|
|
|
cef_time_to_basetime(*cef_time1, base_time1);
|
|
|
|
cef_time_to_basetime(*cef_time2, base_time2);
|
|
|
|
|
|
|
|
base::TimeDelta time_delta = base_time2 - base_time1;
|
|
|
|
*delta = time_delta.InMilliseconds();
|
|
|
|
return 1;
|
|
|
|
}
|
2022-05-31 22:28:43 +02:00
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_to_basetime(const cef_time_t* from,
|
|
|
|
cef_basetime_t* to) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!from || !to) {
|
2022-05-31 22:28:43 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-05-31 22:28:43 +02:00
|
|
|
|
|
|
|
base::Time::Exploded exploded;
|
|
|
|
exploded.year = from->year;
|
|
|
|
exploded.month = from->month;
|
|
|
|
exploded.day_of_week = from->day_of_week;
|
|
|
|
exploded.day_of_month = from->day_of_month;
|
|
|
|
exploded.hour = from->hour;
|
|
|
|
exploded.minute = from->minute;
|
|
|
|
exploded.second = from->second;
|
|
|
|
exploded.millisecond = from->millisecond;
|
|
|
|
base::Time time;
|
|
|
|
bool result = base::Time::FromUTCExploded(exploded, &time);
|
|
|
|
*to = CefBaseTime(time);
|
|
|
|
return result ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_EXPORT int cef_time_from_basetime(const cef_basetime_t from,
|
|
|
|
cef_time_t* to) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!to) {
|
2022-05-31 22:28:43 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-05-31 22:28:43 +02:00
|
|
|
|
|
|
|
base::Time time = CefBaseTime(from);
|
|
|
|
|
|
|
|
base::Time::Exploded exploded;
|
|
|
|
time.UTCExplode(&exploded);
|
|
|
|
to->year = exploded.year;
|
|
|
|
to->month = exploded.month;
|
|
|
|
to->day_of_week = exploded.day_of_week;
|
|
|
|
to->day_of_month = exploded.day_of_month;
|
|
|
|
to->hour = exploded.hour;
|
|
|
|
to->minute = exploded.minute;
|
|
|
|
to->second = exploded.second;
|
|
|
|
to->millisecond = exploded.millisecond;
|
|
|
|
return exploded.HasValidValues() ? 1 : 0;
|
|
|
|
}
|