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

+1
View File
@@ -844,6 +844,7 @@ local function civilFromDays(z: number): (number, number, number)
return (if m <= 2 then y + 1 else y), m, d
end
DateLib.civilFromDays = civilFromDays -- days since 1970-01-01 -> year, month, day (proleptic Gregorian)
DateLib.daysFromCivil = daysFromCivil -- year, month, day -> days since 1970-01-01
local MDAYS = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
-- new(fields) -> Date (kind inferred from the fields present when not given)
function DateLib.new(f: { [string]: any }): Date
+2 -19
View File
@@ -43,23 +43,6 @@ function Arj.sniff(r: Bytes.Reader): number
end
-- ------------------------------------------------------------------------------------------------ decode
local function dosTime(v: number): number?
local time, date = v % 65536, v // 65536
if date == 0 then
return nil
end
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
if m < 1 or m > 12 or d < 1 then
return nil
end
local yy = if m <= 2 then y - 1 else y
local era = yy // 400
local yoe = yy - era * 400
local doy = (153 * (if m > 2 then m - 3 else m + 9) + 2) // 5 + d - 1
local days = era * 146097 + yoe * 365 + yoe // 4 - yoe // 100 + doy - 719468
return days * 86400 + bit32.rshift(time, 11) * 3600 + bit32.band(bit32.rshift(time, 5), 63) * 60 + bit32.band(time, 31) * 2
end
type Header = { fields: Bytes.Reader, name: string, comment: string, size: number, at: number, next: number }
-- the header block at p: 60 EA, size, the basic header, its CRC-32, extended headers; nil at the end marker
@@ -134,7 +117,7 @@ function Arj.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
a.meta.archive = main.name
a.meta.comment = if main.comment ~= "" then main.comment else nil
a.meta.os = OS[host]
a.meta.created = dosTime(created)
a.meta.created = Archive.fromDos(created)
a.meta.source = { format = "arj", version = version, minVersion = minVersion, flags = flags }
local p = main.next
local count = 0
@@ -176,7 +159,7 @@ function Arj.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
size = if isDir then 0 else size,
compressedSize = packed,
method = if method == 0 then "store" else "arj" .. method,
mtime = dosTime(stamp),
mtime = Archive.fromDos(stamp),
dir = isDir,
mode = mode,
os = OS[hostOs],
+1 -18
View File
@@ -58,23 +58,6 @@ function Lha.sniff(r: Bytes.Reader): number
end
-- ------------------------------------------------------------------------------------------------ decode
local function dosTime(v: number): number?
local time, date = v % 65536, v // 65536
if date == 0 then
return nil
end
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
if m < 1 or m > 12 or d < 1 then
return nil
end
local yy = if m <= 2 then y - 1 else y
local era = yy // 400
local yoe = yy - era * 400
local doy = (153 * (if m > 2 then m - 3 else m + 9) + 2) // 5 + d - 1
local days = era * 146097 + yoe * 365 + yoe // 4 - yoe // 100 + doy - 719468
return days * 86400 + bit32.rshift(time, 11) * 3600 + bit32.band(bit32.rshift(time, 5), 63) * 60 + bit32.band(time, 31) * 2
end
-- a path with "/" separators, without a leading "/" and without "." / ".." components (as lhasa collapses them)
local function path(s: string): string
s = string.gsub(s, "[\\\255]", "/")
@@ -184,7 +167,7 @@ local function header(r: Bytes.Reader, p: number): (Header?, string?)
local nlen = hr:u8()
h.name = hr:string(nlen)
h.crc = hr:u16le()
h.mtime = dosTime(stamp)
h.mtime = Archive.fromDos(stamp)
if level == 0 then
-- an optional Unix extension: 'U', mtime, mode, uid, gid
if hr:remaining() >= 11 and (hr:peekU8() == 0x55 or hr:peekU8() == 0x4B) then
+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
+5 -45
View File
@@ -12,6 +12,8 @@
--
-- DOS dates are local time without a zone: they are read and written as UTC. Never throws; every chain is bounded by
-- the cluster count (loops are errors).
local Archive = require("@formats/containers/Archive")
local Fat = {}
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
@@ -232,25 +234,6 @@ local function readChain(v: Volume, first: number, size: number?): (buffer?, str
end
-- ------------------------------------------------------------------------------------------------ directories
local function dosTime(date: number, time: number): number?
if date == 0 then
return nil
end
local y, m, d = 1980 + bit32.rshift(date, 9), bit32.band(bit32.rshift(date, 5), 15), bit32.band(date, 31)
local hh, mm, ss = bit32.rshift(time, 11), bit32.band(bit32.rshift(time, 5), 63), bit32.band(time, 31) * 2
if m < 1 or m > 12 or d < 1 then
return nil
end
-- days from civil (Howard Hinnant's algorithm)
local yy = if m <= 2 then y - 1 else y
local era = yy // 400
local yoe = yy - era * 400
local doy = (153 * (if m > 2 then m - 3 else m + 9) + 2) // 5 + d - 1
local doe = yoe * 365 + yoe // 4 - yoe // 100 + doy
local days = era * 146097 + doe - 719468
return days * 86400 + hh * 3600 + mm * 60 + ss
end
local function latin1(s: string): string
if not string.find(s, "[\128-\255]") then
return s
@@ -351,7 +334,7 @@ local function parseDir(d: buffer, prefix: string, out: { Entry }): { Entry }
name = if isDir then name .. "/" else name,
dir = isDir,
size = if isDir then 0 else readu32(d, p + 28),
mtime = dosTime(readu16(d, p + 24), readu16(d, p + 22)),
mtime = Archive.fromDos(readu16(d, p + 24) * 65536 + readu16(d, p + 22)),
cluster = cluster,
attr = attr,
shortName = short,
@@ -450,31 +433,8 @@ local GEOMETRY = {
local VALID83 = "^[%w!#%$%%&'%(%)%-@%^_`{}~]+$"
local function toDos(t: number?): (number, number)
local s = math.floor(t or 315532800)
if s < 315532800 then
s = 315532800 -- 1980-01-01
end
local days = s // 86400
local rem = s - days * 86400
-- civil from days (Howard Hinnant)
local z = days + 719468
local era = z // 146097
local doe = z - era * 146097
local yoe = (doe - doe // 1460 + doe // 36524 - doe // 146096) // 365
local y = yoe + era * 400
local doy = doe - (365 * yoe + yoe // 4 - yoe // 100)
local mp = (5 * doy + 2) // 153
local d = doy - (153 * mp + 2) // 5 + 1
local m = if mp < 10 then mp + 3 else mp - 9
if m <= 2 then
y += 1
end
if y > 2107 then
y, m, d = 2107, 12, 31
end
local date = bit32.bor(bit32.lshift(y - 1980, 9), bit32.lshift(m, 5), d)
local time = bit32.bor(bit32.lshift(rem // 3600, 11), bit32.lshift((rem // 60) % 60, 5), (rem % 60) // 2)
return date, time
local v = Archive.toDos(t)
return v // 65536, v % 65536
end
type Node = { name: string, data: buffer?, dir: boolean, mtime: number?, children: { Node }, byName: { [string]: Node }, cluster: number }