Magic_Game/src/kiwano/core/Time.cpp

534 lines
12 KiB
C++
Raw Normal View History

2019-11-13 14:33:15 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-11-13 14:33:15 +08:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
2019-11-13 14:33:15 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-11-13 14:33:15 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2020-02-10 14:41:19 +08:00
#include <kiwano/core/Exception.h>
2020-05-24 11:26:21 +08:00
#include <kiwano/utils/Logger.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/core/Time.h>
2019-11-13 14:33:15 +08:00
#include <regex>
#include <unordered_map>
2020-05-19 21:13:42 +08:00
#include <chrono>
#include <thread>
2019-11-13 14:33:15 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
//-------------------------------------------------------
// Time
//-------------------------------------------------------
Time::Time()
: dur_(0)
{
}
2020-05-26 00:57:07 +08:00
Time::Time(int64_t dur)
2020-01-21 10:09:55 +08:00
: dur_(dur)
{
}
const Time Time::operator+(const Duration& dur) const
{
return Time{ dur_ + dur.Milliseconds() };
}
const Time Time::operator-(const Duration& dur) const
{
return Time{ dur_ - dur.Milliseconds() };
}
Time& Time::operator+=(const Duration& other)
{
dur_ += other.Milliseconds();
return (*this);
}
2019-11-13 14:33:15 +08:00
2020-01-21 10:09:55 +08:00
Time& Time::operator-=(const Duration& other)
{
dur_ -= other.Milliseconds();
return (*this);
}
const Duration Time::operator-(const Time& other) const
{
return Duration(dur_ - other.dur_);
}
Time Time::Now() noexcept
{
2020-05-19 21:13:42 +08:00
#if defined(KGE_PLATFORM_WINDOWS)
static double millisecs_per_count = {};
if (millisecs_per_count == 0)
2020-01-21 10:09:55 +08:00
{
2020-05-19 21:13:42 +08:00
LARGE_INTEGER freq = {};
2020-01-21 10:09:55 +08:00
// the Function will always succceed on systems that run Windows XP or later
QueryPerformanceFrequency(&freq);
2020-05-19 21:13:42 +08:00
millisecs_per_count = 1000.0 / static_cast<double>(freq.QuadPart);
2020-01-21 10:09:55 +08:00
}
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
2020-05-26 00:57:07 +08:00
return Time{ static_cast<int64_t>(count.QuadPart * millisecs_per_count) };
2020-05-19 21:13:42 +08:00
#else
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
2020-05-26 00:57:07 +08:00
const auto now = steady_clock::now();
const auto count = duration_cast<milliseconds>(now.time_since_epoch()).count();
return Time{ static_cast<int64_t>(count) };
2020-01-21 10:09:55 +08:00
2020-05-19 21:13:42 +08:00
#endif
2020-01-21 10:09:55 +08:00
}
2020-05-24 12:00:47 +08:00
//-------------------------------------------------------
// ClockTime
//-------------------------------------------------------
ClockTime::ClockTime()
: ms_since_epoch_(0)
{
}
2020-05-26 00:57:07 +08:00
int64_t ClockTime::GetTimeStamp() const
2020-05-24 12:00:47 +08:00
{
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
const auto timestamp = duration_cast<seconds>(milliseconds(ms_since_epoch_)).count();
2020-05-26 00:57:07 +08:00
return static_cast<int64_t>(timestamp);
2020-05-24 12:00:47 +08:00
}
2020-05-26 00:57:07 +08:00
int64_t ClockTime::GetMillisecondsSinceEpoch() const
2020-05-24 12:00:47 +08:00
{
return ms_since_epoch_;
}
std::time_t ClockTime::GetCTime() const
{
return static_cast<time_t>(GetTimeStamp());
}
2020-05-26 00:57:07 +08:00
ClockTime::ClockTime(int64_t ms_since_epoch)
2020-05-24 12:00:47 +08:00
: ms_since_epoch_(ms_since_epoch)
{
}
2020-05-26 00:57:07 +08:00
ClockTime ClockTime::FromTimeStamp(int64_t timestamp) noexcept
2020-05-24 12:00:47 +08:00
{
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
const auto ms = duration_cast<milliseconds>(seconds(timestamp)).count();
2020-05-26 00:57:07 +08:00
return ClockTime(static_cast<int64_t>(ms));
2020-05-24 12:00:47 +08:00
}
ClockTime ClockTime::Now() noexcept
{
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::system_clock;
2020-05-26 00:57:07 +08:00
const auto now = system_clock::now();
const auto count = duration_cast<milliseconds>(now.time_since_epoch()).count();
return ClockTime{ static_cast<int64_t>(count) };
2020-05-24 12:00:47 +08:00
}
const Duration ClockTime::operator-(const ClockTime& other) const
{
return Duration(ms_since_epoch_ - other.ms_since_epoch_);
}
const ClockTime ClockTime::operator+(const Duration& dur) const
{
return ClockTime{ ms_since_epoch_ + dur.Milliseconds() };
}
const ClockTime ClockTime::operator-(const Duration& dur) const
{
return ClockTime{ ms_since_epoch_ - dur.Milliseconds() };
}
ClockTime& ClockTime::operator+=(const Duration& other)
{
ms_since_epoch_ += other.Milliseconds();
return (*this);
}
ClockTime& ClockTime::operator-=(const Duration& other)
{
ms_since_epoch_ -= other.Milliseconds();
return (*this);
}
2020-01-21 10:09:55 +08:00
//-------------------------------------------------------
// Duration
//-------------------------------------------------------
const Duration Duration::Ms = 1L;
const Duration Duration::Second = 1000 * Duration::Ms;
const Duration Duration::Minute = 60 * Duration::Second;
const Duration Duration::Hour = 60 * Duration::Minute;
namespace
{
2020-02-10 13:47:00 +08:00
const auto duration_regex = std::regex(R"(^[-+]?([0-9]*(\.[0-9]*)?(h|m|s|ms)+)+$)");
2020-01-21 10:09:55 +08:00
typedef std::unordered_map<String, Duration> UnitMap;
2020-02-10 13:47:00 +08:00
const auto unit_map =
UnitMap{ { "ms", Duration::Ms }, { "s", Duration::Second }, { "m", Duration::Minute }, { "h", Duration::Hour } };
2020-01-21 10:09:55 +08:00
} // namespace
Duration::Duration()
: milliseconds_(0)
{
}
2020-05-26 00:57:07 +08:00
Duration::Duration(int64_t milliseconds)
2020-01-21 10:09:55 +08:00
: milliseconds_(milliseconds)
{
}
float Duration::Seconds() const
{
2020-05-26 00:57:07 +08:00
auto sec = milliseconds_ / Second.milliseconds_;
auto ms = milliseconds_ % Second.milliseconds_;
return static_cast<float>(sec + ms) / 1000.f;
2020-01-21 10:09:55 +08:00
}
float Duration::Minutes() const
{
2020-05-26 00:57:07 +08:00
auto min = milliseconds_ / Minute.milliseconds_;
auto ms = milliseconds_ % Minute.milliseconds_;
return static_cast<float>(min + ms) / (60 * 1000.f);
2020-01-21 10:09:55 +08:00
}
float Duration::Hours() const
{
2020-05-26 00:57:07 +08:00
auto hour = milliseconds_ / Hour.milliseconds_;
auto ms = milliseconds_ % Hour.milliseconds_;
return static_cast<float>(hour + ms) / (60 * 60 * 1000.f);
}
void Duration::Sleep() const
{
using std::chrono::milliseconds;
using std::this_thread::sleep_for;
if (milliseconds_)
{
sleep_for(milliseconds(milliseconds_));
}
2020-01-21 10:09:55 +08:00
}
String Duration::ToString() const
{
if (IsZero())
{
2020-02-10 13:47:00 +08:00
return String("0s");
2020-01-21 10:09:55 +08:00
}
2020-07-20 16:55:56 +08:00
StringStream stream;
2020-05-26 00:57:07 +08:00
int64_t total_ms = milliseconds_;
2020-01-21 10:09:55 +08:00
if (total_ms < 0)
{
2020-07-20 16:55:56 +08:00
stream << "-";
2020-01-21 10:09:55 +08:00
total_ms = -total_ms;
}
2020-05-26 00:57:07 +08:00
int64_t hour = total_ms / Hour.milliseconds_;
int64_t min = total_ms / Minute.milliseconds_ - hour * 60;
int64_t sec = total_ms / Second.milliseconds_ - (hour * 60 * 60 + min * 60);
int64_t ms = total_ms % Second.milliseconds_;
2020-01-21 10:09:55 +08:00
if (hour)
{
2020-07-20 16:55:56 +08:00
stream << hour << 'h' << min << 'm';
2020-01-21 10:09:55 +08:00
}
else if (min)
{
2020-07-20 16:55:56 +08:00
stream << min << 'm';
2020-01-21 10:09:55 +08:00
}
if (ms != 0)
{
2020-07-20 16:55:56 +08:00
stream << float(sec) + float(ms) / 1000.f << 's';
2020-01-21 10:09:55 +08:00
}
else if (sec != 0)
{
2020-07-20 16:55:56 +08:00
stream << sec << 's';
2020-01-21 10:09:55 +08:00
}
2020-07-20 16:55:56 +08:00
return stream.str();
2020-01-21 10:09:55 +08:00
}
bool Duration::operator==(const Duration& other) const
{
return milliseconds_ == other.milliseconds_;
}
bool Duration::operator!=(const Duration& other) const
{
return milliseconds_ != other.milliseconds_;
}
bool Duration::operator>(const Duration& other) const
{
return milliseconds_ > other.milliseconds_;
}
bool Duration::operator>=(const Duration& other) const
{
return milliseconds_ >= other.milliseconds_;
}
bool Duration::operator<(const Duration& other) const
{
return milliseconds_ < other.milliseconds_;
}
bool Duration::operator<=(const Duration& other) const
{
return milliseconds_ <= other.milliseconds_;
}
float Duration::operator/(const Duration& other) const
{
return static_cast<float>(milliseconds_) / other.milliseconds_;
}
const Duration Duration::operator+(const Duration& other) const
{
return Duration(milliseconds_ + other.milliseconds_);
}
const Duration Duration::operator-(const Duration& other) const
{
return Duration(milliseconds_ - other.milliseconds_);
2019-11-13 14:33:15 +08:00
}
2020-01-21 10:09:55 +08:00
const Duration Duration::operator-() const
{
return Duration(-milliseconds_);
}
const Duration Duration::operator*(int val) const
{
return Duration(milliseconds_ * val);
}
const Duration Duration::operator*(unsigned long long val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ * val));
2020-01-21 10:09:55 +08:00
}
const Duration Duration::operator*(float val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ * val));
2020-01-21 10:09:55 +08:00
}
const Duration Duration::operator*(double val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ * val));
2020-01-21 10:09:55 +08:00
}
const Duration Duration::operator*(long double val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ * val));
2020-01-21 10:09:55 +08:00
}
const Duration Duration::operator/(int val) const
{
return Duration(milliseconds_ / val);
}
const Duration Duration::operator/(float val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ / val));
2020-01-21 10:09:55 +08:00
}
const Duration Duration::operator/(double val) const
{
2020-05-26 00:57:07 +08:00
return Duration(static_cast<int64_t>(milliseconds_ / val));
2020-01-21 10:09:55 +08:00
}
Duration& Duration::operator+=(const Duration& other)
{
milliseconds_ += other.milliseconds_;
return (*this);
}
Duration& Duration::operator-=(const Duration& other)
{
milliseconds_ -= other.milliseconds_;
return (*this);
}
Duration& Duration::operator*=(int val)
{
milliseconds_ *= val;
return (*this);
}
Duration& Duration::operator/=(int val)
{
2020-05-26 00:57:07 +08:00
milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
2020-01-21 10:09:55 +08:00
return (*this);
}
Duration& Duration::operator*=(float val)
{
2020-05-26 00:57:07 +08:00
milliseconds_ = static_cast<int64_t>(milliseconds_ * val);
2020-01-21 10:09:55 +08:00
return (*this);
}
Duration& Duration::operator/=(float val)
{
2020-05-26 00:57:07 +08:00
milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
2020-01-21 10:09:55 +08:00
return (*this);
}
Duration& Duration::operator*=(double val)
{
2020-05-26 00:57:07 +08:00
milliseconds_ = static_cast<int64_t>(milliseconds_ * val);
2020-01-21 10:09:55 +08:00
return (*this);
}
Duration& Duration::operator/=(double val)
{
2020-05-26 00:57:07 +08:00
milliseconds_ = static_cast<int64_t>(milliseconds_ / val);
2020-01-21 10:09:55 +08:00
return (*this);
}
const Duration operator*(int val, const Duration& dur)
{
return dur * val;
}
const Duration operator/(int val, const Duration& dur)
{
return dur / val;
}
const Duration operator*(float val, const Duration& dur)
{
return dur * val;
}
const Duration operator/(float val, const Duration& dur)
{
return dur / val;
}
const Duration operator*(double val, const Duration& dur)
{
return dur * val;
}
const Duration operator/(double val, const Duration& dur)
{
return dur / val;
}
const Duration operator*(long double val, const Duration& dur)
{
return dur * val;
}
Duration Duration::Parse(const String& format)
{
bool negative = false;
size_t len = format.length();
size_t pos = 0;
Duration ret;
if (!std::regex_match(format.c_str(), duration_regex))
{
2020-02-13 22:35:04 +08:00
KGE_THROW("Duration::Parse failed, invalid duration");
2020-01-21 10:09:55 +08:00
}
2020-02-10 13:47:00 +08:00
if (format.empty() || format == "0")
2020-01-21 10:09:55 +08:00
{
return ret;
}
// <20><><EFBFBD><EFBFBD>λ
2020-02-10 13:47:00 +08:00
if (format[0] == '-' || format[0] == '+')
2020-01-21 10:09:55 +08:00
{
2020-02-10 13:47:00 +08:00
negative = (format[0] == '-');
2020-01-21 10:09:55 +08:00
pos++;
}
while (pos < len)
{
// <20><>ֵ
2020-01-21 10:09:55 +08:00
size_t i = pos;
for (; i < len; ++i)
{
wchar_t ch = format[i];
2020-02-10 13:47:00 +08:00
if (!(ch == '.' || '0' <= ch && ch <= '9'))
2020-01-21 10:09:55 +08:00
{
break;
}
}
String num_str = format.substr(pos, i - pos);
2020-02-10 13:47:00 +08:00
pos = i;
if (num_str.empty() || num_str == ".")
2020-02-13 22:35:04 +08:00
KGE_THROW("Duration::Parse failed, invalid duration");
2020-01-21 10:09:55 +08:00
// <20><>λ
2020-01-21 10:09:55 +08:00
for (; i < len; ++i)
{
wchar_t ch = format[i];
2020-02-10 13:47:00 +08:00
if (ch == '.' || '0' <= ch && ch <= '9')
2020-01-21 10:09:55 +08:00
{
break;
}
}
String unit_str = format.substr(pos, i - pos);
2020-02-10 13:47:00 +08:00
pos = i;
2020-01-21 10:09:55 +08:00
if (unit_map.find(unit_str) == unit_map.end())
2020-02-13 22:35:04 +08:00
KGE_THROW("Duration::Parse failed, invalid duration");
2020-01-21 10:09:55 +08:00
2020-02-10 13:47:00 +08:00
double num = std::stod(num_str.c_str());
2020-01-21 10:09:55 +08:00
Duration unit = unit_map.at(unit_str);
ret += unit * num;
}
if (negative)
{
ret = -ret;
}
return ret;
}
} // namespace kiwano