1 /* gettime -- get the system clock 2 3 Copyright (C) 2002, 2004-2007, 2009-2023 Free Software Foundation, 4 Inc. 5 6 This file is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as 8 published by the Free Software Foundation, either version 3 of the 9 License, or (at your option) any later version. 10 11 This file is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. */ 18 19 /* Written by Paul Eggert. */ 20 21 #include <config.h> 22 23 #include "timespec.h" 24 25 #include <sys/time.h> 26 27 /* Get the system time into *TS. */ 28 29 void 30 gettime (struct timespec *ts) 31 { 32 #if defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME 33 clock_gettime (CLOCK_REALTIME, ts); 34 #elif defined HAVE_TIMESPEC_GET 35 timespec_get (ts, TIME_UTC); 36 #else 37 struct timeval tv; 38 gettimeofday (&tv, NULL); 39 ts->tv_sec = tv.tv_sec; 40 ts->tv_nsec = tv.tv_usec * 1000; 41 #endif 42 } 43 44 /* Return the current system time as a struct timespec. */ 45 46 struct timespec 47 current_timespec (void) 48 { 49 struct timespec ts; 50 gettime (&ts); 51 return ts; 52 }