VoidArchive/TimeStamp.h

47 lines
No EOL
817 B
C++

#pragma once
#include <cstdint>
#include <ctime>
class TimeStamp
{
public:
TimeStamp()
:time(std::time(nullptr))
{}
~TimeStamp() = default;
uint32_t get()
{
#if defined(_WIN32) localtime_s(&lt, &time);
#else localtime_r(&lt, &time);
#endif
uint16_t d = dosDate(lt);
uint16_t t = dosTime(lt);
uint32_t combined = ((uint32_t)d << 16) | t;
return combined;
}
private:
std::time_t time;
std::tm lt{};
uint16_t dosDate(const std::tm& t)
{
int year = t.tm_year + 1900;
int y = (year >= 1980) ? (year - 1980) : 0;
int m = t.tm_mon + 1;
int d = t.tm_mday;
return static_cast<uint16_t>((y << 9) | (m << 5) | d);
}
uint16_t dosTime(const std::tm& t)
{
int h = t.tm_hour;
int min = t.tm_min;
int s2 = t.tm_sec / 2;
return static_cast<uint16_t>((h << 11) | (min << 5) | s2);
}
};