On Sat, 16 Mar 2024 at 19:25, Bjorn Buckwalter
<
bjorn.bu...@gmail.com> wrote:
> Thanks all for the suggestions! I should clarify, though: I am working with files of time series data (satellite ephemeris) and as such am more interested in manipulating (parsing, arithmetic, formatting) date and time data rather than accurately determining the current time or measuring elapsed time. I could probably wrap something around the os module functions adding a nanosecond field, but thought something might be out there already (Denis pointed to luatz, but from the docs alone it isn't obvious to me if fractional seconds are supported).
I do similar stuff with CDRs, but my external representation are
normally standarized to ISO 8601 ( with explicit +00 timezone ) so
parsing is easy.
Leaving aside parsing from text, you can use fractional seconds if 53
bits is enough for you, this should go to about microseconds, in this
times, wrap os.date() to handle that:
> function tdump(t) for k,v in pairs(t) do print(k,v) end endAnd you can wrap it into a function adding the fractional part into it:
> function explode(t) s,f=math.modf(t) res=os.date('*t',s); res.usec=math.floor(f*1000000); return res; end
> print(tdump(explode(os.time()+0.1234567)))
month 3
min 57
day 16
wday 7
year 2024
yday 76
usec 123456
isdst false
hour 19
sec 12
Backwards conversion is easy, os.time() does not care about usec
field, just wrap and add.
I, personally, would go for a 64 bit nanoseconds count ( which gives
you...2^63/365.2425/86400/1e9= 292.27702462692771494535 years range )
and just do the div/mod mul/add adjustements in a wrapper. Arithmetic
is then easy.
If you want to roll your own conversions from/to parts/strings, for
complete control, I did a java class for that clocking at 583 lines /
189 semicolons, with a lot of unneeded functionality ( it is a library
), I have some in perl in less than 30 lines for special purposes, lua
should have a similar count. I have found UTC date/time conversions to
numbers just have two tricks, use two arrays for acumulated day count
before start of month, one for leap, or for other years, and
precalculate a numeric value for days since 01-01-year 0 ( which does
not exist but is useful ) to the epoch.
Francisco Olarte.