4 * Wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
8 * (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
9 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include <wsutil/nstime.h>
24 #define NS_PER_S 1000000000
26 /* WSLUA_CONTINUE_MODULE Pinfo */
29 WSLUA_CLASS_DEFINE(NSTime
,FAIL_ON_NULL("NSTime"));
30 /* NSTime represents a nstime_t. This is an object with seconds and nanoseconds. */
32 WSLUA_CONSTRUCTOR
NSTime_new(lua_State
*L
) {
33 /* Creates a new NSTime object. */
34 #define WSLUA_OPTARG_NSTime_new_SECONDS 1 /* Seconds. */
35 #define WSLUA_OPTARG_NSTime_new_NSECONDS 2 /* Nano seconds. */
36 lua_Integer secs
= luaL_optinteger(L
,WSLUA_OPTARG_NSTime_new_SECONDS
,0);
37 lua_Integer nsecs
= luaL_optinteger(L
,WSLUA_OPTARG_NSTime_new_NSECONDS
,0);
39 NSTime nstime
= g_new(nstime_t
, 1);
40 if (!nstime
) return 0;
41 nstime
->secs
= (time_t) secs
;
42 nstime
->nsecs
= (int) nsecs
;
46 WSLUA_RETURN(1); /* The new NSTime object. */
49 WSLUA_METAMETHOD
NSTime__call(lua_State
* L
) { /* Creates a NSTime object. */
50 #define WSLUA_OPTARG_NSTime__call_SECONDS 1 /* Seconds. */
51 #define WSLUA_OPTARG_NSTime__call_NSECONDS 2 /* Nanoseconds. */
52 lua_remove(L
,1); /* remove the table */
53 WSLUA_RETURN(NSTime_new(L
)); /* The new NSTime object. */
56 WSLUA_METHOD
NSTime_tonumber(lua_State
* L
) {
57 /* Returns a Lua number of the `NSTime` representing seconds from epoch. */
58 NSTime nstime
= checkNSTime(L
,1);
59 lua_pushnumber(L
, (lua_Number
)nstime_to_sec(nstime
));
60 WSLUA_RETURN(1); /* The Lua number. */
63 WSLUA_METAMETHOD
NSTime__tostring(lua_State
* L
) {
64 NSTime nstime
= checkNSTime(L
,1);
66 long secs
= (long)nstime
->secs
;
67 int nsecs
= nstime
->nsecs
;
68 bool negative_zero
= false;
70 /* Time is defined as sec + nsec/10^9, both parts can be negative.
71 * Translate this into the more familiar sec.nsec notation instead. */
72 if (secs
> 0 && nsecs
< 0) {
73 /* sign mismatch: (2, -3ns) -> 1.7 */
76 } else if (secs
< 0 && nsecs
> 0) {
77 /* sign mismatch: (-2, 3ns) -> -1.7 */
78 nsecs
= NS_PER_S
- nsecs
;
80 } else if (nsecs
< 0) {
81 /* Drop sign, the integer part already has it: (-2, -3ns) -> -2.3 */
83 /* In case the integer part is zero, it does not has a sign, so remember
84 * that it must be explicitly added. */
85 negative_zero
= secs
== 0;
89 str
= wmem_strdup_printf(NULL
, "-0.%09d", nsecs
);
91 str
= wmem_strdup_printf(NULL
, "%ld.%09d", secs
, nsecs
);
93 lua_pushstring(L
, str
);
96 WSLUA_RETURN(1); /* The string representing the nstime. */
98 WSLUA_METAMETHOD
NSTime__add(lua_State
* L
) { /* Calculates the sum of two NSTimes. */
99 NSTime time1
= checkNSTime(L
,1);
100 NSTime time2
= checkNSTime(L
,2);
101 NSTime time3
= (NSTime
)g_malloc (sizeof (nstime_t
));
103 nstime_sum (time3
, time1
, time2
);
104 pushNSTime (L
, time3
);
109 WSLUA_METAMETHOD
NSTime__sub(lua_State
* L
) { /* Calculates the diff of two NSTimes. */
110 NSTime time1
= checkNSTime(L
,1);
111 NSTime time2
= checkNSTime(L
,2);
112 NSTime time3
= (NSTime
)g_malloc (sizeof (nstime_t
));
114 nstime_delta (time3
, time1
, time2
);
115 pushNSTime (L
, time3
);
120 WSLUA_METAMETHOD
NSTime__unm(lua_State
* L
) { /* Calculates the negative NSTime. */
121 NSTime time1
= checkNSTime(L
,1);
122 NSTime time2
= (NSTime
)g_malloc (sizeof (nstime_t
));
124 nstime_set_zero (time2
);
125 nstime_subtract (time2
, time1
);
126 pushNSTime (L
, time2
);
131 WSLUA_METAMETHOD
NSTime__eq(lua_State
* L
) { /* Compares two NSTimes. */
132 NSTime time1
= checkNSTime(L
,1);
133 NSTime time2
= checkNSTime(L
,2);
136 if (nstime_cmp(time1
, time2
) == 0)
139 lua_pushboolean(L
,result
);
144 WSLUA_METAMETHOD
NSTime__le(lua_State
* L
) { /* Compares two NSTimes. */
145 NSTime time1
= checkNSTime(L
,1);
146 NSTime time2
= checkNSTime(L
,2);
149 if (nstime_cmp(time1
, time2
) <= 0)
152 lua_pushboolean(L
,result
);
157 WSLUA_METAMETHOD
NSTime__lt(lua_State
* L
) { /* Compares two NSTimes. */
158 NSTime time1
= checkNSTime(L
,1);
159 NSTime time2
= checkNSTime(L
,2);
162 if (nstime_cmp(time1
, time2
) < 0)
165 lua_pushboolean(L
,result
);
171 /* WSLUA_ATTRIBUTE NSTime_secs RW The NSTime seconds. */
172 WSLUA_ATTRIBUTE_INTEGER_GETTER(NSTime
,secs
);
173 WSLUA_ATTRIBUTE_INTEGER_SETTER(NSTime
,secs
,time_t);
175 /* WSLUA_ATTRIBUTE NSTime_nsecs RW The NSTime nano seconds. */
176 WSLUA_ATTRIBUTE_INTEGER_GETTER(NSTime
,nsecs
);
177 WSLUA_ATTRIBUTE_INTEGER_SETTER(NSTime
,nsecs
,int);
179 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
180 static int NSTime__gc(lua_State
* L
) {
181 NSTime nstime
= toNSTime(L
,1);
183 if (!nstime
) return 0;
189 /* This table is ultimately registered as a sub-table of the class' metatable,
190 * and if __index/__newindex is invoked then it calls the appropriate function
191 * from this table for getting/setting the members.
193 WSLUA_ATTRIBUTES NSTime_attributes
[] = {
194 WSLUA_ATTRIBUTE_RWREG(NSTime
,secs
),
195 WSLUA_ATTRIBUTE_RWREG(NSTime
,nsecs
),
199 WSLUA_METHODS NSTime_methods
[] = {
200 WSLUA_CLASS_FNREG(NSTime
,new),
201 WSLUA_CLASS_FNREG(NSTime
,tonumber
),
205 WSLUA_META NSTime_meta
[] = {
206 WSLUA_CLASS_MTREG(NSTime
,tostring
),
207 WSLUA_CLASS_MTREG(NSTime
,add
),
208 WSLUA_CLASS_MTREG(NSTime
,sub
),
209 WSLUA_CLASS_MTREG(NSTime
,unm
),
210 WSLUA_CLASS_MTREG(NSTime
,eq
),
211 WSLUA_CLASS_MTREG(NSTime
,le
),
212 WSLUA_CLASS_MTREG(NSTime
,lt
),
213 WSLUA_CLASS_MTREG(NSTime
,call
),
217 int NSTime_register(lua_State
* L
) {
218 WSLUA_REGISTER_CLASS_WITH_ATTRS(NSTime
);
224 * Editor modelines - https://www.wireshark.org/tools/modelines.html
229 * indent-tabs-mode: nil
232 * vi: set shiftwidth=4 tabstop=8 expandtab:
233 * :indentSize=4:tabSize=8:noTabs=true: