[480] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | time handling functions
|
---|
| 4 |
|
---|
| 5 | Copyright (C) Andrew Tridgell 1992-2004
|
---|
| 6 | Copyright (C) Stefan (metze) Metzmacher 2002
|
---|
[745] | 7 | Copyright (C) Jeremy Allison 2007
|
---|
| 8 | Copyright (C) Andrew Bartlett 2011
|
---|
[480] | 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify
|
---|
| 11 | it under the terms of the GNU General Public License as published by
|
---|
| 12 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 13 | (at your option) any later version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful,
|
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | GNU General Public License for more details.
|
---|
| 19 |
|
---|
| 20 | You should have received a copy of the GNU General Public License
|
---|
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "includes.h"
|
---|
| 25 | #include "system/time.h"
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * @file
|
---|
| 29 | * @brief time handling functions
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | #if (SIZEOF_LONG == 8)
|
---|
| 33 | #define TIME_FIXUP_CONSTANT_INT 11644473600L
|
---|
| 34 | #elif (SIZEOF_LONG_LONG == 8)
|
---|
| 35 | #define TIME_FIXUP_CONSTANT_INT 11644473600LL
|
---|
| 36 | #endif
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | External access to time_t_min and time_t_max.
|
---|
| 42 | **/
|
---|
| 43 | _PUBLIC_ time_t get_time_t_max(void)
|
---|
| 44 | {
|
---|
| 45 | return TIME_T_MAX;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | a gettimeofday wrapper
|
---|
| 50 | **/
|
---|
| 51 | _PUBLIC_ void GetTimeOfDay(struct timeval *tval)
|
---|
| 52 | {
|
---|
| 53 | #ifdef HAVE_GETTIMEOFDAY_TZ
|
---|
| 54 | gettimeofday(tval,NULL);
|
---|
| 55 | #else
|
---|
| 56 | gettimeofday(tval);
|
---|
| 57 | #endif
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[745] | 60 | /**
|
---|
| 61 | a wrapper to preferably get the monotonic time
|
---|
| 62 | **/
|
---|
| 63 | _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
|
---|
| 64 | {
|
---|
| 65 | if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) != 0) {
|
---|
| 66 | clock_gettime(CLOCK_REALTIME,tp);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
[480] | 69 |
|
---|
[745] | 70 | /**
|
---|
| 71 | a wrapper to preferably get the monotonic time in seconds
|
---|
| 72 | as this is only second resolution we can use the cached
|
---|
| 73 | (and much faster) COARSE clock variant
|
---|
| 74 | **/
|
---|
| 75 | _PUBLIC_ time_t time_mono(time_t *t)
|
---|
| 76 | {
|
---|
| 77 | struct timespec tp;
|
---|
| 78 | int rc = -1;
|
---|
| 79 | #ifdef CLOCK_MONOTONIC_COARSE
|
---|
| 80 | rc = clock_gettime(CLOCK_MONOTONIC_COARSE,&tp);
|
---|
| 81 | #endif
|
---|
| 82 | if (rc != 0) {
|
---|
| 83 | clock_gettime_mono(&tp);
|
---|
| 84 | }
|
---|
| 85 | if (t != NULL) {
|
---|
| 86 | *t = tp.tv_sec;
|
---|
| 87 | }
|
---|
| 88 | return tp.tv_sec;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
[480] | 92 | #define TIME_FIXUP_CONSTANT 11644473600LL
|
---|
| 93 |
|
---|
| 94 | time_t convert_timespec_to_time_t(struct timespec ts)
|
---|
| 95 | {
|
---|
[745] | 96 | /* Ensure tv_nsec is less than 1sec. */
|
---|
| 97 | while (ts.tv_nsec > 1000000000) {
|
---|
| 98 | ts.tv_sec += 1;
|
---|
| 99 | ts.tv_nsec -= 1000000000;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[480] | 102 | /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
|
---|
| 103 | increment if it's greater than 500 millionth of a second. */
|
---|
[745] | 104 |
|
---|
[480] | 105 | if (ts.tv_nsec > 500000000) {
|
---|
| 106 | return ts.tv_sec + 1;
|
---|
| 107 | }
|
---|
| 108 | return ts.tv_sec;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | struct timespec convert_time_t_to_timespec(time_t t)
|
---|
| 112 | {
|
---|
| 113 | struct timespec ts;
|
---|
| 114 | ts.tv_sec = t;
|
---|
| 115 | ts.tv_nsec = 0;
|
---|
| 116 | return ts;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | Interpret an 8 byte "filetime" structure to a time_t
|
---|
| 123 | It's originally in "100ns units since jan 1st 1601"
|
---|
| 124 |
|
---|
| 125 | An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
|
---|
| 126 |
|
---|
| 127 | tv_sec = 0
|
---|
| 128 | tv_nsec = 0;
|
---|
| 129 |
|
---|
| 130 | Returns GMT.
|
---|
| 131 | **/
|
---|
| 132 | time_t nt_time_to_unix(NTTIME nt)
|
---|
| 133 | {
|
---|
| 134 | return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt));
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | put a 8 byte filetime from a time_t
|
---|
| 140 | This takes GMT as input
|
---|
| 141 | **/
|
---|
| 142 | _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
|
---|
| 143 | {
|
---|
| 144 | uint64_t t2;
|
---|
| 145 |
|
---|
| 146 | if (t == (time_t)-1) {
|
---|
| 147 | *nt = (NTTIME)-1LL;
|
---|
| 148 | return;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (t == TIME_T_MAX) {
|
---|
| 152 | *nt = 0x7fffffffffffffffLL;
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if (t == 0) {
|
---|
| 157 | *nt = 0;
|
---|
| 158 | return;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | t2 = t;
|
---|
| 162 | t2 += TIME_FIXUP_CONSTANT_INT;
|
---|
| 163 | t2 *= 1000*1000*10;
|
---|
| 164 |
|
---|
| 165 | *nt = t2;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | check if it's a null unix time
|
---|
| 171 | **/
|
---|
| 172 | _PUBLIC_ bool null_time(time_t t)
|
---|
| 173 | {
|
---|
| 174 | return t == 0 ||
|
---|
| 175 | t == (time_t)0xFFFFFFFF ||
|
---|
| 176 | t == (time_t)-1;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | check if it's a null NTTIME
|
---|
| 182 | **/
|
---|
| 183 | _PUBLIC_ bool null_nttime(NTTIME t)
|
---|
| 184 | {
|
---|
| 185 | return t == 0 || t == (NTTIME)-1;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /*******************************************************************
|
---|
| 189 | create a 16 bit dos packed date
|
---|
| 190 | ********************************************************************/
|
---|
| 191 | static uint16_t make_dos_date1(struct tm *t)
|
---|
| 192 | {
|
---|
| 193 | uint16_t ret=0;
|
---|
| 194 | ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
|
---|
| 195 | ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
|
---|
| 196 | return ret;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /*******************************************************************
|
---|
| 200 | create a 16 bit dos packed time
|
---|
| 201 | ********************************************************************/
|
---|
| 202 | static uint16_t make_dos_time1(struct tm *t)
|
---|
| 203 | {
|
---|
| 204 | uint16_t ret=0;
|
---|
| 205 | ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
|
---|
| 206 | ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
|
---|
| 207 | return ret;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*******************************************************************
|
---|
| 211 | create a 32 bit dos packed date/time from some parameters
|
---|
| 212 | This takes a GMT time and returns a packed localtime structure
|
---|
| 213 | ********************************************************************/
|
---|
| 214 | static uint32_t make_dos_date(time_t unixdate, int zone_offset)
|
---|
| 215 | {
|
---|
| 216 | struct tm *t;
|
---|
| 217 | uint32_t ret=0;
|
---|
| 218 |
|
---|
| 219 | if (unixdate == 0) {
|
---|
| 220 | return 0;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | unixdate -= zone_offset;
|
---|
| 224 |
|
---|
| 225 | t = gmtime(&unixdate);
|
---|
| 226 | if (!t) {
|
---|
| 227 | return 0xFFFFFFFF;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | ret = make_dos_date1(t);
|
---|
| 231 | ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
|
---|
| 232 |
|
---|
| 233 | return ret;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | put a dos date into a buffer (time/date format)
|
---|
| 238 | This takes GMT time and puts local time in the buffer
|
---|
| 239 | **/
|
---|
| 240 | _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
|
---|
| 241 | {
|
---|
| 242 | uint32_t x = make_dos_date(unixdate, zone_offset);
|
---|
| 243 | SIVAL(buf,offset,x);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | put a dos date into a buffer (date/time format)
|
---|
| 248 | This takes GMT time and puts local time in the buffer
|
---|
| 249 | **/
|
---|
| 250 | _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
|
---|
| 251 | {
|
---|
| 252 | uint32_t x;
|
---|
| 253 | x = make_dos_date(unixdate, zone_offset);
|
---|
| 254 | x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
| 255 | SIVAL(buf,offset,x);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /**
|
---|
| 259 | put a dos 32 bit "unix like" date into a buffer. This routine takes
|
---|
| 260 | GMT and converts it to LOCAL time before putting it (most SMBs assume
|
---|
| 261 | localtime for this sort of date)
|
---|
| 262 | **/
|
---|
| 263 | _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
|
---|
| 264 | {
|
---|
| 265 | if (!null_time(unixdate)) {
|
---|
| 266 | unixdate -= zone_offset;
|
---|
| 267 | }
|
---|
| 268 | SIVAL(buf,offset,unixdate);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | /*******************************************************************
|
---|
| 272 | interpret a 32 bit dos packed date/time to some parameters
|
---|
| 273 | ********************************************************************/
|
---|
| 274 | void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
|
---|
| 275 | {
|
---|
| 276 | uint32_t p0,p1,p2,p3;
|
---|
| 277 |
|
---|
| 278 | p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
|
---|
| 279 | p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
|
---|
| 280 |
|
---|
| 281 | *second = 2*(p0 & 0x1F);
|
---|
| 282 | *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
|
---|
| 283 | *hour = (p1>>3)&0xFF;
|
---|
| 284 | *day = (p2&0x1F);
|
---|
| 285 | *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
|
---|
| 286 | *year = ((p3>>1)&0xFF) + 80;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | create a unix date (int GMT) from a dos date (which is actually in
|
---|
| 291 | localtime)
|
---|
| 292 | **/
|
---|
| 293 | _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
|
---|
| 294 | {
|
---|
| 295 | uint32_t dos_date=0;
|
---|
| 296 | struct tm t;
|
---|
| 297 | time_t ret;
|
---|
| 298 |
|
---|
| 299 | dos_date = IVAL(date_ptr,0);
|
---|
| 300 |
|
---|
| 301 | if (dos_date == 0) return (time_t)0;
|
---|
| 302 |
|
---|
| 303 | interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
|
---|
| 304 | &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
|
---|
| 305 | t.tm_isdst = -1;
|
---|
| 306 |
|
---|
| 307 | ret = timegm(&t);
|
---|
| 308 |
|
---|
| 309 | ret += zone_offset;
|
---|
| 310 |
|
---|
| 311 | return ret;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /**
|
---|
| 315 | like make_unix_date() but the words are reversed
|
---|
| 316 | **/
|
---|
| 317 | _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
|
---|
| 318 | {
|
---|
| 319 | uint32_t x,x2;
|
---|
| 320 |
|
---|
| 321 | x = IVAL(date_ptr,0);
|
---|
| 322 | x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
| 323 | SIVAL(&x,0,x2);
|
---|
| 324 |
|
---|
| 325 | return pull_dos_date((const uint8_t *)&x, zone_offset);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /**
|
---|
| 329 | create a unix GMT date from a dos date in 32 bit "unix like" format
|
---|
| 330 | these generally arrive as localtimes, with corresponding DST
|
---|
| 331 | **/
|
---|
| 332 | _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
|
---|
| 333 | {
|
---|
| 334 | time_t t = (time_t)IVAL(date_ptr,0);
|
---|
| 335 | if (!null_time(t)) {
|
---|
| 336 | t += zone_offset;
|
---|
| 337 | }
|
---|
| 338 | return t;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
[745] | 342 | /****************************************************************************
|
---|
| 343 | Return the date and time as a string
|
---|
| 344 | ****************************************************************************/
|
---|
| 345 |
|
---|
| 346 | char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
|
---|
| 347 | {
|
---|
| 348 | time_t t;
|
---|
| 349 | struct tm *tm;
|
---|
| 350 |
|
---|
| 351 | t = (time_t)tp->tv_sec;
|
---|
| 352 | tm = localtime(&t);
|
---|
| 353 | if (!tm) {
|
---|
| 354 | if (hires) {
|
---|
| 355 | return talloc_asprintf(ctx,
|
---|
| 356 | "%ld.%06ld seconds since the Epoch",
|
---|
| 357 | (long)tp->tv_sec,
|
---|
| 358 | (long)tp->tv_usec);
|
---|
| 359 | } else {
|
---|
| 360 | return talloc_asprintf(ctx,
|
---|
| 361 | "%ld seconds since the Epoch",
|
---|
| 362 | (long)t);
|
---|
| 363 | }
|
---|
| 364 | } else {
|
---|
| 365 | #ifdef HAVE_STRFTIME
|
---|
| 366 | char TimeBuf[60];
|
---|
| 367 | if (hires) {
|
---|
| 368 | strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
|
---|
| 369 | return talloc_asprintf(ctx,
|
---|
| 370 | "%s.%06ld", TimeBuf,
|
---|
| 371 | (long)tp->tv_usec);
|
---|
| 372 | } else {
|
---|
| 373 | strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
|
---|
| 374 | return talloc_strdup(ctx, TimeBuf);
|
---|
| 375 | }
|
---|
| 376 | #else
|
---|
| 377 | if (hires) {
|
---|
| 378 | const char *asct = asctime(tm);
|
---|
| 379 | return talloc_asprintf(ctx, "%s.%06ld",
|
---|
| 380 | asct ? asct : "unknown",
|
---|
| 381 | (long)tp->tv_usec);
|
---|
| 382 | } else {
|
---|
| 383 | const char *asct = asctime(tm);
|
---|
| 384 | return talloc_asprintf(ctx, asct ? asct : "unknown");
|
---|
| 385 | }
|
---|
| 386 | #endif
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | char *current_timestring(TALLOC_CTX *ctx, bool hires)
|
---|
| 391 | {
|
---|
| 392 | struct timeval tv;
|
---|
| 393 |
|
---|
| 394 | GetTimeOfDay(&tv);
|
---|
| 395 | return timeval_string(ctx, &tv, hires);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 |
|
---|
[480] | 399 | /**
|
---|
| 400 | return a HTTP/1.0 time string
|
---|
| 401 | **/
|
---|
| 402 | _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
|
---|
| 403 | {
|
---|
| 404 | char *buf;
|
---|
| 405 | char tempTime[60];
|
---|
| 406 | struct tm *tm = localtime(&t);
|
---|
| 407 |
|
---|
| 408 | if (t == TIME_T_MAX) {
|
---|
| 409 | return talloc_strdup(mem_ctx, "never");
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | if (!tm) {
|
---|
| 413 | return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | #ifndef HAVE_STRFTIME
|
---|
| 417 | buf = talloc_strdup(mem_ctx, asctime(tm));
|
---|
| 418 | if (buf[strlen(buf)-1] == '\n') {
|
---|
| 419 | buf[strlen(buf)-1] = 0;
|
---|
| 420 | }
|
---|
| 421 | #else
|
---|
| 422 | strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
|
---|
| 423 | buf = talloc_strdup(mem_ctx, tempTime);
|
---|
| 424 | #endif /* !HAVE_STRFTIME */
|
---|
| 425 |
|
---|
| 426 | return buf;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | /**
|
---|
| 430 | Return the date and time as a string
|
---|
| 431 | **/
|
---|
| 432 | _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
|
---|
| 433 | {
|
---|
| 434 | char *TimeBuf;
|
---|
| 435 | char tempTime[80];
|
---|
| 436 | struct tm *tm;
|
---|
| 437 |
|
---|
| 438 | tm = localtime(&t);
|
---|
| 439 | if (!tm) {
|
---|
| 440 | return talloc_asprintf(mem_ctx,
|
---|
| 441 | "%ld seconds since the Epoch",
|
---|
| 442 | (long)t);
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | #ifdef HAVE_STRFTIME
|
---|
[745] | 446 | /* Some versions of gcc complain about using some special format
|
---|
| 447 | * specifiers. This is a bug in gcc, not a bug in this code. See a
|
---|
| 448 | * recent strftime() manual page for details. */
|
---|
| 449 | strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
|
---|
[480] | 450 | TimeBuf = talloc_strdup(mem_ctx, tempTime);
|
---|
| 451 | #else
|
---|
| 452 | TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
|
---|
| 453 | #endif
|
---|
| 454 |
|
---|
| 455 | return TimeBuf;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | /**
|
---|
| 459 | return a talloced string representing a NTTIME for human consumption
|
---|
| 460 | */
|
---|
| 461 | _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
|
---|
| 462 | {
|
---|
| 463 | time_t t;
|
---|
| 464 | if (nt == 0) {
|
---|
| 465 | return "NTTIME(0)";
|
---|
| 466 | }
|
---|
| 467 | t = nt_time_to_unix(nt);
|
---|
| 468 | return timestring(mem_ctx, t);
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 |
|
---|
| 472 | /**
|
---|
| 473 | put a NTTIME into a packet
|
---|
| 474 | */
|
---|
| 475 | _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
|
---|
| 476 | {
|
---|
| 477 | SBVAL(base, offset, t);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | /**
|
---|
| 481 | pull a NTTIME from a packet
|
---|
| 482 | */
|
---|
| 483 | _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
|
---|
| 484 | {
|
---|
| 485 | NTTIME ret = BVAL(base, offset);
|
---|
| 486 | return ret;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | /**
|
---|
| 490 | return (tv1 - tv2) in microseconds
|
---|
| 491 | */
|
---|
| 492 | _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
|
---|
| 493 | {
|
---|
| 494 | int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
|
---|
| 495 | return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[745] | 498 | /**
|
---|
| 499 | return (tp1 - tp2) in microseconds
|
---|
| 500 | */
|
---|
| 501 | _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
|
---|
| 502 | {
|
---|
| 503 | int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
|
---|
| 504 | return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
|
---|
| 505 | }
|
---|
[480] | 506 |
|
---|
[745] | 507 |
|
---|
[480] | 508 | /**
|
---|
| 509 | return a zero timeval
|
---|
| 510 | */
|
---|
| 511 | _PUBLIC_ struct timeval timeval_zero(void)
|
---|
| 512 | {
|
---|
| 513 | struct timeval tv;
|
---|
| 514 | tv.tv_sec = 0;
|
---|
| 515 | tv.tv_usec = 0;
|
---|
| 516 | return tv;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /**
|
---|
| 520 | return true if a timeval is zero
|
---|
| 521 | */
|
---|
| 522 | _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
|
---|
| 523 | {
|
---|
| 524 | return tv->tv_sec == 0 && tv->tv_usec == 0;
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /**
|
---|
| 528 | return a timeval for the current time
|
---|
| 529 | */
|
---|
| 530 | _PUBLIC_ struct timeval timeval_current(void)
|
---|
| 531 | {
|
---|
| 532 | struct timeval tv;
|
---|
| 533 | GetTimeOfDay(&tv);
|
---|
| 534 | return tv;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | /**
|
---|
| 538 | return a timeval struct with the given elements
|
---|
| 539 | */
|
---|
| 540 | _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
|
---|
| 541 | {
|
---|
| 542 | struct timeval tv;
|
---|
| 543 | tv.tv_sec = secs;
|
---|
| 544 | tv.tv_usec = usecs;
|
---|
| 545 | return tv;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 |
|
---|
| 549 | /**
|
---|
| 550 | return a timeval ofs microseconds after tv
|
---|
| 551 | */
|
---|
| 552 | _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
|
---|
| 553 | uint32_t secs, uint32_t usecs)
|
---|
| 554 | {
|
---|
| 555 | struct timeval tv2 = *tv;
|
---|
| 556 | const unsigned int million = 1000000;
|
---|
| 557 | tv2.tv_sec += secs;
|
---|
| 558 | tv2.tv_usec += usecs;
|
---|
| 559 | tv2.tv_sec += tv2.tv_usec / million;
|
---|
| 560 | tv2.tv_usec = tv2.tv_usec % million;
|
---|
| 561 | return tv2;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | /**
|
---|
| 565 | return the sum of two timeval structures
|
---|
| 566 | */
|
---|
| 567 | struct timeval timeval_sum(const struct timeval *tv1,
|
---|
| 568 | const struct timeval *tv2)
|
---|
| 569 | {
|
---|
| 570 | return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | /**
|
---|
| 574 | return a timeval secs/usecs into the future
|
---|
| 575 | */
|
---|
| 576 | _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
|
---|
| 577 | {
|
---|
| 578 | struct timeval tv = timeval_current();
|
---|
| 579 | return timeval_add(&tv, secs, usecs);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | /**
|
---|
| 583 | compare two timeval structures.
|
---|
| 584 | Return -1 if tv1 < tv2
|
---|
| 585 | Return 0 if tv1 == tv2
|
---|
| 586 | Return 1 if tv1 > tv2
|
---|
| 587 | */
|
---|
| 588 | _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
|
---|
| 589 | {
|
---|
| 590 | if (tv1->tv_sec > tv2->tv_sec) return 1;
|
---|
| 591 | if (tv1->tv_sec < tv2->tv_sec) return -1;
|
---|
| 592 | if (tv1->tv_usec > tv2->tv_usec) return 1;
|
---|
| 593 | if (tv1->tv_usec < tv2->tv_usec) return -1;
|
---|
| 594 | return 0;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | /**
|
---|
| 598 | return true if a timer is in the past
|
---|
| 599 | */
|
---|
| 600 | _PUBLIC_ bool timeval_expired(const struct timeval *tv)
|
---|
| 601 | {
|
---|
| 602 | struct timeval tv2 = timeval_current();
|
---|
| 603 | if (tv2.tv_sec > tv->tv_sec) return true;
|
---|
| 604 | if (tv2.tv_sec < tv->tv_sec) return false;
|
---|
| 605 | return (tv2.tv_usec >= tv->tv_usec);
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | /**
|
---|
| 609 | return the number of seconds elapsed between two times
|
---|
| 610 | */
|
---|
| 611 | _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
|
---|
| 612 | {
|
---|
| 613 | return (tv2->tv_sec - tv1->tv_sec) +
|
---|
| 614 | (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | /**
|
---|
| 618 | return the number of seconds elapsed since a given time
|
---|
| 619 | */
|
---|
| 620 | _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
|
---|
| 621 | {
|
---|
| 622 | struct timeval tv2 = timeval_current();
|
---|
| 623 | return timeval_elapsed2(tv, &tv2);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | /**
|
---|
| 627 | return the lesser of two timevals
|
---|
| 628 | */
|
---|
| 629 | _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
|
---|
| 630 | const struct timeval *tv2)
|
---|
| 631 | {
|
---|
| 632 | if (tv1->tv_sec < tv2->tv_sec) return *tv1;
|
---|
| 633 | if (tv1->tv_sec > tv2->tv_sec) return *tv2;
|
---|
| 634 | if (tv1->tv_usec < tv2->tv_usec) return *tv1;
|
---|
| 635 | return *tv2;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | /**
|
---|
| 639 | return the greater of two timevals
|
---|
| 640 | */
|
---|
| 641 | _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
|
---|
| 642 | const struct timeval *tv2)
|
---|
| 643 | {
|
---|
| 644 | if (tv1->tv_sec > tv2->tv_sec) return *tv1;
|
---|
| 645 | if (tv1->tv_sec < tv2->tv_sec) return *tv2;
|
---|
| 646 | if (tv1->tv_usec > tv2->tv_usec) return *tv1;
|
---|
| 647 | return *tv2;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | /**
|
---|
| 651 | return the difference between two timevals as a timeval
|
---|
| 652 | if tv1 comes after tv2, then return a zero timeval
|
---|
| 653 | (this is *tv2 - *tv1)
|
---|
| 654 | */
|
---|
| 655 | _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
|
---|
| 656 | const struct timeval *tv2)
|
---|
| 657 | {
|
---|
| 658 | struct timeval t;
|
---|
| 659 | if (timeval_compare(tv1, tv2) >= 0) {
|
---|
| 660 | return timeval_zero();
|
---|
| 661 | }
|
---|
| 662 | t.tv_sec = tv2->tv_sec - tv1->tv_sec;
|
---|
| 663 | if (tv1->tv_usec > tv2->tv_usec) {
|
---|
| 664 | t.tv_sec--;
|
---|
| 665 | t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
|
---|
| 666 | } else {
|
---|
| 667 | t.tv_usec = tv2->tv_usec - tv1->tv_usec;
|
---|
| 668 | }
|
---|
| 669 | return t;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 |
|
---|
| 673 | /**
|
---|
| 674 | convert a timeval to a NTTIME
|
---|
| 675 | */
|
---|
| 676 | _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
|
---|
| 677 | {
|
---|
| 678 | return 10*(tv->tv_usec +
|
---|
| 679 | ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | /**
|
---|
| 683 | convert a NTTIME to a timeval
|
---|
| 684 | */
|
---|
| 685 | _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
|
---|
| 686 | {
|
---|
| 687 | if (tv == NULL) return;
|
---|
| 688 |
|
---|
| 689 | t += 10/2;
|
---|
| 690 | t /= 10;
|
---|
| 691 | t -= TIME_FIXUP_CONSTANT*1000*1000;
|
---|
| 692 |
|
---|
| 693 | tv->tv_sec = t / 1000000;
|
---|
| 694 |
|
---|
| 695 | if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
|
---|
| 696 | tv->tv_sec = 0;
|
---|
| 697 | tv->tv_usec = 0;
|
---|
| 698 | return;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | tv->tv_usec = t - tv->tv_sec*1000000;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | /*******************************************************************
|
---|
| 705 | yield the difference between *A and *B, in seconds, ignoring leap seconds
|
---|
| 706 | ********************************************************************/
|
---|
| 707 | static int tm_diff(struct tm *a, struct tm *b)
|
---|
| 708 | {
|
---|
| 709 | int ay = a->tm_year + (1900 - 1);
|
---|
| 710 | int by = b->tm_year + (1900 - 1);
|
---|
| 711 | int intervening_leap_days =
|
---|
| 712 | (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
|
---|
| 713 | int years = ay - by;
|
---|
| 714 | int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
|
---|
| 715 | int hours = 24*days + (a->tm_hour - b->tm_hour);
|
---|
| 716 | int minutes = 60*hours + (a->tm_min - b->tm_min);
|
---|
| 717 | int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
|
---|
| 718 |
|
---|
| 719 | return seconds;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 |
|
---|
| 723 | int extra_time_offset=0;
|
---|
| 724 |
|
---|
| 725 | /**
|
---|
| 726 | return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
|
---|
| 727 | */
|
---|
| 728 | _PUBLIC_ int get_time_zone(time_t t)
|
---|
| 729 | {
|
---|
| 730 | struct tm *tm = gmtime(&t);
|
---|
| 731 | struct tm tm_utc;
|
---|
| 732 | if (!tm)
|
---|
| 733 | return 0;
|
---|
| 734 | tm_utc = *tm;
|
---|
| 735 | tm = localtime(&t);
|
---|
| 736 | if (!tm)
|
---|
| 737 | return 0;
|
---|
| 738 | return tm_diff(&tm_utc,tm)+60*extra_time_offset;
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | struct timespec nt_time_to_unix_timespec(NTTIME *nt)
|
---|
| 742 | {
|
---|
| 743 | int64_t d;
|
---|
| 744 | struct timespec ret;
|
---|
| 745 |
|
---|
| 746 | if (*nt == 0 || *nt == (int64_t)-1) {
|
---|
| 747 | ret.tv_sec = 0;
|
---|
| 748 | ret.tv_nsec = 0;
|
---|
| 749 | return ret;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | d = (int64_t)*nt;
|
---|
| 753 | /* d is now in 100ns units, since jan 1st 1601".
|
---|
| 754 | Save off the ns fraction. */
|
---|
| 755 |
|
---|
| 756 | /*
|
---|
| 757 | * Take the last seven decimal digits and multiply by 100.
|
---|
| 758 | * to convert from 100ns units to 1ns units.
|
---|
| 759 | */
|
---|
| 760 | ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
|
---|
| 761 |
|
---|
| 762 | /* Convert to seconds */
|
---|
| 763 | d /= 1000*1000*10;
|
---|
| 764 |
|
---|
| 765 | /* Now adjust by 369 years to make the secs since 1970 */
|
---|
| 766 | d -= TIME_FIXUP_CONSTANT_INT;
|
---|
| 767 |
|
---|
| 768 | if (d <= (int64_t)TIME_T_MIN) {
|
---|
| 769 | ret.tv_sec = TIME_T_MIN;
|
---|
| 770 | ret.tv_nsec = 0;
|
---|
| 771 | return ret;
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | if (d >= (int64_t)TIME_T_MAX) {
|
---|
| 775 | ret.tv_sec = TIME_T_MAX;
|
---|
| 776 | ret.tv_nsec = 0;
|
---|
| 777 | return ret;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
| 780 | ret.tv_sec = (time_t)d;
|
---|
| 781 | return ret;
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 |
|
---|
| 785 | /**
|
---|
| 786 | check if 2 NTTIMEs are equal.
|
---|
| 787 | */
|
---|
| 788 | bool nt_time_equal(NTTIME *t1, NTTIME *t2)
|
---|
| 789 | {
|
---|
| 790 | return *t1 == *t2;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | /**
|
---|
| 794 | Check if it's a null timespec.
|
---|
| 795 | **/
|
---|
| 796 |
|
---|
| 797 | bool null_timespec(struct timespec ts)
|
---|
| 798 | {
|
---|
| 799 | return ts.tv_sec == 0 ||
|
---|
| 800 | ts.tv_sec == (time_t)0xFFFFFFFF ||
|
---|
| 801 | ts.tv_sec == (time_t)-1;
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 |
|
---|