4 * Copyright (C) 2006 Rackable Systems All rights reserved.
6 * This file is part of LVM2.
8 * This copyrighted material is made available to anyone wishing to use,
9 * modify, copy, or redistribute it subject to the terms and conditions
10 * of the GNU Lesser General Public License v.2.1.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Abstract out the time methods used so they can be adjusted later -
19 * the results of these routines should stay in-core. This implementation
26 #include "timestamp.h"
29 * The realtime section uses clock_gettime with the CLOCK_MONOTONIC
30 * parameter to prevent issues with time warps
35 #include <bits/time.h>
41 struct timestamp
*get_timestamp(void)
43 struct timestamp
*ts
= NULL
;
45 if (!(ts
= dm_malloc(sizeof(*ts
))))
48 if (clock_gettime(CLOCK_MONOTONIC
, &ts
->t
)) {
49 log_sys_error("clock_gettime", "get_timestamp");
56 /* cmp_timestamp: Compare two timestamps
58 * Return: -1 if t1 is less than t2
59 * 0 if t1 is equal to t2
60 * 1 if t1 is greater than t2
62 int cmp_timestamp(struct timestamp
*t1
, struct timestamp
*t2
)
64 if(t1
->t
.tv_sec
< t2
->t
.tv_sec
)
66 if(t1
->t
.tv_sec
> t2
->t
.tv_sec
)
69 if(t1
->t
.tv_nsec
< t2
->t
.tv_nsec
)
71 if(t1
->t
.tv_nsec
> t2
->t
.tv_nsec
)
77 #else /* ! HAVE_REALTIME */
80 * The !realtime section just uses gettimeofday and is therefore subject
81 * to ntp-type time warps - not sure if should allow that.
90 struct timestamp
*get_timestamp(void)
92 struct timestamp
*ts
= NULL
;
94 if (!(ts
= dm_malloc(sizeof(*ts
))))
97 if (gettimeofday(&ts
->t
, NULL
)) {
98 log_sys_error("gettimeofday", "get_timestamp");
105 /* cmp_timestamp: Compare two timestamps
107 * Return: -1 if t1 is less than t2
108 * 0 if t1 is equal to t2
109 * 1 if t1 is greater than t2
111 int cmp_timestamp(struct timestamp
*t1
, struct timestamp
*t2
)
113 if(t1
->t
.tv_sec
< t2
->t
.tv_sec
)
115 if(t1
->t
.tv_sec
> t2
->t
.tv_sec
)
118 if(t1
->t
.tv_usec
< t2
->t
.tv_usec
)
120 if(t1
->t
.tv_usec
> t2
->t
.tv_usec
)
126 #endif /* HAVE_REALTIME */
128 void destroy_timestamp(struct timestamp
*t
)