Changeset 988 for vendor/current/source3/lib/time.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/lib/time.c
r740 r988 1 /* 1 /* 2 2 Unix SMB/CIFS implementation. 3 3 time handling functions 4 4 5 5 Copyright (C) Andrew Tridgell 1992-2004 6 Copyright (C) Stefan (metze) Metzmacher 2002 6 Copyright (C) Stefan (metze) Metzmacher 2002 7 7 Copyright (C) Jeremy Allison 2007 8 8 … … 11 11 the Free Software Foundation; either version 3 of the License, or 12 12 (at your option) any later version. 13 13 14 14 This program is distributed in the hope that it will be useful, 15 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 17 GNU General Public License for more details. 18 18 19 19 You should have received a copy of the GNU General Public License 20 20 along with this program. If not, see <http://www.gnu.org/licenses/>. … … 88 88 /**************************************************************************** 89 89 Convert ASN.1 GeneralizedTime string to unix-time. 90 Returns 0 on failure; Currently ignores timezone. 90 Returns 0 on failure; Currently ignores timezone. 91 91 ****************************************************************************/ 92 92 93 93 time_t generalized_to_unix_time(const char *str) 94 { 94 { 95 95 struct tm tm; 96 96 97 97 ZERO_STRUCT(tm); 98 98 99 if (sscanf(str, "%4d%2d%2d%2d%2d%2d", 100 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 99 if (sscanf(str, "%4d%2d%2d%2d%2d%2d", 100 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 101 101 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) { 102 102 return 0; … … 165 165 166 166 /**************************************************************************** 167 Take a Unix time and convert to an NTTIME structure and place in buffer 167 Take a Unix time and convert to an NTTIME structure and place in buffer 168 168 pointed to by p, rounded to the correct resolution. 169 169 ****************************************************************************/ … … 173 173 NTTIME nt; 174 174 round_timespec(res, &ts); 175 unix_timespec_to_nt_time(&nt, ts); 176 SIVAL(p, 0, nt & 0xFFFFFFFF); 177 SIVAL(p, 4, nt >> 32); 175 nt = unix_timespec_to_nt_time(ts); 176 SBVAL(p, 0, nt); 178 177 } 179 178 … … 199 198 time_t make_unix_date(const void *date_ptr, int zone_offset) 200 199 { 201 uint32_t dos_date=0; 202 struct tm t; 203 time_t ret; 204 205 dos_date = IVAL(date_ptr,0); 206 207 if (dos_date == 0) { 208 return 0; 209 } 210 211 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon, 212 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec); 213 t.tm_isdst = -1; 214 215 ret = timegm(&t); 216 217 ret += zone_offset; 218 219 return(ret); 200 return pull_dos_date(date_ptr, zone_offset); 220 201 } 221 202 … … 226 207 time_t make_unix_date2(const void *date_ptr, int zone_offset) 227 208 { 228 uint32_t x,x2; 229 230 x = IVAL(date_ptr,0); 231 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16); 232 SIVAL(&x,0,x2); 233 234 return(make_unix_date((const void *)&x, zone_offset)); 209 return pull_dos_date2(date_ptr, zone_offset); 235 210 } 236 211 … … 242 217 time_t make_unix_date3(const void *date_ptr, int zone_offset) 243 218 { 244 time_t t = (time_t)IVAL(date_ptr,0); 245 if (!null_time(t)) { 246 t += zone_offset; 247 } 248 return(t); 219 return pull_dos_date3(date_ptr, zone_offset); 249 220 } 250 221 … … 262 233 { 263 234 return make_unix_date3(date_ptr, server_zone_offset); 264 }265 266 /****************************************************************************267 Convert a normalized timeval to a timespec.268 ****************************************************************************/269 270 struct timespec convert_timeval_to_timespec(const struct timeval tv)271 {272 struct timespec ts;273 ts.tv_sec = tv.tv_sec;274 ts.tv_nsec = tv.tv_usec * 1000;275 return ts;276 }277 278 /****************************************************************************279 Convert a normalized timespec to a timeval.280 ****************************************************************************/281 282 struct timeval convert_timespec_to_timeval(const struct timespec ts)283 {284 struct timeval tv;285 tv.tv_sec = ts.tv_sec;286 tv.tv_usec = ts.tv_nsec / 1000;287 return tv;288 }289 290 /****************************************************************************291 Return a timespec for the current time292 ****************************************************************************/293 294 struct timespec timespec_current(void)295 {296 struct timespec ts;297 clock_gettime(CLOCK_REALTIME, &ts);298 return ts;299 }300 301 /****************************************************************************302 Return the lesser of two timespecs.303 ****************************************************************************/304 305 struct timespec timespec_min(const struct timespec *ts1,306 const struct timespec *ts2)307 {308 if (ts1->tv_sec < ts2->tv_sec) return *ts1;309 if (ts1->tv_sec > ts2->tv_sec) return *ts2;310 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;311 return *ts2;312 }313 314 /****************************************************************************315 compare two timespec structures.316 Return -1 if ts1 < ts2317 Return 0 if ts1 == ts2318 Return 1 if ts1 > ts2319 ****************************************************************************/320 321 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)322 {323 if (ts1->tv_sec > ts2->tv_sec) return 1;324 if (ts1->tv_sec < ts2->tv_sec) return -1;325 if (ts1->tv_nsec > ts2->tv_nsec) return 1;326 if (ts1->tv_nsec < ts2->tv_nsec) return -1;327 return 0;328 }329 330 /****************************************************************************331 Round up a timespec if nsec > 500000000, round down if lower,332 then zero nsec.333 ****************************************************************************/334 335 void round_timespec_to_sec(struct timespec *ts)336 {337 ts->tv_sec = convert_timespec_to_time_t(*ts);338 ts->tv_nsec = 0;339 }340 341 /****************************************************************************342 Round a timespec to usec value.343 ****************************************************************************/344 345 void round_timespec_to_usec(struct timespec *ts)346 {347 struct timeval tv = convert_timespec_to_timeval(*ts);348 *ts = convert_timeval_to_timespec(tv);349 while (ts->tv_nsec > 1000000000) {350 ts->tv_sec += 1;351 ts->tv_nsec -= 1000000000;352 }353 235 } 354 236 … … 362 244 { 363 245 NTTIME nt; 364 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);246 nt = BVAL(p, 0); 365 247 if (nt == (uint64_t)-1) { 366 248 struct timespec ret; … … 369 251 return ret; 370 252 } 371 return nt_time_to_unix_timespec( &nt);253 return nt_time_to_unix_timespec(nt); 372 254 } 373 255 … … 470 352 471 353 /**************************************************************************** 472 Put a 8 byte filetime from a struct timespec. Uses GMT.473 ****************************************************************************/474 475 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)476 {477 uint64_t d;478 479 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {480 *nt = 0;481 return;482 }483 if (ts.tv_sec == TIME_T_MAX) {484 *nt = 0x7fffffffffffffffLL;485 return;486 }487 if (ts.tv_sec == (time_t)-1) {488 *nt = (uint64_t)-1;489 return;490 }491 492 d = ts.tv_sec;493 d += TIME_FIXUP_CONSTANT_INT;494 d *= 1000*1000*10;495 /* d is now in 100ns units. */496 d += (ts.tv_nsec / 100);497 498 *nt = d;499 }500 501 #if 0502 void nt_time_to_unix_timespec(struct timespec *ts, NTTIME t)503 {504 if (ts == NULL) {505 return;506 }507 508 /* t starts in 100 nsec units since 1601-01-01. */509 510 t *= 100;511 /* t is now in nsec units since 1601-01-01. */512 513 t -= TIME_FIXUP_CONSTANT*1000*1000*100;514 /* t is now in nsec units since the UNIX epoch 1970-01-01. */515 516 ts->tv_sec = t / 1000000000LL;517 518 if (TIME_T_MIN > ts->tv_sec || ts->tv_sec > TIME_T_MAX) {519 ts->tv_sec = 0;520 ts->tv_nsec = 0;521 return;522 }523 524 ts->tv_nsec = t - ts->tv_sec*1000000000LL;525 }526 #endif527 528 /****************************************************************************529 354 Convert a time_t to a NTTIME structure 530 355 … … 547 372 return; 548 373 } 549 374 550 375 if (t == (time_t)-1) { 551 376 /* that's what NT uses for infinite */ 552 377 *nt = NTTIME_INFINITY; 553 378 return; 554 } 379 } 555 380 556 381 d = (double)(t); … … 598 423 return "Never"; 599 424 600 high = 65536; 425 high = 65536; 601 426 high = high/10000; 602 427 high = high*65536;
Note:
See TracChangeset
for help on using the changeset viewer.