1 1.3 nia /* $NetBSD: timespec_get.c,v 1.3 2025/04/21 08:57:32 nia Exp $ */ 2 1.1 kamil 3 1.1 kamil /*- 4 1.1 kamil * Copyright (c) 2016 The NetBSD Foundation, Inc. 5 1.1 kamil * All rights reserved. 6 1.1 kamil * 7 1.1 kamil * This code is derived from software contributed to The NetBSD Foundation 8 1.1 kamil * by Kamil Rytarowski. 9 1.1 kamil * 10 1.1 kamil * Redistribution and use in source and binary forms, with or without 11 1.1 kamil * modification, are permitted provided that the following conditions 12 1.1 kamil * are met: 13 1.1 kamil * 1. Redistributions of source code must retain the above copyright 14 1.1 kamil * notice, this list of conditions and the following disclaimer. 15 1.1 kamil * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 kamil * notice, this list of conditions and the following disclaimer in the 17 1.1 kamil * documentation and/or other materials provided with the distribution. 18 1.1 kamil * 19 1.1 kamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 kamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 kamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 kamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 kamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 kamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 kamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 kamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 kamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 kamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 kamil * POSSIBILITY OF SUCH DAMAGE. 30 1.1 kamil */ 31 1.1 kamil 32 1.1 kamil #include <sys/cdefs.h> 33 1.1 kamil #ifndef lint 34 1.3 nia __RCSID("$NetBSD: timespec_get.c,v 1.3 2025/04/21 08:57:32 nia Exp $"); 35 1.1 kamil #endif /* !defined lint */ 36 1.1 kamil 37 1.1 kamil #include <assert.h> 38 1.1 kamil #include <time.h> 39 1.1 kamil 40 1.1 kamil /* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */ 41 1.1 kamil 42 1.1 kamil int 43 1.1 kamil timespec_get(struct timespec *ts, int base) 44 1.1 kamil { 45 1.1 kamil 46 1.1 kamil _DIAGASSERT(ts != NULL); 47 1.1 kamil 48 1.1 kamil switch (base) { 49 1.1 kamil case TIME_UTC: 50 1.2 christos if (clock_gettime(CLOCK_REALTIME, ts) == -1) 51 1.1 kamil return 0; 52 1.2 christos break; 53 1.3 nia case TIME_MONOTONIC: 54 1.3 nia if (clock_gettime(CLOCK_MONOTONIC, ts) == -1) 55 1.3 nia return 0; 56 1.3 nia break; 57 1.2 christos default: 58 1.2 christos return 0; 59 1.1 kamil } 60 1.1 kamil 61 1.1 kamil return base; 62 1.1 kamil } 63