| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Easy2D - 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.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "time.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | #include <regex>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace easy2d | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	namespace time | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 		// TimePoint
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint::TimePoint() | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 		TimePoint::TimePoint(const Duration& dur) | 
					
						
							|  |  |  |  | 			: dur(dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint::TimePoint(const TimePoint & other) | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			: dur(other.dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint::TimePoint(TimePoint && other) | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			: dur(std::move(other.dur)) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const TimePoint TimePoint::operator+(const Duration & dur) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			return TimePoint(dur + dur); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const TimePoint TimePoint::operator-(const Duration & dur) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			return TimePoint(dur - dur); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint & TimePoint::operator+=(const Duration & other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			dur += other; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint & TimePoint::operator-=(const Duration &other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			dur -= other; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration TimePoint::operator-(const TimePoint & other) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			return dur - other.dur; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint& TimePoint::operator=(const TimePoint & other) E2D_NOEXCEPT | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (this == &other) | 
					
						
							|  |  |  |  | 				return *this; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			dur = other.dur; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return *this; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		TimePoint& TimePoint::operator=(TimePoint && other) E2D_NOEXCEPT | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (this == &other) | 
					
						
							|  |  |  |  | 				return *this; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			dur = std::move(other.dur); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return *this; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 		// Duration
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Millisecond	= Duration(1); | 
					
						
							|  |  |  |  | 		const Duration Second		= 1000 * Millisecond; | 
					
						
							|  |  |  |  | 		const Duration Minute		= 60 * Second; | 
					
						
							|  |  |  |  | 		const Duration Hour			= 60 * Minute; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		namespace | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			const auto duration_regex = std::wregex(LR"([-+]?([0-9]*(\.[0-9]*)?[a-z]+)+)"); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			typedef std::map<std::wstring, Duration> UnitMap; | 
					
						
							|  |  |  |  | 			const auto unit_map = UnitMap | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				{L"ms", Millisecond}, | 
					
						
							|  |  |  |  | 				{L"s", Second}, | 
					
						
							|  |  |  |  | 				{L"m", Minute}, | 
					
						
							|  |  |  |  | 				{L"h", Hour} | 
					
						
							|  |  |  |  | 			}; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration::Duration() | 
					
						
							|  |  |  |  | 			: milliseconds_(0) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration::Duration(int64_t milliseconds) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			: milliseconds_(milliseconds) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Seconds() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			int64_t sec = milliseconds_ / Second.milliseconds_; | 
					
						
							|  |  |  |  | 			int64_t ms = milliseconds_ % Second.milliseconds_; | 
					
						
							|  |  |  |  | 			return static_cast<float>(sec) + static_cast<float>(ms) / 1000.f; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Minutes() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			int64_t min = milliseconds_ / Minute.milliseconds_; | 
					
						
							|  |  |  |  | 			int64_t ms = milliseconds_ % Minute.milliseconds_; | 
					
						
							|  |  |  |  | 			return static_cast<float>(min) + static_cast<float>(ms) / (60 * 1000.f); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Hours() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			int64_t hour = milliseconds_ / Hour.milliseconds_; | 
					
						
							|  |  |  |  | 			int64_t ms = milliseconds_ % Hour.milliseconds_; | 
					
						
							|  |  |  |  | 			return static_cast<float>(hour) + static_cast<float>(ms) / (60 * 60 * 1000.f); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		std::wstring easy2d::time::Duration::ToString() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 			if (IsZero()) | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				return std::wstring(L"0s"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			std::wstring result; | 
					
						
							|  |  |  |  | 			int64_t hour = milliseconds_ / Hour.milliseconds_; | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  |  | 			int64_t min = milliseconds_ / Minute.milliseconds_ - hour * 60; | 
					
						
							|  |  |  |  | 			int64_t sec = milliseconds_ / Second.milliseconds_ - (hour * 60 * 60 + min * 60); | 
					
						
							|  |  |  |  | 			int64_t ms = milliseconds_ % Second.milliseconds_; | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (milliseconds_ < 0) | 
					
						
							|  |  |  |  | 				result.append(L"-"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:43:43 +08:00
										 |  |  |  | 			if (hour) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				result.append(std::to_wstring(hour)).append(L"h"); | 
					
						
							|  |  |  |  | 				result.append(std::to_wstring(min)).append(L"m"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			else if(min) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				result.append(std::to_wstring(min)).append(L"m"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  |  | 			if (ms != 0) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:43:43 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  |  | 				auto float_to_str = [](float val) -> std::wstring | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					wchar_t buf[10] = {}; | 
					
						
							|  |  |  |  | 					::swprintf_s(buf, L"%.2f", val); | 
					
						
							|  |  |  |  | 					return std::wstring(buf); | 
					
						
							|  |  |  |  | 				}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				result.append(float_to_str(static_cast<float>(sec) + static_cast<float>(ms) / 1000.f)) | 
					
						
							|  |  |  |  | 					.append(L"s"); | 
					
						
							| 
									
										
										
										
											2018-11-11 16:43:43 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  |  | 			else if (sec != 0) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:43:43 +08:00
										 |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				result.append(std::to_wstring(sec)).append(L"s"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return result; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +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_; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		float easy2d::time::Duration::operator/(const Duration & other) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return static_cast<float>(milliseconds_) / other.milliseconds_; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator+(const Duration & other) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ + other.milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator-(const Duration & other) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ - other.milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator-() const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(-milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator*(int val) const | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ * val); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator/(int val) const | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ / val); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::Duration::operator*(unsigned long long val) const | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::Duration::operator/(unsigned long long val) const | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator*(float val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator/(float val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator*(double val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator*(long double val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ * val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator/(double val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration Duration::operator/(long double val) const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return Duration(static_cast<int64_t>(milliseconds_ / val)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator+=(const Duration &other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ += other.milliseconds_; | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator-=(const Duration &other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ -= other.milliseconds_; | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & Duration::operator*=(int val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ *= val; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & Duration::operator/=(int val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & easy2d::time::Duration::operator*=(unsigned long long val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & easy2d::time::Duration::operator/=(unsigned long long val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & Duration::operator*=(float val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & Duration::operator/=(float val) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration & Duration::operator*=(double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator*=(long double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ * val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator/=(double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator/=(long double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<int64_t>(milliseconds_ / val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator*(int val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator*(unsigned long long val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator/(int val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator/(unsigned long long val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator*(float val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator/(float val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator*(double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return dur * val; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator/(double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return dur / val; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator*(long double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return dur * val; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:56:47 +08:00
										 |  |  |  | 		const Duration easy2d::time::operator/(long double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return dur / val; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		std::wostream & easy2d::time::operator<<(std::wostream & out, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			return out << dur.ToString(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		std::wistream & easy2d::time::operator>>(std::wistream & in, Duration & dur) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 			std::wstring str; | 
					
						
							|  |  |  |  | 			in >> str; | 
					
						
							|  |  |  |  | 			dur = time::ParseDuration(str); | 
					
						
							|  |  |  |  | 			return in; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 		// Functions
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		TimePoint easy2d::time::Now() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			static LARGE_INTEGER freq = {}; | 
					
						
							|  |  |  |  | 			if (freq.QuadPart == 0LL) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				if (QueryPerformanceFrequency(&freq) == 0) | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					const char* err = "QueryPerformanceFrequency not supported"; | 
					
						
							|  |  |  |  | 					logs::Errorln(HRESULT_FROM_WIN32(GetLastError()), err); | 
					
						
							|  |  |  |  | 					throw std::runtime_error(err); | 
					
						
							|  |  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			LARGE_INTEGER count; | 
					
						
							|  |  |  |  | 			QueryPerformanceCounter(&count); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			const long long whole = (count.QuadPart / freq.QuadPart) * 1000LL; | 
					
						
							|  |  |  |  | 			const long long part = (count.QuadPart % freq.QuadPart) * 1000LL / freq.QuadPart; | 
					
						
							|  |  |  |  | 			return TimePoint{ Duration{ whole + part } }; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-11 16:32:12 +08:00
										 |  |  |  | 		Duration easy2d::time::ParseDuration(const std::wstring & str) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			size_t len = str.length(); | 
					
						
							|  |  |  |  | 			size_t pos = 0; | 
					
						
							|  |  |  |  | 			bool negative = false; | 
					
						
							|  |  |  |  | 			Duration d; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (!std::regex_match(str, duration_regex)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 				logs::Errorln("time::ParseDuration failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 				return Duration(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (str.empty() || str == L"0") { return d; } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD>λ
 | 
					
						
							|  |  |  |  | 			if (str[0] == L'-' || str[0] == L'+') | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				negative = (str[0] == L'-'); | 
					
						
							|  |  |  |  | 				pos++; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			while (pos < len) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				// <20><>ֵ
 | 
					
						
							|  |  |  |  | 				size_t i = pos; | 
					
						
							|  |  |  |  | 				for (; i < len; ++i) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					wchar_t ch = str[i]; | 
					
						
							|  |  |  |  | 					if (!(ch == L'.' || L'0' <= ch && ch <= L'9')) | 
					
						
							|  |  |  |  | 					{ | 
					
						
							|  |  |  |  | 						break; | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				std::wstring num_str = str.substr(pos, i - pos); | 
					
						
							|  |  |  |  | 				pos = i; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (num_str.empty() || num_str == L".") | 
					
						
							|  |  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 					logs::Errorln("time::ParseDuration failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 					return Duration(); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				// <20><>λ
 | 
					
						
							|  |  |  |  | 				for (; i < len; ++i) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					wchar_t ch = str[i]; | 
					
						
							|  |  |  |  | 					if (ch == L'.' || L'0' <= ch && ch <= L'9') | 
					
						
							|  |  |  |  | 					{ | 
					
						
							|  |  |  |  | 						break; | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				std::wstring unit_str = str.substr(pos, i - pos); | 
					
						
							|  |  |  |  | 				pos = i; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (unit_map.find(unit_str) == unit_map.end()) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 					logs::Errorln("time::ParseDuration failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 					return Duration(); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				double num = std::stod(num_str); | 
					
						
							|  |  |  |  | 				Duration unit = unit_map.at(unit_str); | 
					
						
							|  |  |  |  | 				d += unit * num; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (negative) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				d = -d; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return d; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |