xref: /dragonfly/lib/libc/stdtime/timespec_get.c (revision 6219de94)
1*6219de94STomasz Konojacki /*
2*6219de94STomasz Konojacki  * Copyright (c) 2016 The DragonFly Project. All rights reserved.
3*6219de94STomasz Konojacki  *
4*6219de94STomasz Konojacki  * This code is derived from software contributed to The DragonFly Project
5*6219de94STomasz Konojacki  * by Tomasz Konojacki <me@xenu.pl>.
6*6219de94STomasz Konojacki  *
7*6219de94STomasz Konojacki  * Redistribution and use in source and binary forms, with or without
8*6219de94STomasz Konojacki  * modification, are permitted provided that the following conditions
9*6219de94STomasz Konojacki  * are met:
10*6219de94STomasz Konojacki  *
11*6219de94STomasz Konojacki  * 1. Redistributions of source code must retain the above copyright
12*6219de94STomasz Konojacki  *    notice, this list of conditions and the following disclaimer.
13*6219de94STomasz Konojacki  * 2. Redistributions in binary form must reproduce the above copyright
14*6219de94STomasz Konojacki  *    notice, this list of conditions and the following disclaimer in
15*6219de94STomasz Konojacki  *    the documentation and/or other materials provided with the
16*6219de94STomasz Konojacki  *    distribution.
17*6219de94STomasz Konojacki  * 3. Neither the name of The DragonFly Project nor the names of its
18*6219de94STomasz Konojacki  *    contributors may be used to endorse or promote products derived
19*6219de94STomasz Konojacki  *    from this software without specific, prior written permission.
20*6219de94STomasz Konojacki  *
21*6219de94STomasz Konojacki  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*6219de94STomasz Konojacki  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*6219de94STomasz Konojacki  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*6219de94STomasz Konojacki  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*6219de94STomasz Konojacki  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*6219de94STomasz Konojacki  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*6219de94STomasz Konojacki  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*6219de94STomasz Konojacki  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*6219de94STomasz Konojacki  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*6219de94STomasz Konojacki  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*6219de94STomasz Konojacki  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*6219de94STomasz Konojacki  * SUCH DAMAGE.
33*6219de94STomasz Konojacki  */
34*6219de94STomasz Konojacki 
35*6219de94STomasz Konojacki #include <time.h>
36*6219de94STomasz Konojacki #include <errno.h>
37*6219de94STomasz Konojacki 
38*6219de94STomasz Konojacki int
timespec_get(struct timespec * ts,int base)39*6219de94STomasz Konojacki timespec_get(struct timespec *ts, int base)
40*6219de94STomasz Konojacki {
41*6219de94STomasz Konojacki 	switch (base) {
42*6219de94STomasz Konojacki 	case TIME_UTC:
43*6219de94STomasz Konojacki 		if (clock_gettime(CLOCK_REALTIME, ts))
44*6219de94STomasz Konojacki 			return 0;
45*6219de94STomasz Konojacki 		break;
46*6219de94STomasz Konojacki 	default:
47*6219de94STomasz Konojacki 		errno = EINVAL;
48*6219de94STomasz Konojacki 		return 0;
49*6219de94STomasz Konojacki 	}
50*6219de94STomasz Konojacki 
51*6219de94STomasz Konojacki 	return base;
52*6219de94STomasz Konojacki }
53