Module Name:    src
Committed By:   kamil
Date:           Tue Oct  4 09:41:41 UTC 2016

Modified Files:
        src/distrib/sets/lists/comp: mi
        src/include: time.h
        src/lib/libc/gen: Makefile.inc
Added Files:
        src/lib/libc/gen: timespec_get.3 timespec_get.c

Log Message:
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>


To generate a diff of this commit:
cvs rdiff -u -r1.2063 -r1.2064 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.46 -r1.47 src/include/time.h
cvs rdiff -u -r1.195 -r1.196 src/lib/libc/gen/Makefile.inc
cvs rdiff -u -r0 -r1.1 src/lib/libc/gen/timespec_get.3 \
    src/lib/libc/gen/timespec_get.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.2063 src/distrib/sets/lists/comp/mi:1.2064
--- src/distrib/sets/lists/comp/mi:1.2063	Mon Oct  3 01:00:27 2016
+++ src/distrib/sets/lists/comp/mi	Tue Oct  4 09:41:40 2016
@@ -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

Index: src/include/time.h
diff -u src/include/time.h:1.46 src/include/time.h:1.47
--- src/include/time.h:1.46	Sat Apr 23 23:10:10 2016
+++ src/include/time.h	Tue Oct  4 09:41:41 2016
@@ -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 @@ struct tm *getdate(const char *);
 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 @@ char *strptime_l(const char * __restrict
 
 #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_ */

Index: src/lib/libc/gen/Makefile.inc
diff -u src/lib/libc/gen/Makefile.inc:1.195 src/lib/libc/gen/Makefile.inc:1.196
--- src/lib/libc/gen/Makefile.inc:1.195	Sun Jun  5 17:16:44 2016
+++ src/lib/libc/gen/Makefile.inc	Tue Oct  4 09:41:41 2016
@@ -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 @@ SRCS+=	alarm.c alphasort.c arc4random.c 
 	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 @@ MAN+=	alarm.3 arc4random.3 basename.3 bs
 	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
 

Added files:

Index: src/lib/libc/gen/timespec_get.3
diff -u /dev/null src/lib/libc/gen/timespec_get.3:1.1
--- /dev/null	Tue Oct  4 09:41:41 2016
+++ src/lib/libc/gen/timespec_get.3	Tue Oct  4 09:41:41 2016
@@ -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 ka...@netbsd.org
Index: src/lib/libc/gen/timespec_get.c
diff -u /dev/null src/lib/libc/gen/timespec_get.c:1.1
--- /dev/null	Tue Oct  4 09:41:41 2016
+++ src/lib/libc/gen/timespec_get.c	Tue Oct  4 09:41:41 2016
@@ -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;
+}

Reply via email to