Magic_Game/src/kiwano/core/Time.h

268 lines
7.2 KiB
C
Raw Normal View History

2019-11-13 14:33:15 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
#pragma once
#include <kiwano/macros.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-11-13 14:33:15 +08:00
namespace kiwano
{
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-12-25 18:07:57 +08:00
* @par
2019-12-20 17:49:16 +08:00
* ʱ<EFBFBD><EFBFBD><EFBFBD>α<EFBFBD>ʾ<EFBFBD><EFBFBD>:
2019-12-21 18:41:16 +08:00
* @code
* time::Second * 5 // 5 <20><>
* time::Hour * 1.5 // 1.5 Сʱ
* time::Hour * 3 + time::Minute * 45 + time::Second * 15 // 3 Сʱ 45 <20><> 15 <20><>
* @endcode
2019-12-20 17:49:16 +08:00
* <EFBFBD><EFBFBD> VS2015 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߰<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> time literals:
2019-12-21 18:41:16 +08:00
* @code
* using namespace kiwano;
* 5_sec // 5 <20><>
* 1.5_hour // 1.5 Сʱ
* 3_hour + 45_min + 15_sec // 3 Сʱ 45 <20><> 15 <20><>
* @endcode
2019-12-20 17:49:16 +08:00
*/
2019-11-13 14:33:15 +08:00
struct KGE_API Duration
{
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
Duration();
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
/// @param milliseconds <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
Duration(
long milliseconds
);
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
long Milliseconds() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
float Seconds() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
float Minutes() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><>ȡСʱ<D0A1><CAB1>
2019-11-13 14:33:15 +08:00
float Hours() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief ʱ<><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
/// @return <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3A3AC><EFBFBD><EFBFBD>true
2019-11-13 14:33:15 +08:00
bool IsZero() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD>
/// @param ms <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetMilliseconds(long ms);
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// @param seconds <20><><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetSeconds(float seconds);
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>
/// @param minutes <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetMinutes(float minutes);
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>Сʱ<D0A1><CAB1>
/// @param hours Сʱ<D0A1><CAB1>
2019-11-13 14:33:15 +08:00
void SetHours(float hours);
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief תΪ<D7AA>ַ<EFBFBD><D6B7><EFBFBD>
2019-11-13 14:33:15 +08:00
String ToString() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
2019-12-22 11:04:49 +08:00
/// @brief <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
/// @param str ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2019-12-20 17:49:16 +08:00
/// @details
/// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><D0B7>ŵĸ<C5B5><C4B8><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD>ʱ<EFBFBD>䵥λ<E4B5A5><CEBB>׺
/// <20><><EFBFBD><EFBFBD>: "300ms", "-1.5h", "2h45m"
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䵥λ<E4B5A5><CEBB> "ms", "s", "m", "h"
/// @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
/// @throw std::runtime_error <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7>ĸ<EFBFBD>ʽ
static Duration Parse(const String& str);
static const Duration Ms; ///< <20><><EFBFBD><EFBFBD>
static const Duration Second; ///< <20><>
static const Duration Minute; ///< <20><><EFBFBD><EFBFBD>
static const Duration Hour; ///< Сʱ
2019-11-13 14:33:15 +08:00
bool operator== (const Duration &) const;
bool operator!= (const Duration &) const;
bool operator> (const Duration &) const;
bool operator>= (const Duration &) const;
bool operator< (const Duration &) const;
bool operator<= (const Duration &) const;
float operator / (const Duration &) const;
const Duration operator + (const Duration &) const;
const Duration operator - (const Duration &) const;
const Duration operator - () const;
const Duration operator * (int) const;
const Duration operator * (unsigned long long) const;
const Duration operator * (float) const;
const Duration operator * (double) const;
const Duration operator * (long double) const;
const Duration operator / (int) const;
const Duration operator / (float) const;
const Duration operator / (double) const;
Duration& operator += (const Duration &);
Duration& operator -= (const Duration &);
Duration& operator *= (int);
Duration& operator *= (float);
Duration& operator *= (double);
Duration& operator /= (int);
Duration& operator /= (float);
Duration& operator /= (double);
friend const Duration operator* (int, const Duration &);
friend const Duration operator* (float, const Duration &);
friend const Duration operator* (double, const Duration &);
friend const Duration operator* (long double, const Duration &);
friend const Duration operator/ (int, const Duration &);
friend const Duration operator/ (float, const Duration &);
friend const Duration operator/ (double, const Duration &);
private:
long milliseconds_;
};
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief ʱ<EFBFBD><EFBFBD>
* @par ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @code
* // <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>ɵõ<C9B5>һ<EFBFBD><D2BB> Duration <20><><EFBFBD><EFBFBD>
* Time t1 = Time::Now();
* // <20>ȴ<EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
* Time t2 = Time::Now();
* int ms = (t2 - t1).Milliseconds(); // <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD>
* @endcode
* @note ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳʱ<EFBFBD><EFBFBD><EFBFBD>޹أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><EFBFBD>ܽ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>Ϊʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2019-11-13 14:33:15 +08:00
struct KGE_API Time
{
Time();
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ʱ
/// @return <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>true
2019-11-13 14:33:15 +08:00
bool IsZero() const;
2019-12-20 17:49:16 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
static Time Now() noexcept;
2019-11-13 14:33:15 +08:00
const Duration operator -(const Time&) const;
const Time operator +(const Duration &) const;
const Time operator -(const Duration &) const;
Time& operator +=(const Duration &);
Time& operator -=(const Duration &);
private:
Time(long ms);
private:
long dur_;
};
inline long Duration::Milliseconds() const { return milliseconds_; }
inline bool Duration::IsZero() const { return milliseconds_ == 0LL; }
inline void Duration::SetMilliseconds(long ms) { milliseconds_ = ms; }
inline void Duration::SetSeconds(float seconds) { milliseconds_ = static_cast<long>(seconds * 1000.f); }
inline void Duration::SetMinutes(float minutes) { milliseconds_ = static_cast<long>(minutes * 60 * 1000.f); }
inline void Duration::SetHours(float hours) { milliseconds_ = static_cast<long>(hours * 60 * 60 * 1000.f); }
inline bool Time::IsZero() const { return dur_ == 0; }
}
2019-12-21 18:41:16 +08:00
#if defined(KGE_VS_VER) && KGE_VS_VER > KGE_VS_2013
2019-11-13 14:33:15 +08:00
namespace kiwano
{
inline namespace literals
{
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _msec(long double val)
2019-11-13 14:33:15 +08:00
{
return kiwano::Duration::Ms * val;
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _msec(unsigned long long val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Ms * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _sec(long double val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Second * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _sec(unsigned long long val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Second * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _min(long double val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Minute * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _min(unsigned long long val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Minute * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _hour(long double val)
2019-11-13 14:33:15 +08:00
{
2019-12-21 18:41:16 +08:00
return kiwano::Duration::Hour * val;
2019-11-13 14:33:15 +08:00
}
2019-12-21 18:41:16 +08:00
inline const kiwano::Duration operator "" _hour(unsigned long long val)
2019-11-13 14:33:15 +08:00
{
return kiwano::Duration::Hour * val;
}
}
}
#endif