mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 04:00:04 +00:00
Time and integer parsing
This commit is contained in:
parent
c8d8b24317
commit
2cd76d7da4
4 changed files with 62 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
@ -29,5 +30,8 @@ std::vector<std::string> ParseTokens(const std::string& s,
|
||||||
|
|
||||||
std::string ToString(const std::vector<std::string>& v);
|
std::string ToString(const std::vector<std::string>& v);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::optional<T> TryParseUnsignedLong(const std::string& str);
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#if !defined(_MSC_VER)
|
#if !defined(_MSC_VER)
|
||||||
# include <date/tz.h>
|
# include <date/tz.h>
|
||||||
|
|
@ -24,5 +25,9 @@ std::string TimeString(std::chrono::system_clock::time_point time,
|
||||||
const time_zone* timeZone = nullptr,
|
const time_zone* timeZone = nullptr,
|
||||||
bool epochValid = true);
|
bool epochValid = true);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::optional<std::chrono::sys_time<T>>
|
||||||
|
TryParseDateTime(const std::string& dateTimeFormat, const std::string& str);
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
|
|
@ -88,5 +88,24 @@ std::string ToString(const std::vector<std::string>& v)
|
||||||
return value;
|
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 util
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,12 @@
|
||||||
|
|
||||||
#include <scwx/util/time.hpp>
|
#include <scwx/util/time.hpp>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#if !defined(_MSC_VER)
|
||||||
|
# include <date/date.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace scwx
|
namespace scwx
|
||||||
{
|
{
|
||||||
namespace util
|
namespace util
|
||||||
|
|
@ -55,5 +61,33 @@ std::string TimeString(std::chrono::system_clock::time_point time,
|
||||||
return os.str();
|
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 util
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue