1
0
Fork 0
forked from dlang/cdcdb
cdcdb/source/cdcdb/lib/uts.d
Alexander Zhirov 1f50b21457
Библиотека модернизирована под использование в БД в качестве первичных ключей UUID идентификаторов.
Время фиксируется в Unix Timestamp метке с миллисекундами.
Добавлена новая сущеность StorageFile, который является отправной точкой для работы со снимками.
Расширен интерфейс Storage - добавлены дополнительные фозможности для работы со снимками.
Введены структуры: для работы со временм - UTS, для работы с идентификатором - Identifier.
2025-09-30 02:39:29 +03:00

61 lines
1.2 KiB
D
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module cdcdb.lib.uts;
import std.datetime : SysTime, msecs;
// 2050-01-01 00:00:00 UTC
private enum UTS_LAST_TS = 0x967a7600; // 2524608000L
// Extended
private enum UTS_LAST_TS_EXT = UTS_LAST_TS * 1_000L;
// Unix Timestamp с миллисекундами
struct UTS
{
private:
long _ts;
long calc(SysTime systime) {
long millis = systime.toUnixTime() * 1000L + systime.fracSecs.total!"msecs";
return millis;
}
public:
this(long ts) {
assert(ts < UTS_LAST_TS_EXT);
_ts = ts;
}
this(SysTime systime) {
_ts = calc(systime);
}
void opAssign(SysTime systime) {
_ts = calc(systime);
}
void opAssign(long ts) {
assert(ts < UTS_LAST_TS_EXT);
_ts = ts;
}
string toString() const
{
import std.format : format;
string formatStr = "%04d-%02d-%02d %02d:%02d:%02d.%03d";
long seconds = _ts / 1_000L;
long millis = _ts % 1_000L;
auto sysTime = SysTime.fromUnixTime(seconds) + msecs(millis);
return format(formatStr,
sysTime.year, sysTime.month, sysTime.day,
sysTime.hour, sysTime.minute, sysTime.second,
sysTime.fracSecs.total!"msecs");
}
@property const(SysTime) sys() const @safe {
return SysTime.fromUnixTime(_ts / 1_000L);
}
@property long unix() const @safe {
return _ts;
}
}