| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | time utility functions | 
|---|
| 4 |  | 
|---|
| 5 | This program is free software; you can redistribute it and/or modify | 
|---|
| 6 | it under the terms of the GNU General Public License as published by | 
|---|
| 7 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 8 | (at your option) any later version. | 
|---|
| 9 |  | 
|---|
| 10 | This program is distributed in the hope that it will be useful, | 
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 13 | GNU General Public License for more details. | 
|---|
| 14 |  | 
|---|
| 15 | You should have received a copy of the GNU General Public License | 
|---|
| 16 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 | #ifndef _SAMBA_TIME_H_ | 
|---|
| 20 | #define _SAMBA_TIME_H_ | 
|---|
| 21 |  | 
|---|
| 22 | #ifndef _PUBLIC_ | 
|---|
| 23 | #define _PUBLIC_ | 
|---|
| 24 | #endif | 
|---|
| 25 |  | 
|---|
| 26 | #ifndef TIME_T_MIN | 
|---|
| 27 | /* we use 0 here, because (time_t)-1 means error */ | 
|---|
| 28 | #define TIME_T_MIN 0 | 
|---|
| 29 | #endif | 
|---|
| 30 |  | 
|---|
| 31 | /* | 
|---|
| 32 | * we use the INT32_MAX here as on 64 bit systems, | 
|---|
| 33 | * gmtime() fails with INT64_MAX | 
|---|
| 34 | */ | 
|---|
| 35 | #ifndef TIME_T_MAX | 
|---|
| 36 | #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t)) | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | /* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */ | 
|---|
| 41 | typedef uint64_t NTTIME; | 
|---|
| 42 |  | 
|---|
| 43 | /** | 
|---|
| 44 | External access to time_t_min and time_t_max. | 
|---|
| 45 | **/ | 
|---|
| 46 | _PUBLIC_ time_t get_time_t_max(void); | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 | a gettimeofday wrapper | 
|---|
| 50 | **/ | 
|---|
| 51 | _PUBLIC_ void GetTimeOfDay(struct timeval *tval); | 
|---|
| 52 |  | 
|---|
| 53 | /** | 
|---|
| 54 | interpret an 8 byte "filetime" structure to a time_t | 
|---|
| 55 | It's originally in "100ns units since jan 1st 1601" | 
|---|
| 56 | **/ | 
|---|
| 57 | _PUBLIC_ time_t nt_time_to_unix(NTTIME nt); | 
|---|
| 58 |  | 
|---|
| 59 | /** | 
|---|
| 60 | put a 8 byte filetime from a time_t | 
|---|
| 61 | This takes GMT as input | 
|---|
| 62 | **/ | 
|---|
| 63 | _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t); | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | check if it's a null unix time | 
|---|
| 67 | **/ | 
|---|
| 68 | _PUBLIC_ bool null_time(time_t t); | 
|---|
| 69 |  | 
|---|
| 70 | /** | 
|---|
| 71 | check if it's a null NTTIME | 
|---|
| 72 | **/ | 
|---|
| 73 | _PUBLIC_ bool null_nttime(NTTIME t); | 
|---|
| 74 |  | 
|---|
| 75 | /** | 
|---|
| 76 | put a dos date into a buffer (time/date format) | 
|---|
| 77 | This takes GMT time and puts local time in the buffer | 
|---|
| 78 | **/ | 
|---|
| 79 | _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset); | 
|---|
| 80 |  | 
|---|
| 81 | /** | 
|---|
| 82 | put a dos date into a buffer (date/time format) | 
|---|
| 83 | This takes GMT time and puts local time in the buffer | 
|---|
| 84 | **/ | 
|---|
| 85 | _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset); | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 | put a dos 32 bit "unix like" date into a buffer. This routine takes | 
|---|
| 89 | GMT and converts it to LOCAL time before putting it (most SMBs assume | 
|---|
| 90 | localtime for this sort of date) | 
|---|
| 91 | **/ | 
|---|
| 92 | _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset); | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 | create a unix date (int GMT) from a dos date (which is actually in | 
|---|
| 96 | localtime) | 
|---|
| 97 | **/ | 
|---|
| 98 | _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset); | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | like make_unix_date() but the words are reversed | 
|---|
| 102 | **/ | 
|---|
| 103 | _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset); | 
|---|
| 104 |  | 
|---|
| 105 | /** | 
|---|
| 106 | create a unix GMT date from a dos date in 32 bit "unix like" format | 
|---|
| 107 | these generally arrive as localtimes, with corresponding DST | 
|---|
| 108 | **/ | 
|---|
| 109 | _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset); | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 | return a HTTP/1.0 time string | 
|---|
| 113 | **/ | 
|---|
| 114 | _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t); | 
|---|
| 115 |  | 
|---|
| 116 | /** | 
|---|
| 117 | Return the date and time as a string | 
|---|
| 118 | **/ | 
|---|
| 119 | _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t); | 
|---|
| 120 |  | 
|---|
| 121 | /** | 
|---|
| 122 | return a talloced string representing a NTTIME for human consumption | 
|---|
| 123 | */ | 
|---|
| 124 | _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt); | 
|---|
| 125 |  | 
|---|
| 126 | /** | 
|---|
| 127 | put a NTTIME into a packet | 
|---|
| 128 | */ | 
|---|
| 129 | _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t); | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 | pull a NTTIME from a packet | 
|---|
| 133 | */ | 
|---|
| 134 | _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset); | 
|---|
| 135 |  | 
|---|
| 136 | /** | 
|---|
| 137 | parse a nttime as a large integer in a string and return a NTTIME | 
|---|
| 138 | */ | 
|---|
| 139 | _PUBLIC_ NTTIME nttime_from_string(const char *s); | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | return (tv1 - tv2) in microseconds | 
|---|
| 143 | */ | 
|---|
| 144 | _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2); | 
|---|
| 145 |  | 
|---|
| 146 | /** | 
|---|
| 147 | return a zero timeval | 
|---|
| 148 | */ | 
|---|
| 149 | _PUBLIC_ struct timeval timeval_zero(void); | 
|---|
| 150 |  | 
|---|
| 151 | /** | 
|---|
| 152 | return true if a timeval is zero | 
|---|
| 153 | */ | 
|---|
| 154 | _PUBLIC_ bool timeval_is_zero(const struct timeval *tv); | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 | return a timeval for the current time | 
|---|
| 158 | */ | 
|---|
| 159 | _PUBLIC_ struct timeval timeval_current(void); | 
|---|
| 160 |  | 
|---|
| 161 | /** | 
|---|
| 162 | return a timeval struct with the given elements | 
|---|
| 163 | */ | 
|---|
| 164 | _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs); | 
|---|
| 165 |  | 
|---|
| 166 | /** | 
|---|
| 167 | return a timeval ofs microseconds after tv | 
|---|
| 168 | */ | 
|---|
| 169 | _PUBLIC_ struct timeval timeval_add(const struct timeval *tv, | 
|---|
| 170 | uint32_t secs, uint32_t usecs); | 
|---|
| 171 |  | 
|---|
| 172 | /** | 
|---|
| 173 | return the sum of two timeval structures | 
|---|
| 174 | */ | 
|---|
| 175 | struct timeval timeval_sum(const struct timeval *tv1, | 
|---|
| 176 | const struct timeval *tv2); | 
|---|
| 177 |  | 
|---|
| 178 | /** | 
|---|
| 179 | return a timeval secs/usecs into the future | 
|---|
| 180 | */ | 
|---|
| 181 | _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs); | 
|---|
| 182 |  | 
|---|
| 183 | /** | 
|---|
| 184 | compare two timeval structures. | 
|---|
| 185 | Return -1 if tv1 < tv2 | 
|---|
| 186 | Return 0 if tv1 == tv2 | 
|---|
| 187 | Return 1 if tv1 > tv2 | 
|---|
| 188 | */ | 
|---|
| 189 | _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2); | 
|---|
| 190 |  | 
|---|
| 191 | /** | 
|---|
| 192 | return true if a timer is in the past | 
|---|
| 193 | */ | 
|---|
| 194 | _PUBLIC_ bool timeval_expired(const struct timeval *tv); | 
|---|
| 195 |  | 
|---|
| 196 | /** | 
|---|
| 197 | return the number of seconds elapsed between two times | 
|---|
| 198 | */ | 
|---|
| 199 | _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2); | 
|---|
| 200 |  | 
|---|
| 201 | /** | 
|---|
| 202 | return the number of seconds elapsed since a given time | 
|---|
| 203 | */ | 
|---|
| 204 | _PUBLIC_ double timeval_elapsed(const struct timeval *tv); | 
|---|
| 205 |  | 
|---|
| 206 | /** | 
|---|
| 207 | return the lesser of two timevals | 
|---|
| 208 | */ | 
|---|
| 209 | _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1, | 
|---|
| 210 | const struct timeval *tv2); | 
|---|
| 211 |  | 
|---|
| 212 | /** | 
|---|
| 213 | return the greater of two timevals | 
|---|
| 214 | */ | 
|---|
| 215 | _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1, | 
|---|
| 216 | const struct timeval *tv2); | 
|---|
| 217 |  | 
|---|
| 218 | /** | 
|---|
| 219 | return the difference between two timevals as a timeval | 
|---|
| 220 | if tv1 comes after tv2, then return a zero timeval | 
|---|
| 221 | (this is *tv2 - *tv1) | 
|---|
| 222 | */ | 
|---|
| 223 | _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1, | 
|---|
| 224 | const struct timeval *tv2); | 
|---|
| 225 |  | 
|---|
| 226 | /** | 
|---|
| 227 | convert a timeval to a NTTIME | 
|---|
| 228 | */ | 
|---|
| 229 | _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv); | 
|---|
| 230 |  | 
|---|
| 231 | /** | 
|---|
| 232 | convert a NTTIME to a timeval | 
|---|
| 233 | */ | 
|---|
| 234 | _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t); | 
|---|
| 235 |  | 
|---|
| 236 | /** | 
|---|
| 237 | return the UTC offset in seconds west of UTC, or 0 if it cannot be determined | 
|---|
| 238 | */ | 
|---|
| 239 | _PUBLIC_ int get_time_zone(time_t t); | 
|---|
| 240 |  | 
|---|
| 241 | /** | 
|---|
| 242 | check if 2 NTTIMEs are equal. | 
|---|
| 243 | */ | 
|---|
| 244 | bool nt_time_equal(NTTIME *t1, NTTIME *t2); | 
|---|
| 245 |  | 
|---|
| 246 | void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second); | 
|---|
| 247 |  | 
|---|
| 248 | struct timespec nt_time_to_unix_timespec(NTTIME *nt); | 
|---|
| 249 |  | 
|---|
| 250 | time_t convert_timespec_to_time_t(struct timespec ts); | 
|---|
| 251 |  | 
|---|
| 252 | struct timespec convert_time_t_to_timespec(time_t t); | 
|---|
| 253 |  | 
|---|
| 254 | bool null_timespec(struct timespec ts); | 
|---|
| 255 |  | 
|---|
| 256 | /** Extra minutes to add to the normal GMT to local time conversion. */ | 
|---|
| 257 | extern int extra_time_offset; | 
|---|
| 258 |  | 
|---|
| 259 | #endif /* _SAMBA_TIME_H_ */ | 
|---|