OpenFusion/src/CNStructs.cpp

36 lines
1.1 KiB
C++
Raw Normal View History

2020-08-18 20:42:30 +00:00
#include "CNStructs.hpp"
#if defined _MSC_VER
#include <chrono>
#endif
2020-08-18 20:42:30 +00:00
std::string U16toU8(char16_t* src) {
2020-08-22 03:11:04 +00:00
try {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
return convert.to_bytes(src);
} catch(std::exception e) {
return "";
}
2020-08-18 20:42:30 +00:00
}
// returns number of char16_t that was written at des
2020-08-22 18:38:27 +00:00
size_t U8toU16(std::string src, char16_t* des) {
2020-08-18 20:42:30 +00:00
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string tmp = convert.from_bytes(src);
// copy utf16 string to buffer
memcpy(des, tmp.c_str(), sizeof(char16_t) * tmp.length());
des[tmp.length()] = '\0';
return tmp.length();
}
uint64_t getTime() {
2020-08-20 15:43:37 +00:00
#ifndef _MSC_VER
2020-08-18 20:42:30 +00:00
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
2020-08-20 15:43:37 +00:00
#else
std::chrono::milliseconds value = std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now())).time_since_epoch());
return (uint64_t)(value.count());
2020-08-20 15:43:37 +00:00
#endif
2020-08-18 20:42:30 +00:00
}