Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / lib / misc / timestamp.c
blob64b22893194147104398cc9ca04f0ed9fbab0067
1 /* $NetBSD$ */
3 /*
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
20 * requires librt.
23 #include "lib.h"
24 #include <stdlib.h>
26 #include "timestamp.h"
29 * The realtime section uses clock_gettime with the CLOCK_MONOTONIC
30 * parameter to prevent issues with time warps
32 #ifdef HAVE_REALTIME
34 #include <time.h>
35 #include <bits/time.h>
37 struct timestamp {
38 struct timespec t;
41 struct timestamp *get_timestamp(void)
43 struct timestamp *ts = NULL;
45 if (!(ts = dm_malloc(sizeof(*ts))))
46 return_NULL;
48 if (clock_gettime(CLOCK_MONOTONIC, &ts->t)) {
49 log_sys_error("clock_gettime", "get_timestamp");
50 return NULL;
53 return ts;
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)
65 return -1;
66 if(t1->t.tv_sec > t2->t.tv_sec)
67 return 1;
69 if(t1->t.tv_nsec < t2->t.tv_nsec)
70 return -1;
71 if(t1->t.tv_nsec > t2->t.tv_nsec)
72 return 1;
74 return 0;
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.
84 #include <sys/time.h>
86 struct timestamp {
87 struct timeval t;
90 struct timestamp *get_timestamp(void)
92 struct timestamp *ts = NULL;
94 if (!(ts = dm_malloc(sizeof(*ts))))
95 return_NULL;
97 if (gettimeofday(&ts->t, NULL)) {
98 log_sys_error("gettimeofday", "get_timestamp");
99 return NULL;
102 return ts;
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)
114 return -1;
115 if(t1->t.tv_sec > t2->t.tv_sec)
116 return 1;
118 if(t1->t.tv_usec < t2->t.tv_usec)
119 return -1;
120 if(t1->t.tv_usec > t2->t.tv_usec)
121 return 1;
123 return 0;
126 #endif /* HAVE_REALTIME */
128 void destroy_timestamp(struct timestamp *t)
130 if (t)
131 dm_free(t);