timestamp.c (1913B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "config.h" 21 22#include "guacamole/timestamp.h" 23 24#include <sys/time.h> 25 26#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_NANOSLEEP) 27#include <time.h> 28#endif 29 30guac_timestamp guac_timestamp_current() { 31 32#ifdef HAVE_CLOCK_GETTIME 33 34 struct timespec current; 35 36 /* Get current time, monotonically increasing */ 37#ifdef CLOCK_MONOTONIC 38 clock_gettime(CLOCK_MONOTONIC, ¤t); 39#else 40 clock_gettime(CLOCK_REALTIME, ¤t); 41#endif 42 43 /* Calculate milliseconds */ 44 return (guac_timestamp) current.tv_sec * 1000 + current.tv_nsec / 1000000; 45 46#else 47 48 struct timeval current; 49 50 /* Get current time */ 51 gettimeofday(¤t, NULL); 52 53 /* Calculate milliseconds */ 54 return (guac_timestamp) current.tv_sec * 1000 + current.tv_usec / 1000; 55 56#endif 57 58} 59 60void guac_timestamp_msleep(int duration) { 61 62 /* Split milliseconds into equivalent seconds + nanoseconds */ 63 struct timespec sleep_period = { 64 .tv_sec = duration / 1000, 65 .tv_nsec = (duration % 1000) * 1000000 66 }; 67 68 /* Sleep for specified interval */ 69 nanosleep(&sleep_period, NULL); 70 71} 72