xref: /freebsd-src/lib/libc/gen/timespec_get.c (revision 9b5d724cad10087e34165199e55f15f2df744ed5)
17e299411SWarner Losh /*	$NetBSD: timespec_get.c,v 1.2 2016/10/04 12:48:15 christos Exp $	*/
27e299411SWarner Losh 
37e299411SWarner Losh /*-
47e299411SWarner Losh  * Copyright (c) 2016 The NetBSD Foundation, Inc.
57e299411SWarner Losh  * All rights reserved.
67e299411SWarner Losh  *
77e299411SWarner Losh  * This code is derived from software contributed to The NetBSD Foundation
87e299411SWarner Losh  * by Kamil Rytarowski.
97e299411SWarner Losh  *
107e299411SWarner Losh  * Redistribution and use in source and binary forms, with or without
117e299411SWarner Losh  * modification, are permitted provided that the following conditions
127e299411SWarner Losh  * are met:
137e299411SWarner Losh  * 1. Redistributions of source code must retain the above copyright
147e299411SWarner Losh  *    notice, this list of conditions and the following disclaimer.
157e299411SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
167e299411SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
177e299411SWarner Losh  *    documentation and/or other materials provided with the distribution.
187e299411SWarner Losh  *
197e299411SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
207e299411SWarner Losh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
217e299411SWarner Losh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
227e299411SWarner Losh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
237e299411SWarner Losh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
247e299411SWarner Losh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
257e299411SWarner Losh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
267e299411SWarner Losh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
277e299411SWarner Losh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287e299411SWarner Losh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
297e299411SWarner Losh  * POSSIBILITY OF SUCH DAMAGE.
307e299411SWarner Losh  */
317e299411SWarner Losh 
327e299411SWarner Losh #include <sys/cdefs.h>
337e299411SWarner Losh __RCSID("$NetBSD: timespec_get.c,v 1.2 2016/10/04 12:48:15 christos Exp $");
347e299411SWarner Losh #include <time.h>
357e299411SWarner Losh 
367e299411SWarner Losh /* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
377e299411SWarner Losh 
387e299411SWarner Losh int
timespec_get(struct timespec * ts,int base)397e299411SWarner Losh timespec_get(struct timespec *ts, int base)
407e299411SWarner Losh {
417e299411SWarner Losh 
427e299411SWarner Losh 	switch (base) {
437e299411SWarner Losh 	case TIME_UTC:
447e299411SWarner Losh 		if (clock_gettime(CLOCK_REALTIME, ts) == -1)
457e299411SWarner Losh 			return 0;
467e299411SWarner Losh 		break;
47*9b5d724cSDag-Erling Smørgrav 	case TIME_MONOTONIC:
48*9b5d724cSDag-Erling Smørgrav 		if (clock_gettime(CLOCK_MONOTONIC, ts) == -1)
49*9b5d724cSDag-Erling Smørgrav 			return 0;
50*9b5d724cSDag-Erling Smørgrav 		break;
517e299411SWarner Losh 	default:
527e299411SWarner Losh 		return 0;
537e299411SWarner Losh 	}
547e299411SWarner Losh 
557e299411SWarner Losh 	return base;
567e299411SWarner Losh }
57