How do I convert TvbRange.le_nstime()
to a string? I tried the following Lua:
local field_time = ProtoField.absolute_time("TIME","TIME",base.LOCAL);
...
function SCP_proto.dissector (buf, pkt, root){
local value={
[0] =0,
[1] =0,
[2] =0
}
...
for i=0,2,1 do
...
local example_subtree = subtree:add(field_time,buf(offset,8):le_nstime());
value[i] = tostring(buf(offset,8):le_nstime());
...
end
}
...but value[i] = tostring(buf(offset,8):le\_nstime())
does not work the way I expected. The time format of value[i]
is in seconds ("1336188353.000000150") instead of "Month|Day|Year|Time" format ("May 5, 2012 07:25:53.000000150"). However, in the Packet Details Pane, I see "May 5, 2012 07:25:53.000000150" as expected.
How can I get a formatted time-string from le_nstime()
like the one I see in the protocol tree?
Wireshark 1.7, Win 7, Lua 5.1
asked 05 Jun '12, 03:24
PavelMSTU
26●2●3●6
accept rate: 50%
edited 05 Jun '12, 16:15
helloworld
3.1k●4●20●41
Helloworld, very thenks to you!
But code:
local seconds = tostring(buf(offset,8):le_nstime())
seconds = tonumber(seconds)
value[i] = format_date(seconds)
don't work. Because tonumber function return nil.
I think this mistake arises, because 8 byte double can't converted in Lua. This code works correctly:
local seconds = tostring( buf(offset,4):le_nstime());
seconds = string.sub(seconds,0,10); --[[ Return 10 numbers of seconds, from January 1, 1970 ]]--
seconds = tonumber(seconds);
value[i] = format_date(seconds);
Actually, an
NSTime
can be either 4 or 8 bytes. You lose precision with 4 bytes.tonumber()
only returnsnil
for non-convertible strings. Thetostring()
conversion could've returned a non-convertible string (perhapsnil
), which would indicate a problem with the buffer contents.You don't need to parse out the seconds from the string since
tonumber()
handles floating-point numbers.