| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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:
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // 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"
 | 
					
						
							|  |  |  |  | #include "logs.h"
 | 
					
						
							|  |  |  |  | #include <regex>
 | 
					
						
							|  |  |  |  | #include <unordered_map>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	namespace time | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		// Time
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		Time::Time() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		Time::Time(long dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			: dur_(dur) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		const Time Time::operator+(const Duration & dur) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			return Time{ dur_ + dur.Milliseconds() }; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		const Time Time::operator-(const Duration & dur) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			return Time{ dur_ - dur.Milliseconds() }; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		Time & Time::operator+=(const Duration & other) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			dur_ += other.Milliseconds(); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		Time & Time::operator-=(const Duration &other) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			dur_ -= other.Milliseconds(); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		const Duration Time::operator-(const Time & other) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(dur_ - other.dur_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 15:27:18 +08:00
										 |  |  |  | 		Time Time::Now() noexcept | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			static LARGE_INTEGER freq = {}; | 
					
						
							|  |  |  |  | 			if (freq.QuadPart == 0LL) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				// the function will always succceed on systems that run Windows XP or later
 | 
					
						
							|  |  |  |  | 				QueryPerformanceFrequency(&freq); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			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 Time{ static_cast<long>(whole + part) }; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 		// Duration
 | 
					
						
							|  |  |  |  | 		//-------------------------------------------------------
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		const Duration Ms	= 1L; | 
					
						
							|  |  |  |  | 		const Duration Sec	= 1000 * Ms; | 
					
						
							|  |  |  |  | 		const Duration Min	= 60 * Sec; | 
					
						
							|  |  |  |  | 		const Duration Hour	= 60 * Min; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		namespace | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			const auto duration_regex = std::wregex(LR"([-+]?([0-9]*(\.[0-9]*)?[a-z]+)+)"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			typedef std::unordered_map<String, Duration> UnitMap; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			const auto unit_map = UnitMap | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				{L"ms", Ms}, | 
					
						
							|  |  |  |  | 				{L"s", Sec}, | 
					
						
							|  |  |  |  | 				{L"m", Min}, | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 				{L"h", Hour} | 
					
						
							|  |  |  |  | 			}; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration::Duration() | 
					
						
							|  |  |  |  | 			: milliseconds_(0) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration::Duration(long milliseconds) | 
					
						
							|  |  |  |  | 			: milliseconds_(milliseconds) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Seconds() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			long sec = milliseconds_ / Sec.milliseconds_; | 
					
						
							|  |  |  |  | 			long ms = milliseconds_ % Sec.milliseconds_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			return static_cast<float>(sec) + static_cast<float>(ms) / 1000.f; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Minutes() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			long min = milliseconds_ / Min.milliseconds_; | 
					
						
							|  |  |  |  | 			long ms = milliseconds_ % Min.milliseconds_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			return static_cast<float>(min) + static_cast<float>(ms) / (60 * 1000.f); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		float Duration::Hours() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			long hour = milliseconds_ / Hour.milliseconds_; | 
					
						
							|  |  |  |  | 			long ms = milliseconds_ % Hour.milliseconds_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			return static_cast<float>(hour) + static_cast<float>(ms) / (60 * 60 * 1000.f); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		String kiwano::time::Duration::ToString() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (IsZero()) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				return String(L"0s"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			String result; | 
					
						
							|  |  |  |  | 			long total_ms = milliseconds_; | 
					
						
							|  |  |  |  | 			if (total_ms < 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 				result.append(L"-"); | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				total_ms = -total_ms; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			long hour = total_ms / Hour.milliseconds_; | 
					
						
							|  |  |  |  | 			long min = total_ms / Min.milliseconds_ - hour * 60; | 
					
						
							|  |  |  |  | 			long sec = total_ms / Sec.milliseconds_ - (hour * 60 * 60 + min * 60); | 
					
						
							|  |  |  |  | 			long ms = total_ms % Sec.milliseconds_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (hour) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 				result.append(kiwano::to_wstring(hour)).append(L"h"); | 
					
						
							|  |  |  |  | 				result.append(kiwano::to_wstring(min)).append(L"m"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			else if(min) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 				result.append(kiwano::to_wstring(min)).append(L"m"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (ms != 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				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"); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			else if (sec != 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 				result.append(kiwano::to_wstring(sec)).append(L"s"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return result; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		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_; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		float kiwano::time::Duration::operator/(const Duration & other) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return static_cast<float>(milliseconds_) / other.milliseconds_; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator+(const Duration & other) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ + other.milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator-(const Duration & other) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ - other.milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator-() const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(-milliseconds_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator*(int val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ * val); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::Duration::operator*(unsigned long long val) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ * val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator*(float val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ * val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator*(double val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ * val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator*(long double val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ * val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator/(int val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(milliseconds_ / val); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator/(float val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ / val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const Duration Duration::operator/(double val) const | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return Duration(static_cast<long>(milliseconds_ / val)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator+=(const Duration &other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ += other.milliseconds_; | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator-=(const Duration &other) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ -= other.milliseconds_; | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator*=(int val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ *= val; | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator/=(int val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<long>(milliseconds_ / val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator*=(float val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<long>(milliseconds_ * val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator/=(float val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<long>(milliseconds_ / val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator*=(double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<long>(milliseconds_ * val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		Duration & Duration::operator/=(double val) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			milliseconds_ = static_cast<long>(milliseconds_ / val); | 
					
						
							|  |  |  |  | 			return (*this); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator*(int val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator/(int val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator*(float val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator/(float val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator*(double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator/(double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur / val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		const Duration kiwano::time::operator*(long double val, const Duration & dur) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return dur * val; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 		Duration Duration::Parse(const String& str) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			size_t len = str.length(); | 
					
						
							|  |  |  |  | 			size_t pos = 0; | 
					
						
							|  |  |  |  | 			bool negative = false; | 
					
						
							|  |  |  |  | 			Duration d; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 			if (!std::regex_match(str.c_str(), duration_regex)) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 				KGE_ERROR_LOG(L"Duration::Parse failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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; | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				String num_str = str.substr(pos, i - pos); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 				pos = i; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (num_str.empty() || num_str == L".") | 
					
						
							|  |  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 					KGE_ERROR_LOG(L"Duration::Parse failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 					return Duration(); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				// <20><>λ
 | 
					
						
							|  |  |  |  | 				for (; i < len; ++i) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					wchar_t ch = str[i]; | 
					
						
							|  |  |  |  | 					if (ch == L'.' || L'0' <= ch && ch <= L'9') | 
					
						
							|  |  |  |  | 					{ | 
					
						
							|  |  |  |  | 						break; | 
					
						
							|  |  |  |  | 					} | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				String unit_str = str.substr(pos, i - pos); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 				pos = i; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (unit_map.find(unit_str) == unit_map.end()) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 					KGE_ERROR_LOG(L"Duration::Parse failed, invalid duration"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 					return Duration(); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 02:25:17 +08:00
										 |  |  |  | 				double num = std::wcstod(num_str.c_str(), nullptr); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 				Duration unit = unit_map.at(unit_str); | 
					
						
							|  |  |  |  | 				d += unit * num; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (negative) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				d = -d; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return d; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |