aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorPhilipp Stephani2019-04-18 17:42:45 +0200
committerPhilipp Stephani2019-04-24 11:33:52 +0200
commitbffceab6339fb4042588b893ef754c6264379e75 (patch)
tree03747b5cb5275a24418aa9fb421977eb6c0ee063 /doc
parent5ae407aad4f2564fae7ddce077eb01aa8efa37fb (diff)
downloademacs-bffceab6339fb4042588b893ef754c6264379e75.tar.gz
emacs-bffceab6339fb4042588b893ef754c6264379e75.zip
Add conversions to and from struct timespec to module interface.
Time values are a fundamental data type, and such conversions are hard to implement within modules because of the various forms of time values in Emacs Lisp. Adding dedicated conversion functions can significantly simplify module code dealing with times. This approach uses nanosecond precision. While Emacs in theory has support for higher-precision time values, in practice most languages and standards, such as POSIX, C, Java, and Go, have settled on nanosecond-precision integers to represent time. * src/emacs-module.h.in: Add header for struct timespec. * src/module-env-27.h: Add module functions for time conversion. * src/emacs-module.c (module_extract_time, module_make_time): New functions. (initialize_environment): Use them. * test/data/emacs-module/mod-test.c (Fmod_test_add_nanosecond): New test function. (emacs_module_init): Define it. * test/src/emacs-module-tests.el (mod-test-add-nanosecond/valid) (mod-test-add-nanosecond/nil, mod-test-add-nanosecond/invalid): New unit tests. * doc/lispref/internals.texi (Module Values): Document time conversion functions.
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/internals.texi44
1 files changed, 44 insertions, 0 deletions
diff --git a/doc/lispref/internals.texi b/doc/lispref/internals.texi
index 25892d4b57c..0e7a1339e76 100644
--- a/doc/lispref/internals.texi
+++ b/doc/lispref/internals.texi
@@ -1387,6 +1387,38 @@ This function returns the value of a Lisp float specified by
1387@var{arg}, as a C @code{double} value. 1387@var{arg}, as a C @code{double} value.
1388@end deftypefn 1388@end deftypefn
1389 1389
1390@deftypefn Function struct timespec extract_time (emacs_env *@var{env}, emacs_value @var{time})
1391This function, which is available since Emacs 27, interprets
1392@var{time} as an Emacs Lisp time value and returns the corresponding
1393@code{struct timespec}. @xref{Time of Day}. @code{struct timespec}
1394represents a timestamp with nanosecond precision. It has the
1395following members:
1396
1397@table @code
1398@item time_t tv_sec
1399Whole number of seconds.
1400@item long tv_nsec
1401Fractional seconds as number of nanoseconds, always less than one
1402billion.
1403@end table
1404
1405@noindent
1406@xref{Elapsed Time,,,libc}.
1407
1408If @var{time} has higher precision than nanoseconds, then this
1409function truncates it to nanosecond precision. This function signals
1410an error if @var{time} (truncated to nanoseconds) cannot be
1411represented by @code{struct timespec}. For example, if @code{time_t}
1412is a 32-bit integral type, then a @var{time} value of ten billion
1413seconds would signal an error, but a @var{time} value of 600
1414picoseconds would get truncated to zero.
1415
1416If you need to deal with time values that are not representable by
1417@code{struct timespec}, or if you want higher precision, call the Lisp
1418function @code{encode-time} and work with its return value.
1419@xref{Time Conversion}.
1420@end deftypefn
1421
1390@deftypefn Function bool copy_string_contents (emacs_env *@var{env}, emacs_value @var{arg}, char *@var{buf}, ptrdiff_t *@var{len}) 1422@deftypefn Function bool copy_string_contents (emacs_env *@var{env}, emacs_value @var{arg}, char *@var{buf}, ptrdiff_t *@var{len})
1391This function stores the UTF-8 encoded text of a Lisp string specified 1423This function stores the UTF-8 encoded text of a Lisp string specified
1392by @var{arg} in the array of @code{char} pointed by @var{buf}, which 1424by @var{arg} in the array of @code{char} pointed by @var{buf}, which
@@ -1452,6 +1484,18 @@ This function takes a @code{double} argument @var{d} and returns the
1452corresponding Emacs floating-point value. 1484corresponding Emacs floating-point value.
1453@end deftypefn 1485@end deftypefn
1454 1486
1487@deftypefn Function emacs_value make_time (emacs_env *@var{env}, struct timespec @var{time})
1488This function, which is available since Emacs 27, takes a @code{struct
1489timespec} argument @var{time} and returns the corresponding Emacs
1490timestamp as a pair @code{(@var{ticks} . @var{hz})}. @xref{Time of
1491Day}. The return value represents exactly the same timestamp as
1492@var{time}: all input values are representable, and there is never a
1493loss of precision. @code{@var{time}.tv_sec} and
1494@code{@var{time}.tv_nsec} can be arbitrary values. In particular,
1495there's no requirement that @var{time} be normalized. This means that
1496@code{@var{time}.tv_nsec} can be negative or larger than 999,999,999.
1497@end deftypefn
1498
1455@deftypefn Function emacs_value make_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{strlen}) 1499@deftypefn Function emacs_value make_string (emacs_env *@var{env}, const char *@var{str}, ptrdiff_t @var{strlen})
1456This function creates an Emacs string from C text string pointed by 1500This function creates an Emacs string from C text string pointed by
1457@var{str} whose length in bytes, not including the terminating null 1501@var{str} whose length in bytes, not including the terminating null