Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Time calculation functions. |
| 3 | * |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 16 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
| 19 | struct timeval now; /* the current date at any moment */ |
| 20 | struct timeval start_date; /* the process's start date */ |
| 21 | |
| 22 | /* |
| 23 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 24 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 25 | REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 27 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 28 | tv->tv_sec = from->tv_sec + (ms / 1000); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | while (tv->tv_usec >= 1000000) { |
| 30 | tv->tv_usec -= 1000000; |
| 31 | tv->tv_sec++; |
| 32 | } |
| 33 | return tv; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 38 | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 40 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 42 | return __tv_ms_cmp(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 47 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 49 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 51 | return __tv_ms_cmp2(tv1, tv2); |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 56 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 57 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 58 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 59 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 60 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 61 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 62 | return __tv_ms_le2(tv1, tv2); |
| 63 | } |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 65 | /* |
| 66 | * returns the remaining time between tv1=now and event=tv2 |
| 67 | * if tv2 is passed, 0 is returned. |
| 68 | * Must not be used when either argument is eternity. |
| 69 | */ |
| 70 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
| 71 | { |
| 72 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * returns the remaining time between tv1=now and event=tv2 |
| 77 | * if tv2 is passed, 0 is returned. |
| 78 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 79 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 80 | REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | if (tv_iseternity(tv2)) |
| 83 | return TIME_ETERNITY; |
| 84 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 85 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 89 | * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | * Must not be used when either argument is eternity. |
| 91 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 92 | REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 94 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 98 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 99 | */ |
| 100 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 101 | { |
| 102 | return __tv_add(tv, from, inc); |
| 103 | } |
| 104 | |
| 105 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 106 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 107 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 108 | */ |
| 109 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 110 | { |
| 111 | return __tv_add_ifset(tv, from, inc); |
| 112 | } |
| 113 | |
| 114 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 115 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 116 | * 0 is returned. The result is stored into tv. |
| 117 | */ |
| 118 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 119 | { |
| 120 | return __tv_remain(tv1, tv2, tv); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 125 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 126 | * eternity. |
| 127 | */ |
| 128 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 129 | { |
| 130 | return __tv_remain2(tv1, tv2, tv); |
| 131 | } |
| 132 | |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 133 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
| 134 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 135 | { |
| 136 | return __tv_isle(tv1, tv2); |
| 137 | } |
| 138 | |
| 139 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
| 140 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 141 | { |
| 142 | return __tv_isgt(tv1, tv2); |
| 143 | } |
| 144 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 145 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | * Local variables: |
| 147 | * c-indent-level: 8 |
| 148 | * c-basic-offset: 8 |
| 149 | * End: |
| 150 | */ |