Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add timespec_get(3) in <time.h> and enable unconditionally s...



details:   https://anonhg.NetBSD.org/src/rev/33accbc91a8a
branches:  trunk
changeset: 818225:33accbc91a8a
user:      kamil <kamil%NetBSD.org@localhost>
date:      Tue Oct 04 09:41:40 2016 +0000

description:
Add timespec_get(3) in <time.h> and enable unconditionally struct timespec

These changes conforms to the C11 standard
References:
 - 7.27.1/3 Components of time (struct timespec)
 - 7.27.2.5 The timespec_get function

According to ISO/IEC 9899:201x (draft) <time.h> defines the timespec
structure and declares the timespec_get(3) function with TIME_UTC
definition.

According to a C++17 standard draft <ctime> offers the same interface in
the std:: namespace.

The timespec_get function modifies the timespec object pointed by ts
to hold the current calendar time in the given base. The standard notes
only the TIME_UTC base with implementation defined value, set it to 1
as zero is reserved for error handling. Once operation was successful this
function returns passed base, otherwise exits with zero.

The timespec struct was already part of the POSIX standard in <time.h>.

Enable this interface unconditionally in the header to allow to use it
in a code prior C11 and C++17 as an extension.

Review notes from <christos>

diffstat:

 distrib/sets/lists/comp/mi  |   5 ++-
 include/time.h              |  11 +++++-
 lib/libc/gen/Makefile.inc   |   9 +++--
 lib/libc/gen/timespec_get.3 |  74 +++++++++++++++++++++++++++++++++++++++++++++
 lib/libc/gen/timespec_get.c |  55 +++++++++++++++++++++++++++++++++
 5 files changed, 147 insertions(+), 7 deletions(-)

diffs (234 lines):

diff -r a74ed9bc10c7 -r 33accbc91a8a distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Tue Oct 04 09:21:05 2016 +0000
+++ b/distrib/sets/lists/comp/mi        Tue Oct 04 09:41:40 2016 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2063 2016/10/03 01:00:27 kamil Exp $
+#      $NetBSD: mi,v 1.2064 2016/10/04 09:41:40 kamil Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -9357,6 +9357,7 @@
 ./usr/share/man/cat3/timersub.0                        comp-c-catman           .cat
 ./usr/share/man/cat3/times.0                   comp-c-catman           .cat
 ./usr/share/man/cat3/timespec.0                        comp-c-catman           .cat
+./usr/share/man/cat3/timespec_get.0            comp-c-catman           .cat
 ./usr/share/man/cat3/timespecadd.0             comp-c-catman           .cat
 ./usr/share/man/cat3/timespecclear.0           comp-c-catman           .cat
 ./usr/share/man/cat3/timespeccmp.0             comp-c-catman           .cat
@@ -16565,6 +16566,7 @@
 ./usr/share/man/html3/timersub.html            comp-c-htmlman          html
 ./usr/share/man/html3/times.html               comp-c-htmlman          html
 ./usr/share/man/html3/timespec.html            comp-c-htmlman          html
+./usr/share/man/html3/timespec_get.html                comp-c-htmlman          html
 ./usr/share/man/html3/timespecadd.html         comp-c-htmlman          html
 ./usr/share/man/html3/timespecclear.html       comp-c-htmlman          html
 ./usr/share/man/html3/timespeccmp.html         comp-c-htmlman          html
@@ -23836,6 +23838,7 @@
 ./usr/share/man/man3/timersub.3                        comp-c-man              .man
 ./usr/share/man/man3/times.3                   comp-c-man              .man
 ./usr/share/man/man3/timespec.3                        comp-c-man              .man
+./usr/share/man/man3/timespec_get.3            comp-c-man              .man
 ./usr/share/man/man3/timespecadd.3             comp-c-man              .man
 ./usr/share/man/man3/timespecclear.3           comp-c-man              .man
 ./usr/share/man/man3/timespeccmp.3             comp-c-man              .man
diff -r a74ed9bc10c7 -r 33accbc91a8a include/time.h
--- a/include/time.h    Tue Oct 04 09:21:05 2016 +0000
+++ b/include/time.h    Tue Oct 04 09:41:40 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: time.h,v 1.46 2016/04/23 23:10:10 christos Exp $       */
+/*     $NetBSD: time.h,v 1.47 2016/10/04 09:41:41 kamil Exp $  */
 
 /*
  * Copyright (c) 1989, 1993
@@ -137,9 +137,12 @@
 extern int getdate_err;
 #endif
 
+/* ISO/IEC 9899:201x 7.27.1/3 Components of time */
+#include <sys/timespec.h>
+
 #if (_POSIX_C_SOURCE - 0) >= 199309L || (_XOPEN_SOURCE - 0) >= 500 || \
     defined(_NETBSD_SOURCE)
-#include <sys/time.h>          /* XXX for struct timespec */
+#include <sys/time.h>
 struct sigevent;
 struct itimerspec;
 int clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
@@ -231,6 +234,10 @@
 
 #endif /* _NETBSD_SOURCE */
 
+/* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
+#define TIME_UTC       1       /* time elapsed since epoch */
+int timespec_get(struct timespec *ts, int base);
+
 __END_DECLS
 
 #endif /* !_TIME_H_ */
diff -r a74ed9bc10c7 -r 33accbc91a8a lib/libc/gen/Makefile.inc
--- a/lib/libc/gen/Makefile.inc Tue Oct 04 09:21:05 2016 +0000
+++ b/lib/libc/gen/Makefile.inc Tue Oct 04 09:41:40 2016 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.195 2016/06/05 17:16:44 christos Exp $
+#      $NetBSD: Makefile.inc,v 1.196 2016/10/04 09:41:41 kamil Exp $
 #      from: @(#)Makefile.inc  8.6 (Berkeley) 5/4/95
 
 # gen sources
@@ -32,7 +32,7 @@
        siglist.c signal.c signame.c sigrelse.c \
        sigset.c sigsetops.c sleep.c \
        stringlist.c sysconf.c sysctl.c sysctlbyname.c sysctlgetmibinfo.c \
-       sysctlnametomib.c syslog.c telldir.c time.c \
+       sysctlnametomib.c syslog.c telldir.c time.c timespec_get.c \
        times.c toascii.c tolower_.c ttyname.c ttyslot.c toupper_.c ualarm.c \
        ulimit.c uname.c unvis.c usleep.c utime.c utimens.c utmp.c \
        utmpx.c valloc.c vis.c wait.c wait3.c waitid.c waitpid.c warn.c \
@@ -80,8 +80,9 @@
        raise.3 randomid.3 realpath.3 scandir.3 setjmp.3 setmode.3 \
        setproctitle.3 shquote.3 sighold.3 sigignore.3 siginterrupt.3 \
        signal.3 signbit.3 sigrelse.3 sigset.3 sigsetops.3 sleep.3 \
-       stringlist.3 sysconf.3 sysctl.3 syslog.3 time.3 times.3 \
-       timezone.3 toascii.3 tolower.3 toupper.3 ttyname.3 \
+       stringlist.3 sysconf.3 sysctl.3 syslog.3 \
+       time.3 timespec_get.3 times.3 timezone.3 toascii.3 tolower.3 \
+       toupper.3 ttyname.3 \
        ualarm.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 valloc.3 vis.3 \
        wordexp.3
 
diff -r a74ed9bc10c7 -r 33accbc91a8a lib/libc/gen/timespec_get.3
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/gen/timespec_get.3       Tue Oct 04 09:41:40 2016 +0000
@@ -0,0 +1,74 @@
+.\"    $NetBSD: timespec_get.3,v 1.1 2016/10/04 09:41:41 kamil Exp $
+.\"
+.\" Copyright (c) 2016 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Kamil Rytarowski.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd October 4, 2016
+.Dt TIMESPEC_GET 3
+.Os
+.Sh NAME
+.Nm timespec_get
+.Nd get current calendar time
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In time.h
+.Vt #define TIME_UTC 1
+.Ft int
+.Fn timespec_get "struct timespec *ts" "int base"
+.Sh DESCRIPTION
+The
+.Nm
+function sets the interval pointed to by
+.Fa ts
+to hold the current calendar time based on the specified time base in
+.Fa base .
+.Pp
+Currently the only supported valid base is
+.Dv TIME_UTC .
+It returns time elapsed since epoch.
+.Sh RETURN VALUES
+The
+.Nm
+function returns the passed value of
+.Fa base
+if successful, otherwise
+.Dv 0
+on failure.
+.\" .Sh ERRORS
+.Sh SEE ALSO
+.Xr clock_gettime 3
+.Sh STANDARDS
+The
+.Nm
+function conforms to
+.St -isoC-2011 .
+.Sh HISTORY
+This interface first appeared in
+.Nx 8 .
+.Sh AUTHORS
+.An Kamil Rytarowski Aq Mt kamil%NetBSD.org@localhost
diff -r a74ed9bc10c7 -r 33accbc91a8a lib/libc/gen/timespec_get.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/gen/timespec_get.c       Tue Oct 04 09:41:40 2016 +0000
@@ -0,0 +1,55 @@
+/*     $NetBSD: timespec_get.c,v 1.1 2016/10/04 09:41:41 kamil Exp $   */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Kamil Rytarowski.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: timespec_get.c,v 1.1 2016/10/04 09:41:41 kamil Exp $");
+#endif /* !defined lint */
+
+#include <assert.h>
+#include <time.h>
+
+/* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
+
+int
+timespec_get(struct timespec *ts, int base)
+{
+
+       _DIAGASSERT(ts != NULL);
+
+       switch (base) {
+       case TIME_UTC:
+               if (clock_gettime(CLOCK_REALTIME, ts) != 0)
+                       return 0;
+       }
+
+       return base;
+}



Home | Main Index | Thread Index | Old Index