1#ifndef _LINUX_TIME_H
2#define _LINUX_TIME_H
3
4#include <asm/param.h>
5#include <linux/types.h>
6
7#ifndef _STRUCT_TIMESPEC
8#define _STRUCT_TIMESPEC
9struct timespec {
10	time_t	tv_sec;		/* seconds */
11	long	tv_nsec;	/* nanoseconds */
12};
13#endif /* _STRUCT_TIMESPEC */
14
15#ifdef __KERNEL__
16
17/*
18 * Change timeval to jiffies, trying to avoid the
19 * most obvious overflows..
20 *
21 * And some not so obvious.
22 *
23 * Note that we don't want to return MAX_LONG, because
24 * for various timeout reasons we often end up having
25 * to wait "jiffies+1" in order to guarantee that we wait
26 * at _least_ "jiffies" - so "jiffies+1" had better still
27 * be positive.
28 */
29#define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
30
31static __inline__ unsigned long
32timespec_to_jiffies(struct timespec *value)
33{
34	unsigned long sec = value->tv_sec;
35	long nsec = value->tv_nsec;
36
37	if (sec >= (MAX_JIFFY_OFFSET / HZ))
38		return MAX_JIFFY_OFFSET;
39	nsec += 1000000000L / HZ - 1;
40	nsec /= 1000000000L / HZ;
41	return HZ * sec + nsec;
42}
43
44static __inline__ void
45jiffies_to_timespec(unsigned long jiffies, struct timespec *value)
46{
47	value->tv_nsec = (jiffies % HZ) * (1000000000L / HZ);
48	value->tv_sec = jiffies / HZ;
49}
50
51
52/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
53 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
54 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
55 *
56 * [For the Julian calendar (which was used in Russia before 1917,
57 * Britain & colonies before 1752, anywhere else before 1582,
58 * and is still in use by some communities) leave out the
59 * -year/100+year/400 terms, and add 10.]
60 *
61 * This algorithm was first published by Gauss (I think).
62 *
63 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
64 * machines were long is 32-bit! (However, as time_t is signed, we
65 * will already get problems at other places on 2038-01-19 03:14:08)
66 */
67static inline unsigned long
68mktime (unsigned int year, unsigned int mon,
69	unsigned int day, unsigned int hour,
70	unsigned int min, unsigned int sec)
71{
72	if (0 >= (int) (mon -= 2)) {	/* 1..12 -> 11,12,1..10 */
73		mon += 12;		/* Puts Feb last since it has leap day */
74		year -= 1;
75	}
76
77	return (((
78		(unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
79			year*365 - 719499
80	    )*24 + hour /* now have hours */
81	  )*60 + min /* now have minutes */
82	)*60 + sec; /* finally seconds */
83}
84
85#endif /* __KERNEL__ */
86
87
88struct timeval {
89	time_t		tv_sec;		/* seconds */
90	suseconds_t	tv_usec;	/* microseconds */
91};
92
93struct timezone {
94	int	tz_minuteswest;	/* minutes west of Greenwich */
95	int	tz_dsttime;	/* type of dst correction */
96};
97
98#define NFDBITS			__NFDBITS
99
100#ifdef __KERNEL__
101extern void do_gettimeofday(struct timeval *tv);
102extern void do_settimeofday(struct timeval *tv);
103#endif
104
105#define FD_SETSIZE		__FD_SETSIZE
106#define FD_SET(fd,fdsetp)	__FD_SET(fd,fdsetp)
107#define FD_CLR(fd,fdsetp)	__FD_CLR(fd,fdsetp)
108#define FD_ISSET(fd,fdsetp)	__FD_ISSET(fd,fdsetp)
109#define FD_ZERO(fdsetp)		__FD_ZERO(fdsetp)
110
111/*
112 * Names of the interval timers, and structure
113 * defining a timer setting.
114 */
115#define	ITIMER_REAL	0
116#define	ITIMER_VIRTUAL	1
117#define	ITIMER_PROF	2
118
119struct  itimerspec {
120        struct  timespec it_interval;    /* timer period */
121        struct  timespec it_value;       /* timer expiration */
122};
123
124struct	itimerval {
125	struct	timeval it_interval;	/* timer interval */
126	struct	timeval it_value;	/* current value */
127};
128
129#endif
130