Magic_Game/kiwano/base/time.h

247 lines
6.6 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +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 "../macros.h"
2019-04-09 02:25:17 +08:00
#include "../common/String.h"
#include <ostream>
#include <istream>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace time
{
// ʱ<><CAB1><EFBFBD><EFBFBD>
//
// ʱ<><CAB1><EFBFBD>α<EFBFBD>ʾ<EFBFBD><CABE>:
2019-04-09 02:25:17 +08:00
// 5 <20><>: time::Sec * 5
// 1.5 Сʱ: time::Hour * 1.5
2019-04-09 02:25:17 +08:00
// 3 Сʱ 45 <20><> 15 <20><>: time::Hour * 3 + time::Min * 45 + time::Sec * 15
// <20><> VS2015 <20><><EFBFBD><EFBFBD><EFBFBD>߰汾<DFB0><E6B1BE><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> time literals:
// 5 <20><>: 5_s
// 1.5 Сʱ: 1.5_h
// 3 Сʱ 45 <20><> 15 <20><>: 3_h + 45_m + 15_s
//
2019-04-11 14:40:54 +08:00
struct KGE_API Duration
{
Duration();
Duration(
long milliseconds
);
// ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
2019-04-14 22:37:05 +08:00
inline long Milliseconds() const { return milliseconds_; }
// ת<><D7AA>Ϊ<EFBFBD><CEAA>
float Seconds() const;
// ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
float Minutes() const;
// ת<><D7AA>ΪСʱ
float Hours() const;
// ʱ<><CAB1><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
2019-04-14 22:37:05 +08:00
inline bool IsZero() const { return milliseconds_ == 0LL; }
inline void SetMilliseconds(long ms) { milliseconds_ = ms; }
inline void SetSeconds(float seconds) { milliseconds_ = static_cast<long>(seconds * 1000.f); }
inline void SetMinutes(float minutes) { milliseconds_ = static_cast<long>(minutes * 60 * 1000.f); }
inline void SetHours(float hours) { milliseconds_ = static_cast<long>(hours * 60 * 60 * 1000.f); }
// תΪ<D7AA>ַ<EFBFBD><D6B7><EFBFBD>
2019-04-09 02:25:17 +08:00
String ToString() const;
2019-04-14 22:37:05 +08:00
inline operator bool() const { return !IsZero(); }
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 &);
2019-04-09 02:25:17 +08:00
public:
// ʱ<><CAB1><EFBFBD>θ<EFBFBD>ʽ<EFBFBD><CABD>
//
// ʱ<><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"
static Duration Parse(const String& parse_str);
template <typename _Char>
friend inline std::basic_ostream<_Char>& operator<<(std::basic_ostream<_Char>& out, const Duration& dur)
{
return out << dur.ToString();
}
template <typename _Char>
friend inline std::basic_istream<_Char>& operator>>(std::basic_istream<_Char>& in, Duration& dur)
{
String str;
if (in >> str)
{
dur = Duration::Parse(str);
}
return in;
}
private:
long milliseconds_;
};
/* Ԥ<><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> */
2019-04-11 14:40:54 +08:00
KGE_API extern const Duration Ms; // <20><><EFBFBD><EFBFBD>
KGE_API extern const Duration Sec; // <20><>
KGE_API extern const Duration Min; // <20><><EFBFBD><EFBFBD>
KGE_API extern const Duration Hour; // Сʱ
// ʱ<><CAB1>
//
2019-04-09 02:25:17 +08:00
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>: Time now = Time::Now();
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>õ<EFBFBD>һ<EFBFBD><D2BB> Duration <20><><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD>:
2019-04-09 02:25:17 +08:00
// Time t1, t2;
// int ms = (t2 - t1).Milliseconds(); // <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD>
//
2019-04-11 14:40:54 +08:00
struct KGE_API Time
{
2019-04-09 02:25:17 +08:00
Time();
2019-04-09 02:25:17 +08:00
Time(long);
// <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ʱ
inline bool IsZero() const { return dur_ == 0; }
2019-04-09 02:25:17 +08:00
const Time operator + (const Duration &) const;
const Time operator - (const Duration &) const;
2019-04-09 02:25:17 +08:00
Time& operator += (const Duration &);
Time& operator -= (const Duration &);
2019-04-09 02:25:17 +08:00
const Duration operator - (const Time &) const;
public:
// <20><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>
// <20><><EFBFBD>ڸ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>ʱ<EFBFBD>ʼ<E4BFAA><CABC><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,
// Ҳ<>޷<EFBFBD><DEB7><EFBFBD><EFBFBD>ø<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> Unix ʱ<><CAB1><EFBFBD><EFBFBD>
static Time Now() noexcept;
private:
long dur_;
};
}
}
2019-04-11 14:40:54 +08:00
namespace kiwano
{
using namespace time;
}
#if VS_VER >= VS_2015
2019-04-11 14:40:54 +08:00
namespace kiwano
{
inline namespace literals
{
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _ms(long double val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Ms * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _s(long double val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Sec * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _m(long double val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Min * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _h(long double val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Hour * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _ms(unsigned long long val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Ms * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _s(unsigned long long val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Sec * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _m(unsigned long long val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Min * val;
}
2019-04-11 14:40:54 +08:00
inline const kiwano::time::Duration operator "" _h(unsigned long long val)
{
2019-04-11 14:40:54 +08:00
return kiwano::time::Hour * val;
}
}
namespace time
{
2019-04-11 14:40:54 +08:00
using namespace kiwano::literals;
}
}
#endif