timeutil.h File Reference

Various utility functions for handling times. More...

#include <limits.h>
#include <isode/base/compat.h>

Go to the source code of this file.

Data Structures

class  Timecompare
 Comparator class for times. More...
 

Functions

bool timespec_cmp (const struct timespec *time1, const struct timespec *time2)
 Function for comparing two times.
 
unsigned long timespec_diff (const struct timespec *time1, const struct timespec *time2)
 Function for getting the difference between two times.
 
void getNow (struct timespec &now)
 Return the current time.
 
void addMillisecs (struct timespec &now, unsigned millisecs)
 Add millisecs to a time.
 

Detailed Description

Various utility functions for handling times.

Definition in file timeutil.h.

Function Documentation

◆ timespec_cmp()

bool timespec_cmp ( const struct timespec *  time1,
const struct timespec *  time2 
)
inline

Function for comparing two times.

Returns
true if time1 preceeds time2

Definition at line 27 of file timeutil.h.

28 {
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}

◆ timespec_diff()

unsigned long timespec_diff ( const struct timespec *  time1,
const struct timespec *  time2 
)
inline

Function for getting the difference between two times.

If time1 follows time2, then it returns the difference in microseconds capped at about ULONG_MAX microseconds (71 minutes for 32 bit ulong). If time1 preceeds time2, then the result is zero.

Definition at line 42 of file timeutil.h.

43 {
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}

Referenced by Poll::Poll_select::Deliver().

◆ getNow()

void getNow ( struct timespec &  now)
inline

Return the current time.

Definition at line 60 of file timeutil.h.

60 {
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}

Referenced by Poll::Poll_select::Deliver().

◆ addMillisecs()

void addMillisecs ( struct timespec &  now,
unsigned  millisecs 
)
inline

Add millisecs to a time.

Definition at line 70 of file timeutil.h.

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}

All rights reserved © 2002 - 2024 Isode Ltd.