backport to buster
[hcoop/debian/openafs.git] / src / opr / opr_time.h
CommitLineData
805e021f
CE
1/*
2 * Copyright (c) 2012 Your File System Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/* This header provides routines for dealing with the 100ns based AFS
26 * time type. We hide the actual variable behind a structure, so that
27 * attempts to do
28 *
29 * time_t ourTime;
30 * opr_time theirTime;
31 *
32 * ourTime = theirTime;
33 *
34 * will be caught by the compiler.
35 */
36
37#ifndef OPENAFS_OPR_TIME_H
38#define OPENAFS_OPR_TIME_H
39
40struct opr_time {
41 afs_int64 time;
42};
43
44#define OPR_TIME_INIT {0LL}
45
46static_inline void
47opr_time_FromSecs(struct opr_time *out, time_t in)
48{
49 out->time = ((afs_int64)in) * 10000000;
50}
51
52static_inline time_t
53opr_time_ToSecs(struct opr_time *in)
54{
55 return in->time/10000000;;
56}
57
58static_inline void
59opr_time_FromMsecs(struct opr_time *out, int msecs)
60{
61 out->time = ((afs_int64)msecs) * 10000;
62}
63
64static_inline int
65opr_time_ToMsecs(struct opr_time *in)
66{
67 return in->time/10000;
68}
69
70static_inline void
71opr_time_FromTimeval(struct opr_time *out, struct timeval *in)
72{
73 out->time = ((afs_int64)in->tv_sec) * 10000000 + in->tv_usec * 10;
74}
75
76static_inline void
77opr_time_ToTimeval(struct opr_time *in, struct timeval *out)
78{
79 out->tv_sec = in->time / 10000000;
80 out->tv_usec = (in->time / 10) % 1000000;
81}
82
83static_inline int
84opr_time_Now(struct opr_time *out)
85{
86 struct timeval tv;
87 int code;
88
89 code = gettimeofday(&tv, NULL);
90 if (code == 0)
91 opr_time_FromTimeval(out, &tv);
92
93 return code;
94}
95
96static_inline int
97opr_time_GreaterThan(struct opr_time *t1, struct opr_time *t2)
98{
99 return t1->time > t2->time;
100}
101
102static_inline int
103opr_time_LessThan(struct opr_time *t1, struct opr_time *t2)
104{
105 return t1->time < t2->time;
106}
107
108static_inline void
109opr_time_Add(struct opr_time *t1, struct opr_time *t2)
110{
111 t1->time += t2->time;
112}
113
114static_inline void
115opr_time_Sub(struct opr_time *t1, struct opr_time *t2)
116{
117 t1->time -= t2->time;
118}
119
120static_inline void
121opr_time_AddMsec(struct opr_time *t1, int msec)
122{
123 struct opr_time t2;
124
125 opr_time_FromMsecs(&t2, msec);
126 opr_time_Add(t1, &t2);
127}
128
129#endif