Skip to content

Commit

Permalink
[ADD] : add dates ISO8601 <> epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed May 20, 2024
1 parent 3ba339a commit 4f0afd0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/ctools/cTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ using namespace cocos2d;
namespace ct // cTools
{

/////////////////////////////////////////////
////// DATEs Conversions ////////////////////
/////////////////////////////////////////////

// convert a string ISO8601 time to epoch time
bool iso8601ToEpoch(const std::string& vIsoDateTime, const std::string& vTimeFormat, std::time_t& vOutTime);

// convert a epoch time to a string ISO8601 time
bool epochToISO8601(const std::time_t& vEpochTime, std::string& vOutTime);

/////////////////////////////////////////////
////// UTF8 <> WideString ///////////////////
/////////////////////////////////////////////
Expand Down Expand Up @@ -250,6 +260,9 @@ CTOOLS_API std::string toStr(const char* fmt, ...);
CTOOLS_API std::string toUpper(const std::string& vStr, const std::locale& vLocale = std::locale());
CTOOLS_API std::string toLower(const std::string& vStr, const std::locale& vLocale = std::locale());

// convert a string byte content to hex string
CTOOLS_API std::string toHex(const std::string& vStr);

template <typename T>
::std::string toStrFromArray(T* arr, size_t n, char delimiter = ';') {
if (arr) {
Expand Down
38 changes: 38 additions & 0 deletions src/cTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ SOFTWARE.
#include <cwchar>
#endif

bool ct::iso8601ToEpoch(const std::string& vIsoDateTime, const std::string& vTimeFormat, std::time_t& vOutTime) {
if (!vIsoDateTime.empty() && !vTimeFormat.empty()) {
struct std::tm time = {};
std::istringstream ss(vIsoDateTime);
ss >> std::get_time(&time, vTimeFormat.c_str());
if (ss.good()) {
time.tm_hour = 0;
time.tm_min = 0;
time.tm_sec = 0;
#ifdef _WIN32
vOutTime = _mkgmtime(&time);
#else
vOutTime = timegm(&time);
#endif
return true;
}
}
return false;
}

bool ct::epochToISO8601(const std::time_t& vEpochTime, std::string& vOutTime) {
auto tp = std::chrono::system_clock::from_time_t(vEpochTime);
auto tt = std::chrono::system_clock::to_time_t(tp);
auto* timeinfo = std::localtime(&tt);
std::ostringstream oss;
oss << std::put_time(timeinfo, "%Y-%m-%d");
if (oss.good()) {
vOutTime = oss.str();
return true;
}
return false;
}

std::string ct::UTF8Encode(const std::wstring& wstr) {
std::string res;
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
Expand Down Expand Up @@ -127,6 +160,11 @@ ::std::string ct::toLower(const std::string& vStr, const std::locale& vLocale) {
return str;
}

std::string ct::toHex(const std::string& vStr) {
CTOOL_DEBUG_BREAK;
return {};
}

/////////////////////////////////////////////////////////////
///////// bitwize ///////////////////////////////////////////
/////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 4f0afd0

Please sign in to comment.