ws-arch2: DOS date / time conversion shared in containers/Archive (fromDos / toDos), used by lha, arj and containers/Fat; Types.Date.daysFromCivil exported (additive)

This commit is contained in:
4DBug committed 2026-10-31 14:44:34 -05:00
1 parent 70cc54f7c8
commit d0b81f02c7
5 files changed
+36 -82

No files matched your search

+27
View File
@@ -176,6 +176,33 @@ function Archive.files(value: any, name: string?): ({ { name: string, data: buff
return out, nil
end
-- fromDos(stamp) -> unix seconds | nil: an MS-DOS date and time (stamp = date * 65536 + time, as LHA / ARJ / ZIP store
-- them little endian; local time without a zone, read as UTC); nil for a zero or invalid date
function Archive.fromDos(stamp: number): number?
local time, date = stamp % 65536, stamp // 65536
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
if date == 0 or m < 1 or m > 12 or d < 1 then
return nil
end
local secs = bit32.rshift(time, 11) * 3600 + bit32.band(bit32.rshift(time, 5), 63) * 60 + bit32.band(time, 31) * 2
return Types.Date.daysFromCivil(y, m, d) * 86400 + secs
end
-- toDos(unix?) -> date * 65536 + time (clamped to 1980-01-01 .. 2107-12-31)
function Archive.toDos(t: number?): number
if type(t) ~= "number" or t ~= t or t < 315532800 then
return 0x210000 -- 1980-01-01 00:00
end
local s = math.floor(t)
local days = s // 86400
local rem = s - days * 86400
local y, m, d = Types.Date.civilFromDays(days)
if y > 2107 then
y, m, d, rem = 2107, 12, 31, 86398
end
return ((y - 1980) * 512 + m * 32 + d) * 65536 + (rem // 3600) * 2048 + ((rem // 60) % 60) * 32 + (rem % 60) // 2
end
-- streamName(opts, ext) -> the entry name for a single-stream archive: opts.name without its compression extension
-- (".tgz" -> ".tar"), else "data"
function Archive.streamName(opts: any?, exts: { string }): string