SettingsHandler: Fix generated serial numbers

Must be 9 characters at most; otherwise the serial number will be
rejected by SDK libraries, as there is a check to ensure the string
length is strictly lower than 10.
This commit is contained in:
Léo Lam 2017-08-08 23:23:46 +08:00
parent a25f7b9b4c
commit 58b7350562
1 changed files with 9 additions and 11 deletions

View File

@ -8,6 +8,8 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <iomanip>
#include <sstream>
#include <string> #include <string>
#ifdef _WIN32 #ifdef _WIN32
@ -134,16 +136,12 @@ void SettingsHandler::WriteByte(u8 b)
std::string SettingsHandler::GenerateSerialNumber() std::string SettingsHandler::GenerateSerialNumber()
{ {
time_t rawtime; const std::time_t t = std::time(nullptr);
tm* timeinfo;
char buffer[12];
char serialNumber[12];
time(&rawtime); // Must be 9 characters at most; otherwise the serial number will be rejected by SDK libraries,
timeinfo = localtime(&rawtime); // as there is a check to ensure the string length is strictly lower than 10.
strftime(buffer, 11, "%j%H%M%S", timeinfo); // 3 for %j, 2 for %H, 2 for %M, 2 for %S.
std::stringstream stream;
snprintf(serialNumber, 11, "%s%i", buffer, (Common::Timer::GetTimeMs() >> 1) & 0xF); stream << std::put_time(std::localtime(&t), "%j%H%M%S");
serialNumber[10] = 0; return stream.str();
return std::string(serialNumber);
} }