timeutil.h
Go to the documentation of this file.
1// -*- C++ -*-
2// Copyright (c) 2005-2009, Isode Limited, London, England.
3// All rights reserved.
4//
5// Acquisition and use of this software and related materials for any
6// purpose requires a written licence agreement from Isode Limited,
7// or a written licence from an organisation licenced by Isode Limited
8// to grant such a licence.
9
10
11//
12//
16//
17// @VERSION@
18
19#ifndef _TIMEUTIL_H_
20#define _TIMEUTIL_H_
21
22#include <limits.h>
23#include <isode/base/compat.h>
24
27inline bool timespec_cmp (const struct timespec *time1,
28 const struct timespec *time2) {
29 if ( time1->tv_sec < time2->tv_sec ) return true;
30
31 if ( time1->tv_sec > time2->tv_sec ) return false;
32
33 return time1->tv_nsec < time2->tv_nsec;
34}
35
41
42inline unsigned long timespec_diff (const struct timespec *time1,
43 const struct timespec *time2) {
44 if ( time1->tv_sec < time2->tv_sec ||
45 (time1->tv_sec == time2->tv_sec && time1->tv_nsec <= time2->tv_nsec))
46 return 0;
47
48 unsigned long diff = time1->tv_sec - time2->tv_sec;
49
50 if ( diff > (ULONG_MAX/1000000)-1 ) return ULONG_MAX;
51
52 diff *= 1000000;
53
54 diff += (time1->tv_nsec - time2->tv_nsec)/1000;
55
56 return diff;
57}
58
60inline void getNow (struct timespec &now) {
61 struct timeval nows;
62
63 ICgetTOD (&nows);
64
65 now.tv_sec = nows.tv_sec;
66 now.tv_nsec = 1000*nows.tv_usec;
67}
68
70inline void addMillisecs (struct timespec &now, unsigned millisecs)
71{
72 now.tv_nsec += 1000*1000*(millisecs % 1000);
73 while ( now.tv_nsec >= 1000*1000*1000 ) {
74 now.tv_nsec -= 1000*1000*1000;
75 now.tv_sec += 1;
76 }
77 now.tv_sec += millisecs / 1000;
78}
79
82 private:
83 struct timespec time;
84 public:
85 Timecompare (struct timespec t) : time(t) {}
86
87 bool preceeds (struct timespec t1) {
88 return timespec_cmp (&time, &t1);
89 }
90};
91
92#endif /* _TIMEUTIL_H_ */
Comparator class for times.
Definition timeutil.h:81
bool timespec_cmp(const struct timespec *time1, const struct timespec *time2)
Function for comparing two times.
Definition timeutil.h:27
void addMillisecs(struct timespec &now, unsigned millisecs)
Add millisecs to a time.
Definition timeutil.h:70
void getNow(struct timespec &now)
Return the current time.
Definition timeutil.h:60
unsigned long timespec_diff(const struct timespec *time1, const struct timespec *time2)
Function for getting the difference between two times.
Definition timeutil.h:42

All rights reserved © 2002 - 2024 Isode Ltd.