📄 time.c
字号:
********************************************************************/static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second){ uint32 p0,p1,p2,p3; p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF; *second = 2*(p0 & 0x1F); *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3); *hour = (p1>>3)&0xFF; *day = (p2&0x1F); *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1; *year = ((p3>>1)&0xFF) + 80;}/******************************************************************* Create a unix date (int GMT) from a dos date (which is actually in localtime).********************************************************************/static time_t make_unix_date(void *date_ptr, int zone_offset){ uint32 dos_date=0; struct tm t; time_t ret; dos_date = IVAL(date_ptr,0); if (dos_date == 0) { return 0; } interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon, &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec); t.tm_isdst = -1; ret = timegm(&t); ret += zone_offset; return(ret);}/******************************************************************* Like make_unix_date() but the words are reversed.********************************************************************/static time_t make_unix_date2(void *date_ptr, int zone_offset){ uint32 x,x2; x = IVAL(date_ptr,0); x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16); SIVAL(&x,0,x2); return(make_unix_date((void *)&x, zone_offset));}/******************************************************************* Create a unix GMT date from a dos date in 32 bit "unix like" format these generally arrive as localtimes, with corresponding DST.******************************************************************/static time_t make_unix_date3(void *date_ptr, int zone_offset){ time_t t = (time_t)IVAL(date_ptr,0); if (!null_mtime(t)) { t += zone_offset; } return(t);}/*************************************************************************** Server versions of the above functions.***************************************************************************/void srv_put_dos_date(char *buf,int offset,time_t unixdate){ put_dos_date(buf, offset, unixdate, server_zone_offset);}void srv_put_dos_date2(char *buf,int offset, time_t unixdate){ put_dos_date2(buf, offset, unixdate, server_zone_offset);}void srv_put_dos_date3(char *buf,int offset,time_t unixdate){ put_dos_date3(buf, offset, unixdate, server_zone_offset);}time_t srv_make_unix_date(void *date_ptr){ return make_unix_date(date_ptr, server_zone_offset);}time_t srv_make_unix_date2(void *date_ptr){ return make_unix_date2(date_ptr, server_zone_offset);}time_t srv_make_unix_date3(void *date_ptr){ return make_unix_date3(date_ptr, server_zone_offset);}/*************************************************************************** Client versions of the above functions.***************************************************************************/void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate){ put_dos_date(buf, offset, unixdate, cli->serverzone);}void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate){ put_dos_date2(buf, offset, unixdate, cli->serverzone);}void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate){ put_dos_date3(buf, offset, unixdate, cli->serverzone);}time_t cli_make_unix_date(struct cli_state *cli, void *date_ptr){ return make_unix_date(date_ptr, cli->serverzone);}time_t cli_make_unix_date2(struct cli_state *cli, void *date_ptr){ return make_unix_date2(date_ptr, cli->serverzone);}time_t cli_make_unix_date3(struct cli_state *cli, void *date_ptr){ return make_unix_date3(date_ptr, cli->serverzone);}/*************************************************************************** Return a HTTP/1.0 time string.***************************************************************************/char *http_timestring(time_t t){ static fstring buf; struct tm *tm = localtime(&t); if (!tm) slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t); else#ifndef HAVE_STRFTIME fstrcpy(buf, asctime(tm)); if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;#else /* !HAVE_STRFTIME */ strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);#endif /* !HAVE_STRFTIME */ return buf;}/**************************************************************************** Return the date and time as a string****************************************************************************/char *timestring(BOOL hires){ static fstring TimeBuf; struct timeval tp; time_t t; struct tm *tm; if (hires) { GetTimeOfDay(&tp); t = (time_t)tp.tv_sec; } else { t = time(NULL); } tm = localtime(&t); if (!tm) { if (hires) { slprintf(TimeBuf, sizeof(TimeBuf)-1, "%ld.%06ld seconds since the Epoch", (long)tp.tv_sec, (long)tp.tv_usec); } else { slprintf(TimeBuf, sizeof(TimeBuf)-1, "%ld seconds since the Epoch", (long)t); } } else {#ifdef HAVE_STRFTIME if (hires) { strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm); slprintf(TimeBuf+strlen(TimeBuf), sizeof(TimeBuf)-1 - strlen(TimeBuf), ".%06ld", (long)tp.tv_usec); } else { strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm); }#else if (hires) { slprintf(TimeBuf, sizeof(TimeBuf)-1, "%s.%06ld", asctime(tm), (long)tp.tv_usec); } else { fstrcpy(TimeBuf, asctime(tm)); }#endif } return(TimeBuf);}/**************************************************************************** Return the best approximation to a 'create time' under UNIX from a stat structure.****************************************************************************/time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs){ time_t ret, ret1; if(S_ISDIR(st->st_mode) && fake_dirs) { return (time_t)315493200L; /* 1/1/1980 */ } ret = MIN(st->st_ctime, st->st_mtime); ret1 = MIN(ret, st->st_atime); if(ret1 != (time_t)0) { return ret1; } /* * One of ctime, mtime or atime was zero (probably atime). * Just return MIN(ctime, mtime). */ return ret;}/**************************************************************************** Initialise an NTTIME to -1, which means "unknown" or "don't expire".****************************************************************************/void init_nt_time(NTTIME *nt){ nt->high = 0x7FFFFFFF; nt->low = 0xFFFFFFFF;}/**************************************************************************** Check if NTTIME is 0.****************************************************************************/BOOL nt_time_is_zero(NTTIME *nt){ if(nt->high==0) { return True; } return False;}/**************************************************************************** Check if two NTTIMEs are the same.****************************************************************************/BOOL nt_time_equals(NTTIME *nt1, NTTIME *nt2){ return (nt1->high == nt2->high && nt1->low == nt2->low);}/**************************************************************************** Return a timeval difference in usec.****************************************************************************/SMB_BIG_INT usec_time_diff(const struct timeval *larget, const struct timeval *smallt){ SMB_BIG_INT sec_diff = larget->tv_sec - smallt->tv_sec; return (sec_diff * 1000000) + (SMB_BIG_INT)(larget->tv_usec - smallt->tv_usec);}/**************************************************************************** Return a timeval struct with the given elements.****************************************************************************/struct timeval timeval_set(uint32_t secs, uint32_t usecs){ struct timeval tv; tv.tv_sec = secs; tv.tv_usec = usecs; return tv;}/**************************************************************************** Return a zero timeval.****************************************************************************/struct timeval timeval_zero(void){ return timeval_set(0,0);}/**************************************************************************** Return True if a timeval is zero.****************************************************************************/BOOL timeval_is_zero(const struct timeval *tv){ return tv->tv_sec == 0 && tv->tv_usec == 0;}/**************************************************************************** Return a timeval for the current time.****************************************************************************/struct timeval timeval_current(void){ struct timeval tv; GetTimeOfDay(&tv); return tv;}/**************************************************************************** Return a timeval ofs microseconds after tv.****************************************************************************/struct timeval timeval_add(const struct timeval *tv, uint32_t secs, uint32_t usecs){ struct timeval tv2 = *tv; tv2.tv_sec += secs; tv2.tv_usec += usecs; tv2.tv_sec += tv2.tv_usec / 1000000; tv2.tv_usec = tv2.tv_usec % 1000000; return tv2;}/**************************************************************************** Return the sum of two timeval structures.****************************************************************************/struct timeval timeval_sum(const struct timeval *tv1, const struct timeval *tv2){ return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);}/**************************************************************************** Return a timeval secs/usecs into the future.****************************************************************************/struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs){ struct timeval tv = timeval_current(); return timeval_add(&tv, secs, usecs);}/**************************************************************************** Compare two timeval structures. Return -1 if tv1 < tv2 Return 0 if tv1 == tv2 Return 1 if tv1 > tv2****************************************************************************/int timeval_compare(const struct timeval *tv1, const struct timeval *tv2){ if (tv1->tv_sec > tv2->tv_sec) { return 1; } if (tv1->tv_sec < tv2->tv_sec) { return -1; } if (tv1->tv_usec > tv2->tv_usec) { return 1; } if (tv1->tv_usec < tv2->tv_usec) { return -1; } return 0;}/**************************************************************************** Return the difference between two timevals as a timeval. If tv1 comes after tv2, then return a zero timeval (this is *tv2 - *tv1).****************************************************************************/struct timeval timeval_until(const struct timeval *tv1, const struct timeval *tv2){ struct timeval t; if (timeval_compare(tv1, tv2) >= 0) { return timeval_zero(); } t.tv_sec = tv2->tv_sec - tv1->tv_sec; if (tv1->tv_usec > tv2->tv_usec) { t.tv_sec--; t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec); } else { t.tv_usec = tv2->tv_usec - tv1->tv_usec; } return t;}/**************************************************************************** Return the lesser of two timevals.****************************************************************************/struct timeval timeval_min(const struct timeval *tv1, const struct timeval *tv2){ if (tv1->tv_sec < tv2->tv_sec) { return *tv1; } if (tv1->tv_sec > tv2->tv_sec) { return *tv2; } if (tv1->tv_usec < tv2->tv_usec) { return *tv1; } return *tv2;}/**************************************************************************** Return the greater of two timevals.****************************************************************************/struct timeval timeval_max(const struct timeval *tv1, const struct timeval *tv2){ if (tv1->tv_sec > tv2->tv_sec) { return *tv1; } if (tv1->tv_sec < tv2->tv_sec) { return *tv2; } if (tv1->tv_usec > tv2->tv_usec) { return *tv1; } return *tv2;}/**************************************************************************** Convert ASN.1 GeneralizedTime string to unix-time. Returns 0 on failure; Currently ignores timezone. ****************************************************************************/time_t generalized_to_unix_time(const char *str){ struct tm tm; ZERO_STRUCT(tm); if (sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) { return 0; } tm.tm_year -= 1900; tm.tm_mon -= 1; return timegm(&tm);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -