Time related functions
These functions let you conveniently process time encoded in a couple of formats, namely Windows FILETIME and Unix time_t formats.
Time = GetFileTime(UseSystemTime) Time = PeekFileTime(UseSystemTime) Time = GetUnixTime(UseSystemTime) Time = PeekUnixTime(UseSystemTime) Time = DecodeFileTime(ReaderData, UseSystemTime) Time = DecodeUnixTime(ReaderData, UseSystemTime)
All functions of this set return this Lua table with the following structure (which is, as it is easy to see, a direct equivalent of the Windows SYSTEMTIME structure):
Time = { Year = ..., -- year Month = ..., -- January = 1, February = 2, and so on DayOfWeek = ..., -- Sunday = 0, Monday = 1, and so on Day = ..., -- 1...31 Hour = ..., -- 0...23 Minute = ..., -- 0...59 Second = ..., -- 0...59 Milliseconds = ... -- 0...999 }If any of these functions fails, a nil value is returned instead of Time table.
All functions of this set expect this boolean parameter. Its puprose is simple: if you want the time you're working with to be expressed as local time, set this parameter to false (or simply omit). If this parameter evaluates to true, the time returned is expressed as system time. Internal implementation of Decode...Time() functions calls either FileTimeToLocalFileTime() or FileTimeToSystemTime() Win32 API functions depending on this parameter.
Time = GetFileTime(UseSystemTime)
Time = PeekFileTime(UseSystemTime)
These functions read FILETIME Win32 API structure from the current position of the current file (8 bytes) and return Lua Time table. The only difference between them is that the Get...() function advances the file pointer while Peek...() does not change the current file position.
Time = GetUnixTime(UseSystemTime)
Time = PeekUnixTime(UseSystemTime)
These functions read standard Unix time_t value (4 bytes) from the current position of the current file. The only difference between them is that the Get...() function advances the file pointer while Peek...() does not change the current file position.
Time = DecodeFileTime(ReaderData, UseSystemTime)
Time = DecodeUnixTime(ReaderData, UseSystemTime)
These internal functions do the bulk of the time processing. Here is how other time functions are defined via these two:
function PeekFileTime(UseSystemTime) return DecodeFileTime(PeekBytes(8), UseSystemTime) end
function GetFileTime(UseSystemTime) return DecodeFileTime(ReadBytes(8), UseSystemTime) end
function PeekUnixTime(UseSystemTime) return DecodeUnixTime(PeekUI32(), UseSystemTime) end
function GetUnixTime(UseSystemTime) return DecodeUnixTime(GetUI32(), UseSystemTime) endHere is a simple real life example of time functions usage:
-- get and neatly format file time (like "3 May 2008 23:25:07") local function FormatFileTime()
local MonthTable = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
local x = GetFileTime()
if not x then return "invalid FILETIME structure" end
return string.format("%d %s %d %d:%02d:%02d", x.Day, MonthTable[x.Month], x.Year, x.Hour, x.Minute, x.Second)
end