add ClockTime
This commit is contained in:
		
							parent
							
								
									5d7c3f4f9a
								
							
						
					
					
						commit
						dc6dd682ff
					
				|  | @ -98,6 +98,88 @@ Time Time::Now() noexcept | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| //-------------------------------------------------------
 | ||||
| // ClockTime
 | ||||
| //-------------------------------------------------------
 | ||||
| 
 | ||||
| ClockTime::ClockTime() | ||||
|     : ms_since_epoch_(0) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| long ClockTime::GetTimeStamp() const | ||||
| { | ||||
|     using std::chrono::duration_cast; | ||||
|     using std::chrono::milliseconds; | ||||
|     using std::chrono::seconds; | ||||
| 
 | ||||
|     const auto timestamp = duration_cast<seconds>(milliseconds(ms_since_epoch_)).count(); | ||||
|     return static_cast<long>(timestamp); | ||||
| } | ||||
| 
 | ||||
| long ClockTime::GetMillisecondsSinceEpoch() const | ||||
| { | ||||
|     return ms_since_epoch_; | ||||
| } | ||||
| 
 | ||||
| std::time_t ClockTime::GetCTime() const | ||||
| { | ||||
|     return static_cast<time_t>(GetTimeStamp()); | ||||
| } | ||||
| 
 | ||||
| ClockTime::ClockTime(long ms_since_epoch) | ||||
|     : ms_since_epoch_(ms_since_epoch) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| ClockTime ClockTime::FromTimeStamp(long timestamp) noexcept | ||||
| { | ||||
|     using std::chrono::duration_cast; | ||||
|     using std::chrono::milliseconds; | ||||
|     using std::chrono::seconds; | ||||
| 
 | ||||
|     const auto ms = duration_cast<milliseconds>(seconds(timestamp)).count(); | ||||
|     return ClockTime(static_cast<long>(ms)); | ||||
| } | ||||
| 
 | ||||
| ClockTime ClockTime::Now() noexcept | ||||
| { | ||||
|     using std::chrono::duration_cast; | ||||
|     using std::chrono::milliseconds; | ||||
|     using std::chrono::system_clock; | ||||
| 
 | ||||
|     const auto      now   = system_clock::now(); | ||||
|     const long long count = duration_cast<milliseconds>(now.time_since_epoch()).count(); | ||||
|     return ClockTime{ static_cast<long>(count) }; | ||||
| } | ||||
| 
 | ||||
| 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); | ||||
| } | ||||
| 
 | ||||
| //-------------------------------------------------------
 | ||||
| // Duration
 | ||||
| //-------------------------------------------------------
 | ||||
|  |  | |||
|  | @ -20,6 +20,7 @@ | |||
| 
 | ||||
| #pragma once | ||||
| #include <kiwano/core/Common.h> | ||||
| #include <ctime> | ||||
| 
 | ||||
| namespace kiwano | ||||
| { | ||||
|  | @ -198,6 +199,49 @@ private: | |||
|     long dur_; | ||||
| }; | ||||
| 
 | ||||
| /**
 | ||||
|  * \~chinese | ||||
|  * @brief 时钟时间 | ||||
|  */ | ||||
| struct KGE_API ClockTime | ||||
| { | ||||
|     ClockTime(); | ||||
| 
 | ||||
|     /// \~chinese
 | ||||
|     /// @brief 获取当前时间戳
 | ||||
|     long GetTimeStamp() const; | ||||
| 
 | ||||
|     /// \~chinese
 | ||||
|     /// @brief 获取自纪元以来的毫秒数
 | ||||
|     long GetMillisecondsSinceEpoch() const; | ||||
| 
 | ||||
|     /// \~chinese
 | ||||
|     /// @brief 获取 C 风格的时间
 | ||||
|     std::time_t GetCTime() const; | ||||
| 
 | ||||
|     /// \~chinese
 | ||||
|     /// @brief 获取当前时间
 | ||||
|     static ClockTime Now() noexcept; | ||||
| 
 | ||||
|     /// \~chinese
 | ||||
|     /// @brief 时间戳转化为时间
 | ||||
|     static ClockTime FromTimeStamp(long timestamp) noexcept; | ||||
| 
 | ||||
|     const Duration operator-(const ClockTime&) const; | ||||
| 
 | ||||
|     const ClockTime operator+(const Duration&) const; | ||||
|     const ClockTime operator-(const Duration&) const; | ||||
| 
 | ||||
|     ClockTime& operator+=(const Duration&); | ||||
|     ClockTime& operator-=(const Duration&); | ||||
| 
 | ||||
| private: | ||||
|     ClockTime(long ms_since_epoch); | ||||
| 
 | ||||
| private: | ||||
|     long ms_since_epoch_; | ||||
| }; | ||||
| 
 | ||||
| inline long Duration::Milliseconds() const | ||||
| { | ||||
|     return milliseconds_; | ||||
|  | @ -232,6 +276,7 @@ inline bool Time::IsZero() const | |||
| { | ||||
|     return dur_ == 0; | ||||
| } | ||||
| 
 | ||||
| }  // namespace kiwano
 | ||||
| 
 | ||||
| #if defined(KGE_HAS_LITERALS) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue