1 /* @(#)time.h 1.20 13/10/01 Copyright 1996-2013 J. Schilling */
3 * Generic header for users of time(), gettimeofday() ...
5 * It includes definitions for time_t, struct timeval, ...
7 * Copyright (c) 1996-2013 J. Schilling
10 * The contents of this file are subject to the terms of the
11 * Common Development and Distribution License, Version 1.0 only
12 * (the "License"). You may not use this file except in compliance
15 * See the file CDDL.Schily.txt in this distribution for details.
16 * A copy of the CDDL is also available via the Internet at
17 * http://www.opensource.org/licenses/cddl1.txt
19 * When distributing Covered Code, include this CDDL HEADER in each
20 * file and include the License file CDDL.Schily.txt from this distribution.
23 #ifndef _SCHILY_TIME_H
24 #define _SCHILY_TIME_H
26 #ifndef _SCHILY_MCONFIG_H
27 #include <schily/mconfig.h>
30 #ifndef _SCHILY_TYPES_H
31 #include <schily/types.h> /* Needed for time_t */
34 #ifdef TIME_WITH_SYS_TIME
35 # ifndef _INCL_SYS_TIME_H
36 # include <sys/time.h>
37 # define _INCL_SYS_TIME_H
44 #ifdef HAVE_SYS_TIME_H
45 # ifndef _INCL_SYS_TIME_H
46 # include <sys/time.h>
47 # define _INCL_SYS_TIME_H
63 * With MSVC timerclear / struct timeval present in case that
64 * winsock2.h has been included before.
66 #undef HAVE_STRUCT_TIMEVAL
67 #define HAVE_STRUCT_TIMEVAL 1
70 #ifndef HAVE_STRUCT_TIMEVAL
78 #ifndef HAVE_STRUCT_TIMEZONE
86 #ifndef HAVE_STRUCT_TIMESPEC
96 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
99 #define timerfix1(tvp) while ((tvp)->tv_usec < 0) { \
101 (tvp)->tv_usec += 1000000; \
104 #define timerfix2(tvp) while ((tvp)->tv_usec > 1000000) { \
106 (tvp)->tv_usec -= 1000000; \
109 #define timerfix(tvp) do { timerfix1(tvp); timerfix2(tvp); } while (0)
112 * timersub() and timeradd() are defined on FreeBSD with a different
113 * interface (3 parameters).
116 #define timersub(tvp1, tvp2) do { \
117 (tvp1)->tv_sec -= (tvp2)->tv_sec; \
118 (tvp1)->tv_usec -= (tvp2)->tv_usec; \
119 timerfix1(tvp1); timerfix2(tvp1); \
123 #define timeradd(tvp1, tvp2) do { \
124 (tvp1)->tv_sec += (tvp2)->tv_sec; \
125 (tvp1)->tv_usec += (tvp2)->tv_usec; \
126 timerfix1(tvp1); timerfix2(tvp1); \
131 #define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
134 #define timespecfix1(tsp) while ((tsp)->tv_nsec < 0) { \
136 (tsp)->tv_nsec += 1000000000; \
139 #define timespecfix2(tsp) while ((tsp)->tv_nsec > 1000000000) { \
141 (tsp)->tv_nsec -= 1000000000; \
144 #define timespecfix(tsp) do { timespecfix1(tsp); timespecfix2(tsp); } while (0)
147 #define timespecsub(tsp1, tsp2) do { \
148 (tsp1)->tv_sec -= (tsp2)->tv_sec; \
149 (tsp1)->tv_nsec -= (tsp2)->tv_nsec; \
150 timespecfix1(tsp1); timespecfix2(tsp1); \
154 #define timespecadd(tsp1, tsp2) do { \
155 (tsp1)->tv_sec += (tsp2)->tv_sec; \
156 (tsp1)->tv_nsec += (tsp2)->tv_nsec; \
157 timespecfix1(tsp1); timespecfix2(tsp1); \
164 #endif /* _SCHILY_TIME_H */