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