File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ description : " Wall-clock time, monotonic time, and native sleep utilities."
3+ ---
4+
5+ # ` timeutil `
6+
7+ Wall-clock time, monotonic time, and native sleep utilities.
8+
9+ ``` lua
10+ local timeutil = require (" timeutil" )
11+ local start = timeutil .mono ()
12+ timeutil .sleep (0.1 )
13+ print (timeutil .now () - start )
14+ ```
15+
16+ ## Functions
17+
18+ <a id =" fn-mono " ></a >
19+
20+ ### ` mono() `
21+
22+ Returns monotonic elapsed time in seconds.
23+
24+ ** Return** :
25+
26+ - ` seconds ` (` number ` )
27+
28+ ** Example** :
29+
30+ ``` lua
31+ local time = require (" timeutil" )
32+ local monotime = time .mono
33+
34+ local start = monotime ()
35+ time .sleep (0.5 )
36+ local stop = monotime ()
37+
38+ local elapsed_ms = math.floor ((stop - start ) * 1000 )
39+ print (elapsed_ms .. " ms" ) -- > 500ms
40+ ```
41+
42+ <a id =" fn-now " ></a >
43+
44+ ### ` now() `
45+
46+ Returns wall-clock Unix time in seconds.
47+
48+ ** Return** :
49+
50+ - ` seconds ` (` number ` )
51+
52+ ** Example** :
53+
54+ ``` lua
55+ local time = require (" timeutil" )
56+ local now = time .now ()
57+ local seconds = math.floor (now )
58+ local ms = math.floor ((now - seconds ) * 1000 )
59+ local dt = string.format (" %s.%03d" , os.date (" %Y-%m-%d %H:%M:%S" , seconds ), ms )
60+ print (dt ) -- > YYYY-MM-DD HH:mm:ss.SSS
61+ ```
62+
63+ <a id =" fn-sleep " ></a >
64+
65+ ### ` sleep(seconds) `
66+
67+ Blocks the current thread for the requested non-negative duration.
68+
69+ ** Parameters** :
70+
71+ - ` seconds ` (` number ` )
72+
73+ ** Example** :
74+
75+ ``` lua
76+ local time = require (" timeutil" )
77+ time .sleep (1.5 ) -- sleep for 1.5 seconds
78+ ```
You can’t perform that action at this time.
0 commit comments