lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I took a slightly different approach for a Time class I did for Grunt (the 
source code is available at luaforge). I converted the Windows FileTime format 
to a double (floating point) number scaled to days and fractions of a day. The 
number is actually stored as GMT (UTC if you want to be pedantic!) and 
converted to and from local time in the accessors.

This has advantages: there are no discontinuity issues (which famously plague 
the UNIX system). Negative numbers can be used to represent dates prior to the 
datum and moving arbitrarily far from the datum you just loose resolution 
rather than running into a brick wall like you do with integers. With 64 bit 
floats the loss of resolution is not really significant over reasonable time 
scales (Windows may record time to 100-nanosecond resolution, but I do not 
believe the accuracy is anything like that!).

Although I use a double in a userdata, of course this is the same as the 
default Lua number and could be represented as such.

-----Original Message-----
[] On Behalf Of KHMan
Sent: 05 May 2009 04:19
To: Lua list
Subject: Re: os.time epoch on Windows

Vaughan McAlley wrote:
> Thanks for the help. It's nice to know I can use the Lua timestamps
> and they won't be translating as 1639 on Windows clients. The closest
> thing to the Unix time-stamp on Windows seems to be FileTime, a 64-bit
> integer counting 100-nanosecond intervals from 1601, which wouldn't
> work with Lua numbers.

When you want to view a FileTime as a sane local time, you'd have
to make another two calls to FileTimeToLocalFileTime and
FileTimeToSystemTime, the result of the latter can then be loaded
into a Lua table. IIRC, in Cygwin, they subtract a FileTime by a
big number to get to the Unix epoch, then scale accordingly. In
most cases it is easier to use the C library, but sometimes it is
necessary to write your own file system layer, e.g. to use Unicode
filenames in Win32.

>> Thu Jan  1 00:00:01 1970
> [snip]

Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

Attachment: smime.p7s
Description: S/MIME cryptographic signature