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.
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-01-17 16:55:47 +08:00
|
|
|
|
#include <kiwano/core/Common.h>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
#include <kiwano/macros.h>
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
|
|
|
|
|
namespace kiwano
|
|
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* @brief 时间段
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @par
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* 时间段表示法:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @code
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* time::Second * 5 // 5 秒
|
|
|
|
|
|
* time::Hour * 1.5 // 1.5 小时
|
|
|
|
|
|
* time::Hour * 3 + time::Minute * 45 + time::Second * 15 // 3 小时 45 分 15
|
|
|
|
|
|
* 秒
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @endcode
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* 在 VS2015 及更高版本可以使用 time literals:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @code
|
|
|
|
|
|
* using namespace kiwano;
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* 5_sec // 5 秒
|
|
|
|
|
|
* 1.5_hour // 1.5 小时
|
|
|
|
|
|
* 3_hour + 45_min + 15_sec // 3 小时 45 分 15 秒
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @endcode
|
|
|
|
|
|
*/
|
|
|
|
|
|
struct KGE_API Duration
|
|
|
|
|
|
{
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 构造时间段
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Duration();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 构造时间段
|
|
|
|
|
|
/// @param milliseconds 毫秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Duration(long milliseconds);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取毫秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
long Milliseconds() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
float Seconds() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取分钟数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
float Minutes() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取小时数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
float Hours() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 时长是否是零
|
|
|
|
|
|
/// @return 若时长是零,返回true
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool IsZero() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置毫秒数
|
|
|
|
|
|
/// @param ms 毫秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetMilliseconds(long ms);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置秒数
|
|
|
|
|
|
/// @param seconds 秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetSeconds(float seconds);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置分钟数
|
|
|
|
|
|
/// @param minutes 分钟数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetMinutes(float minutes);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置小时数
|
|
|
|
|
|
/// @param hours 小时数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetHours(float hours);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 转为字符串
|
2020-01-21 10:09:55 +08:00
|
|
|
|
String ToString() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 解析时间段字符串
|
|
|
|
|
|
/// @param str 时间段字符串
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/// @details
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// 时间段字符串允许是有符号的浮点数, 并且带有时间单位后缀
|
|
|
|
|
|
/// 例如: "300ms", "-1.5h", "2h45m"
|
|
|
|
|
|
/// 允许的时间单位有 "ms", "s", "m", "h"
|
|
|
|
|
|
/// @return 解析出的时间段
|
|
|
|
|
|
/// @throw kiwano::Exception 传入一个不合法的格式时抛出
|
2020-01-21 10:09:55 +08:00
|
|
|
|
static Duration Parse(const String& str);
|
|
|
|
|
|
|
2020-02-10 17:32:04 +08:00
|
|
|
|
static const Duration Ms; ///< 毫秒
|
|
|
|
|
|
static const Duration Second; ///< 秒
|
|
|
|
|
|
static const Duration Minute; ///< 分钟
|
|
|
|
|
|
static const Duration Hour; ///< 小时
|
2020-01-21 10:09:55 +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_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* @brief 时间
|
|
|
|
|
|
* @par 示例:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @code
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* // 两时间相减, 可得到一个 Duration 对象
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* Time t1 = Time::Now();
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* // 等待一段时间后
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* Time t2 = Time::Now();
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* int ms = (t2 - t1).Milliseconds(); // 获取两时间相差的毫秒数
|
2020-01-21 10:09:55 +08:00
|
|
|
|
* @endcode
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* @note 时间点与系统时钟无关,因此不能将时间点转化为时分秒
|
2020-01-21 10:09:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
struct KGE_API Time
|
|
|
|
|
|
{
|
|
|
|
|
|
Time();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 是否是零时
|
|
|
|
|
|
/// @return 若是零时,返回true
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool IsZero() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取当前时间
|
2020-01-21 10:09:55 +08:00
|
|
|
|
static Time Now() noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
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-11-13 14:33:15 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace kiwano
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline namespace literals
|
|
|
|
|
|
{
|
|
|
|
|
|
inline const kiwano::Duration operator"" _msec(long double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Ms * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _msec(unsigned long long val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Ms * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _sec(long double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Second * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _sec(unsigned long long val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Second * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _min(long double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Minute * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _min(unsigned long long val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Minute * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _hour(long double val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Hour * val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const kiwano::Duration operator"" _hour(unsigned long long val)
|
|
|
|
|
|
{
|
|
|
|
|
|
return kiwano::Duration::Hour * val;
|
2019-11-13 14:33:15 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace literals
|
|
|
|
|
|
} // namespace kiwano
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
|
|
|
|
|
#endif
|