mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 08:50:05 +00:00 
			
		
		
		
	Time and integer parsing
This commit is contained in:
		
							parent
							
								
									c8d8b24317
								
							
						
					
					
						commit
						2cd76d7da4
					
				
					 4 changed files with 62 additions and 0 deletions
				
			
		|  | @ -88,5 +88,24 @@ std::string ToString(const std::vector<std::string>& v) | |||
|    return value; | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| std::optional<T> TryParseUnsignedLong(const std::string& str) | ||||
| { | ||||
|    std::optional<T> value = std::nullopt; | ||||
| 
 | ||||
|    try | ||||
|    { | ||||
|       value = static_cast<T>(std::stoul(str)); | ||||
|    } | ||||
|    catch (const std::exception&) | ||||
|    { | ||||
|    } | ||||
| 
 | ||||
|    return value; | ||||
| } | ||||
| 
 | ||||
| template std::optional<std::uint16_t> | ||||
| TryParseUnsignedLong<std::uint16_t>(const std::string& str); | ||||
| 
 | ||||
| } // namespace util
 | ||||
| } // namespace scwx
 | ||||
|  |  | |||
|  | @ -10,6 +10,12 @@ | |||
| 
 | ||||
| #include <scwx/util/time.hpp> | ||||
| 
 | ||||
| #include <sstream> | ||||
| 
 | ||||
| #if !defined(_MSC_VER) | ||||
| #   include <date/date.h> | ||||
| #endif | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace util | ||||
|  | @ -55,5 +61,33 @@ std::string TimeString(std::chrono::system_clock::time_point time, | |||
|    return os.str(); | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| std::optional<std::chrono::sys_time<T>> | ||||
| TryParseDateTime(const std::string& dateTimeFormat, const std::string& str) | ||||
| { | ||||
|    using namespace std::chrono; | ||||
| 
 | ||||
| #if !defined(_MSC_VER) | ||||
|    using namespace date; | ||||
| #endif | ||||
| 
 | ||||
|    std::optional<std::chrono::sys_time<T>> value = std::nullopt; | ||||
|    std::chrono::sys_time<T>                dateTime; | ||||
|    std::istringstream                      ssDateTime {str}; | ||||
| 
 | ||||
|    ssDateTime >> parse(dateTimeFormat, dateTime); | ||||
| 
 | ||||
|    if (!ssDateTime.fail()) | ||||
|    { | ||||
|       value = dateTime; | ||||
|    } | ||||
| 
 | ||||
|    return value; | ||||
| } | ||||
| 
 | ||||
| template std::optional<std::chrono::sys_time<std::chrono::seconds>> | ||||
| TryParseDateTime<std::chrono::seconds>(const std::string& dateTimeFormat, | ||||
|                                        const std::string& str); | ||||
| 
 | ||||
| } // namespace util
 | ||||
| } // namespace scwx
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat