📄 time.c
字号:
return(ret);}/****************************************************************************interprets an nt time into a unix time_t****************************************************************************/time_t interpret_long_date(char *p){ NTTIME nt; nt.low = IVAL(p,0); nt.high = IVAL(p,4); return nt_time_to_unix(&nt);}/****************************************************************************put a 8 byte filetime from a time_tThis takes real GMT as input and converts to kludge-GMT****************************************************************************/void unix_to_nt_time(NTTIME *nt, time_t t){ double d; if (t==0) { nt->low = 0; nt->high = 0; return; } /* this converts GMT to kludge-GMT */ t -= LocTimeDiff(t) - serverzone; d = (double)(t); d += TIME_FIXUP_CONSTANT; d *= 1.0e7; nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30)))); nt->low = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));}/****************************************************************************take an NTTIME structure, containing high / low time. convert to unix time.lkclXXXX this may need 2 SIVALs not a memcpy. we'll see...****************************************************************************/void put_long_date(char *p,time_t t){ NTTIME nt; unix_to_nt_time(&nt, t); SIVAL(p, 0, nt.low); SIVAL(p, 4, nt.high);}/****************************************************************************check if it's a null mtime****************************************************************************/BOOL null_mtime(time_t mtime){ if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1) return(True); return(False);}/******************************************************************* create a 16 bit dos packed date********************************************************************/static uint16 make_dos_date1(struct tm *t){ uint16 ret=0; ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1); ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5)); return(ret);}/******************************************************************* create a 16 bit dos packed time********************************************************************/static uint16 make_dos_time1(struct tm *t){ uint16 ret=0; ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3)); ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5)); return(ret);}/******************************************************************* create a 32 bit dos packed date/time from some parameters This takes a GMT time and returns a packed localtime structure********************************************************************/static uint32 make_dos_date(time_t unixdate){ struct tm *t; uint32 ret=0; t = LocalTime(&unixdate); if (!t) return 0xFFFFFFFF; ret = make_dos_date1(t); ret = ((ret&0xFFFF)<<16) | make_dos_time1(t); return(ret);}/*******************************************************************put a dos date into a buffer (time/date format)This takes GMT time and puts local time in the buffer********************************************************************/void put_dos_date(char *buf,int offset,time_t unixdate){ uint32 x = make_dos_date(unixdate); SIVAL(buf,offset,x);}/*******************************************************************put a dos date into a buffer (date/time format)This takes GMT time and puts local time in the buffer********************************************************************/void put_dos_date2(char *buf,int offset,time_t unixdate){ uint32 x = make_dos_date(unixdate); x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16); SIVAL(buf,offset,x);}/*******************************************************************put a dos 32 bit "unix like" date into a buffer. This routine takesGMT and converts it to LOCAL time before putting it (most SMBs assumelocaltime for this sort of date)********************************************************************/void put_dos_date3(char *buf,int offset,time_t unixdate){ if (!null_mtime(unixdate)) unixdate -= TimeDiff(unixdate); SIVAL(buf,offset,unixdate);}/******************************************************************* interpret a 32 bit dos packed date/time to some parameters********************************************************************/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)********************************************************************/time_t make_unix_date(void *date_ptr){ 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; /* mktime() also does the local to GMT time conversion for us */ ret = mktime(&t); return(ret);}/*******************************************************************like make_unix_date() but the words are reversed********************************************************************/time_t make_unix_date2(void *date_ptr){ 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));}/******************************************************************* create a unix GMT date from a dos date in 32 bit "unix like" format these generally arrive as localtimes, with corresponding DST ******************************************************************/time_t make_unix_date3(void *date_ptr){ time_t t = (time_t)IVAL(date_ptr,0); if (!null_mtime(t)) t += LocTimeDiff(t); return(t);}/***************************************************************************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));#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(void ){ static fstring TimeBuf; time_t t = time(NULL); struct tm *tm = LocalTime(&t); if (!tm) { slprintf(TimeBuf,sizeof(TimeBuf)-1,"%ld seconds since the Epoch",(long)t); } else {#ifdef HAVE_STRFTIME strftime(TimeBuf,100,"%Y/%m/%d %H:%M:%S",tm);#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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -