Changeset 740 for vendor/current/source3/lib/time.c
- Timestamp:
- Nov 14, 2012, 12:59:34 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/lib/time.c
r478 r740 37 37 #endif 38 38 39 /*******************************************************************40 create a 16 bit dos packed date41 ********************************************************************/42 static uint16_t make_dos_date1(struct tm *t)43 {44 uint16_t ret=0;45 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);46 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));47 return ret;48 }49 50 /*******************************************************************51 create a 16 bit dos packed time52 ********************************************************************/53 static uint16_t make_dos_time1(struct tm *t)54 {55 uint16_t ret=0;56 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));57 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));58 return ret;59 }60 61 /*******************************************************************62 create a 32 bit dos packed date/time from some parameters63 This takes a GMT time and returns a packed localtime structure64 ********************************************************************/65 static uint32_t make_dos_date(time_t unixdate, int zone_offset)66 {67 struct tm *t;68 uint32_t ret=0;69 70 if (unixdate == 0) {71 return 0;72 }73 74 unixdate -= zone_offset;75 76 t = gmtime(&unixdate);77 if (!t) {78 return 0xFFFFFFFF;79 }80 81 ret = make_dos_date1(t);82 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);83 84 return ret;85 }86 39 87 40 /** … … 98 51 **************************************************************/ 99 52 100 uint32_t convert_time_t_to_uint32 (time_t t)53 uint32_t convert_time_t_to_uint32_t(time_t t) 101 54 { 102 55 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8)) … … 111 64 } 112 65 113 time_t convert_uint32_t o_time_t(uint32_t u)66 time_t convert_uint32_t_to_time_t(uint32_t u) 114 67 { 115 68 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8)) … … 177 130 } 178 131 179 /****************************************************************************180 Return the date and time as a string181 ****************************************************************************/182 183 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)184 {185 fstring TimeBuf;186 time_t t;187 struct tm *tm;188 189 t = (time_t)tp->tv_sec;190 tm = localtime(&t);191 if (!tm) {192 if (hires) {193 slprintf(TimeBuf,194 sizeof(TimeBuf)-1,195 "%ld.%06ld seconds since the Epoch",196 (long)tp->tv_sec,197 (long)tp->tv_usec);198 } else {199 slprintf(TimeBuf,200 sizeof(TimeBuf)-1,201 "%ld seconds since the Epoch",202 (long)t);203 }204 } else {205 #ifdef HAVE_STRFTIME206 if (hires) {207 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);208 slprintf(TimeBuf+strlen(TimeBuf),209 sizeof(TimeBuf)-1 - strlen(TimeBuf),210 ".%06ld",211 (long)tp->tv_usec);212 } else {213 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);214 }215 #else216 if (hires) {217 const char *asct = asctime(tm);218 slprintf(TimeBuf,219 sizeof(TimeBuf)-1,220 "%s.%06ld",221 asct ? asct : "unknown",222 (long)tp->tv_usec);223 } else {224 const char *asct = asctime(tm);225 fstrcpy(TimeBuf, asct ? asct : "unknown");226 }227 #endif228 }229 return talloc_strdup(ctx, TimeBuf);230 }231 232 char *current_timestring(TALLOC_CTX *ctx, bool hires)233 {234 struct timeval tv;235 236 GetTimeOfDay(&tv);237 return timeval_string(ctx, &tv, hires);238 }239 240 /*******************************************************************241 Put a dos date into a buffer (time/date format).242 This takes GMT time and puts local time in the buffer.243 ********************************************************************/244 245 static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)246 {247 uint32_t x = make_dos_date(unixdate, zone_offset);248 SIVAL(buf,offset,x);249 }250 251 /*******************************************************************252 Put a dos date into a buffer (date/time format).253 This takes GMT time and puts local time in the buffer.254 ********************************************************************/255 256 static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)257 {258 uint32_t x = make_dos_date(unixdate, zone_offset);259 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);260 SIVAL(buf,offset,x);261 }262 263 /*******************************************************************264 Put a dos 32 bit "unix like" date into a buffer. This routine takes265 GMT and converts it to LOCAL time before putting it (most SMBs assume266 localtime for this sort of date)267 ********************************************************************/268 269 static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)270 {271 if (!null_mtime(unixdate)) {272 unixdate -= zone_offset;273 }274 SIVAL(buf,offset,unixdate);275 }276 277 278 132 /*************************************************************************** 279 133 Server versions of the above functions. … … 282 136 void srv_put_dos_date(char *buf,int offset,time_t unixdate) 283 137 { 284 pu t_dos_date(buf, offset, unixdate, server_zone_offset);138 push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset); 285 139 } 286 140 287 141 void srv_put_dos_date2(char *buf,int offset, time_t unixdate) 288 142 { 289 pu t_dos_date2(buf, offset, unixdate, server_zone_offset);143 push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset); 290 144 } 291 145 292 146 void srv_put_dos_date3(char *buf,int offset,time_t unixdate) 293 147 { 294 pu t_dos_date3(buf, offset, unixdate, server_zone_offset);148 push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset); 295 149 } 296 150 … … 343 197 ********************************************************************/ 344 198 345 statictime_t make_unix_date(const void *date_ptr, int zone_offset)199 time_t make_unix_date(const void *date_ptr, int zone_offset) 346 200 { 347 201 uint32_t dos_date=0; … … 389 243 { 390 244 time_t t = (time_t)IVAL(date_ptr,0); 391 if (!null_ mtime(t)) {245 if (!null_time(t)) { 392 246 t += zone_offset; 393 247 } … … 440 294 struct timespec timespec_current(void) 441 295 { 442 struct timeval tv;443 296 struct timespec ts; 444 GetTimeOfDay(&tv); 445 ts.tv_sec = tv.tv_sec; 446 ts.tv_nsec = tv.tv_usec * 1000; 297 clock_gettime(CLOCK_REALTIME, &ts); 447 298 return ts; 448 299 } … … 496 347 struct timeval tv = convert_timespec_to_timeval(*ts); 497 348 *ts = convert_timeval_to_timespec(tv); 349 while (ts->tv_nsec > 1000000000) { 350 ts->tv_sec += 1; 351 ts->tv_nsec -= 1000000000; 352 } 498 353 } 499 354 … … 517 372 } 518 373 519 /***************************************************************************520 Client versions of the above functions.521 ***************************************************************************/522 523 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)524 {525 put_dos_date(buf, offset, unixdate, cli->serverzone);526 }527 528 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)529 {530 put_dos_date2(buf, offset, unixdate, cli->serverzone);531 }532 533 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)534 {535 put_dos_date3(buf, offset, unixdate, cli->serverzone);536 }537 538 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)539 {540 return make_unix_date(date_ptr, cli->serverzone);541 }542 543 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)544 {545 return make_unix_date2(date_ptr, cli->serverzone);546 }547 548 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)549 {550 return make_unix_date3(date_ptr, cli->serverzone);551 }552 553 /****************************************************************************554 Check if two NTTIMEs are the same.555 ****************************************************************************/556 557 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)558 {559 return (*nt1 == *nt2);560 }561 562 374 /******************************************************************* 563 375 Re-read the smb serverzone value. … … 598 410 } 599 411 412 /** 413 * @brief Get the startup time of the server. 414 * 415 * @param[out] ret_time A pointer to a timveal structure to set the startup 416 * time. 417 */ 418 void get_startup_time(struct timeval *ret_time) 419 { 420 ret_time->tv_sec = start_time_hires.tv_sec; 421 ret_time->tv_usec = start_time_hires.tv_usec; 422 } 423 424 600 425 /**************************************************************************** 601 426 Convert a NTTIME structure to a time_t. … … 740 565 741 566 /**************************************************************************** 742 Check if it's a null mtime.743 ****************************************************************************/744 745 bool null_mtime(time_t mtime)746 {747 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)748 return true;749 return false;750 }751 752 /****************************************************************************753 567 Utility function that always returns a const string even if localtime 754 568 and asctime fail.
Note:
See TracChangeset
for help on using the changeset viewer.