⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rvtime.c

📁 h.248协议源码
💻 C
📖 第 1 页 / 共 4 页
字号:
	tp->tv_nsec = (long)(((unsigned long long)ntime->fraction * 1000000000ULL) / 0x100000000ULL);
#endif
	return tp;
}

/* Convert an epoch time to a UTC calendar time (result) */
RvTm *rvTimeUTC(RvTime t, RvTm *result)
{
#if defined(RV_OS_WIN32)
	struct tm *resultptr;
#endif
	
	if (result == NULL) return NULL;

#if defined(RV_OS_SOLARIS) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_REDHAT) || defined(RV_OS_OSE)
	return gmtime_r((time_t *)&t, result);
#endif
#if defined(RV_OS_WIN32)
	resultptr = gmtime((time_t *)&t);
	memcpy(result, resultptr, sizeof(*result));
	return result;
#endif	
#if defined(RV_OS_VXWORKS)
	if(gmtime_r(&t, result) != OK) return NULL;
	return result;
#endif
#if defined(RV_OS_PSOS)
	/* In pSOS, gmtime is declared right but always returns NULL. */
	gmtime_r(&t, result);
	return result;
#endif
#if defined(RV_OS_NUCLEUS)
	/* Use our own gmtime since there's no standard one for Nucleus */
	return rvGmtime_r(t, result);
#endif
}

/* Convert an epoch time to a local calendar time (result) */
RvTm *rvTimeLocal(RvTime t, RvTm *result)
{
#if defined(RV_OS_WIN32)
	struct tm *resultptr;
#endif

	if (result == NULL) return NULL;

#if defined(RV_OS_SOLARIS) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_REDHAT) || defined(RV_OS_PSOS) || defined(RV_OS_OSE)
	return localtime_r((time_t *)&t, result);
#endif
#if defined(RV_OS_WIN32)
	resultptr = localtime((time_t *)&t);
	memcpy(result, resultptr, sizeof(*result));
	return result;
#endif	
#if defined(RV_OS_VXWORKS)
	if(localtime_r(&t, result) != OK) return(NULL);
	return result;
#endif
#if defined(RV_OS_NUCLEUS)
	/* Use our own localtime since there's no standard one for Nucleus */
	return rvLocaltime_r(t, result);
#endif
}

/* Convert calander time back to seconds since 1970. */
RvTime rvTimeConvertTm(RvTm *t)
{
	if(t == NULL) return -1;
	return (RvTime)mktime(t);
}

/* Convert a calander time to a standard time string. The buffer */
/* pointed to by buf MUST be large enough to handle the string */
/* generated. This should be a minimum of the value defined by */
/* the constant RV_TIME_ASCTIME_BUFSIZE. */
char *rvTimeAsctime(RvTm *t, char *buf, int bufsize)
{
#if defined(RV_OS_WIN32)
	char *bufptr;
#endif

#if defined(RV_OS_SOLARIS) || defined(RV_OS_OSE)
	return asctime_r(t, buf, bufsize);
#endif
#if defined(RV_OS_REDHAT) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_PSOS)
	if(bufsize < RV_TIME_ASCTIME_BUFSIZE) return NULL;
	return asctime_r(t, buf);
#endif
#if defined(RV_OS_WIN32)
	bufptr = asctime(t);
	strncpy(buf, bufptr, bufsize);
	return buf;
#endif	
#if defined(RV_OS_VXWORKS)
	asctime_r(t, buf, &bufsize);
	return buf;
#endif
#if defined(RV_OS_NUCLEUS)
	if(bufsize < RV_TIME_ASCTIME_BUFSIZE) return NULL;
	return rvAsctime_r(t, buf); /* use our own since we can't count on libs */
#endif
}

/* Allow custom formatting of a time string. Only use */
/* standard ANSI formats to insure OS comapatability. */
/* Returns the number of characters in the result string */
/* if successfull, otherwise returns a 0. */
int rvTimeStrTm(char *result, int maxsize, char *format, RvTm *t)
{
	return strftime(result, maxsize, format, t);
}

/* Subtract two times (in seconds). If newtime is actually */
/* older than oldtime, the result will be negative. */
#if !defined(rvTimeSubtractSecs) /* only use if no macro version */
RvTime rvTimeSubtractSecs(RvTime newtime, RvTime oldtime)
{
	return (newtime - oldtime);
}
#endif

/* Add two times (in seconds). Times that go past max time */
/* (year 2038) will produce invalid results. */
#if !defined(rvTimeAddSecs) /* only use if no macro version */
RvTime rvTimeAddSecs(RvTime time1, RvTime time2)
{
	return (time1 + time2);
}
#endif

/* Subtract two times (in seconds and nanoseconds). If newtime */
/* is actually older than oldtime the resulting seconds field */
/* will be negative. */
RvTimespec *rvTimeSubtract(RvTimespec *result, const RvTimespec *newtime, const RvTimespec *oldtime)
{
	if ((result == NULL) || (newtime == NULL) || (oldtime == NULL)) return NULL;
	result->tv_sec = newtime->tv_sec - oldtime->tv_sec;
	result->tv_nsec = newtime->tv_nsec - oldtime->tv_nsec;
	if(result->tv_nsec < 0) {
		result->tv_nsec = result->tv_nsec + 1000000000L;
		result->tv_sec = result->tv_sec - 1;
	}
	return (result);
}

/* Add two times (in seconds and nanoseconds). Times that */
/* go past max time (year 2038) will produce invalid results. */
RvTimespec *rvTimeAdd(RvTimespec *result, const RvTimespec *time1, const RvTimespec *time2)
{
	if ((result == NULL) || (time1 == NULL) || (time2 == NULL)) return NULL;
	result->tv_sec = time1->tv_sec + time2->tv_sec;
	result->tv_nsec = time1->tv_nsec + time2->tv_nsec;
	if(result->tv_nsec > 1000000000L) {
		result->tv_nsec = result->tv_nsec - 1000000000L;
		result->tv_sec = result->tv_sec + 1;
	}
	return (result);
}

/* Subtract two hires times (in nanoseconds). If newtime is */
/* actually older than oldtime, the result will be negative. */
#if !defined(rvTimeHrSubtract) /* only use if no macro version */
RvHrtime rvTimeHrSubtract(RvHrtime newtime, RvHrtime oldtime)
{
	return (newtime - oldtime);
}
#endif

/* Add two times (in seconds). Times that go past max time */
/* (year 2038) will produce invalid results. */
#if !defined(rvTimeHrAdd) /* only use if no macro version */
RvHrtime rvTimeHrAdd(RvHrtime time1, RvHrtime time2)
{
	return (time1 + time2);
}
#endif

/*$
{function:
	{name:  rvNtpTimeSubtract}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method subtracts two NTP time objects. NTP time starts at 1900 
            and can not go lower than that. Note that newtime MUST be larger 
            than oldtime since NTP time values are unsigned.}
	}
	{proto: RvNtpTime* rvNtpTimeSubtract(RvNtpTime* result, const RvNtpTime* newtime, const RvNtpTime* oldtime);}
	{params:
		{param: {n: result } {d: The RvNtpTime object to contain the results.}}
		{param: {n: newtime} {d: The left side of the minus operator.}}
		{param: {n: oldtime} {d: The right side of the minus operator.}}
	}
	{returns: A pointer to the RvNtpTime object containing the results.}
}
$*/
RvNtpTime *rvNtpTimeSubtract(RvNtpTime *result, const RvNtpTime *newtime, const RvNtpTime *oldtime)
{
	if((result == NULL) || (newtime == NULL) || (oldtime == NULL)) return NULL;

	result->secs = newtime->secs - oldtime->secs;
	result->fraction = newtime->fraction - oldtime->fraction;
	if(result->fraction > newtime->fraction)
		result->secs -= 1UL;
	return result;
}

/*$
{function:
	{name:  rvNtpTimeAdd}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method adds two NTP time objects. Beware 
            that NTP time wraps after year 2036.}
	}
	{proto: RvNtpTime* rvNtpTimeAdd(RvNtpTime* result, const RvNtpTime* newtime, const RvNtpTime* oldtime);}
	{params:
		{param: {n: result} {d: The RvNtpTime object to contain the results.}}
		{param: {n: newtime} {d: The left side of the plus operator.}}
		{param: {n: oldtime} {d: The right side of the plus operator.}}
	}
	{returns: A pointer to the RvNtpTime object containing the results.}
}
$*/
RvNtpTime *rvNtpTimeAdd(RvNtpTime *result, const RvNtpTime *newtime, const RvNtpTime *oldtime)
{
	if((result == NULL) || (newtime == NULL) || (oldtime == NULL)) return NULL;

	result->secs = newtime->secs + oldtime->secs;
	result->fraction = newtime->fraction + oldtime->fraction;
	if(result->fraction < newtime->fraction)
		result->secs += 1UL;
	return result;
}

/* Chop NTP resolution. Some applications require smaller NTP */
/* numbers than 32+32 bits. Seconds chop high order bits while */
/* fractions chop low order bits. */
RvNtpTime *rvNtpTimeChop(RvNtpTime *result, const RvNtpTime *ntime, int secbits, int fracbits)
{
	if((result == NULL) || (ntime == NULL) || (secbits < 0) || (secbits > 32) ||
	   (fracbits < 0) || (fracbits > 32)) return NULL;

	/* some CPUs are broken on 32 bit shifts so make it special */
	if(secbits < 32) {
		result->secs = ntime->secs & (~(0xFFFFFFFFUL << secbits));
	} else result->secs = ntime->secs;
	if(fracbits < 32) {
		result->fraction = ntime->fraction & (~(0xFFFFFFFFUL >> fracbits));
	} else result->fraction = ntime->fraction;
	return result;
}

/* Chop NTP resolution and put into a single 64 bit number. All */
/* resulting bits are placed into the low order bits of the 64 bit */
/* result (so the high order bits can be thrown out if needed. */
RvNtpTime64 rvNtpTimeChop64(RvNtpTime *ntime, int secbits, int fracbits)
{
	RvNtpTime result;
	RvNtpTime64 result64;

	/* Remove excess bits and do error checking */
	if(rvNtpTimeChop(&result, ntime, secbits, fracbits) == NULL)
		return (RvNtpTime64)0;

	result64 = ((RvNtpTime64)result.secs << fracbits) | ((RvNtpTime64)result.fraction >> (32 - fracbits));
	return result64;
}

/* Rebuild a standard NTP time from a 64 bit NTP value. Obviously the */
/* caller needs to indicate which bit number the decimal point is */
/* to the left of (thus 0 = no decimal). */
RvNtpTime *rvNtpTimeBuild64(RvNtpTime *result, RvNtpTime64 ntime, int decimal)
{

	if((result == NULL) || (decimal < 0) || (decimal > 64)) return NULL;

	/* some CPUs are broken on 64 bit shifts so make it special */
	if(decimal < 64) {
		result->secs = (unsigned long)(ntime >> decimal);
	} else result->secs = 0UL;
	if(decimal <= 32) {
		result->fraction = (unsigned long)(ntime << (32 - decimal));
	} else result->fraction = (unsigned long)(ntime >> (decimal - 32));
	return result;
}


/*$
{function:
	{name:  rvNtpTimeConstruct}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method constructs a NTP time object. }
	}
	{proto: RvNtpTime* rvNtpTimeConstruct(RvNtpTime* thisPtr, RvUint32 secs, RvUint32 fraction);}
	{params:
		{param: {n: thisPtr}  {d: The RvNtpTime object to construct.}}
		{param: {n: secs}     {d: The value to initialize the integer seconds to.}}
		{param: {n: fraction} {d: The value to initialize the fractional seconds to.}}
	}
	{returns: A pointer to the RvNtpTime object if successful, NULL otherwise.}
}
$*/

/*$
{function:
	{name:  rvNtpTimeConstructCopy}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p:  }
	}
	{proto: RvNtpTime* rvNtpTimeConstructCopy(RvNtpTime* thisPtr, const RvNtpTime* srcPtr, RvAllocator* allocatorPtr);}
	{params:
		{param: {n: thisPtr}      {d: The RvNtpTime object to construct.}}
		{param: {n: srcPtr}       {d: The RvNtpTime object to base the copy on.}}
		{param: {n: allocatorPtr} {d: The allocator to use.}}
	}
	{returns: A pointer to the RvNtpTime object if successful, NULL otherwise.}
}
$*/

/*$
{function:
	{name:  rvNtpTimeDestruct}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method destroys a NTP time object freeing all
            memory and resources it holds.}
	}
	{proto: void rvNtpTimeDestruct(RvNtpTime* thisPtr);}
	{params:
		{param: {n: thisPtr} {d: The RvNtpTime object to destruct.}}
	}
}
$*/

/*$
{function:
	{name:  rvNtpTimeCopy}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method copies a NTP time object into another NTP time
            object.}
	}
	{proto: void rvNtpTimeCopy(RvNtpTime* thisPtr, const RvNtpTime* srcPtr);}
	{params:
		{param: {n: thisPtr} {d: The RvNtpTime object to copy to.}}
		{param: {n: srcPtr}  {d: The RvNtpTime object to base the copy on.}}
	}
}
$*/

/*$
{function:
	{name:  rvNtpTimeSetSecs}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method sets the integer seconds part of the NTP time. }
	}
	{proto: void rvNtpTimeSetSecs(RvNtpTime* thisPtr, RvUint32 seconds);}
	{params:
		{param: {n: thisPtr} {d: The RvNtpTime object.}}
		{param: {n: seconds} {d: The new integer seconds value.}}
	}
}
$*/

/*$
{function:
	{name:  rvNtpTimeSetFraction}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method sets the fractional seconds part of the NTP time. }
	}
	{proto: void rvNtpTimeSetFraction(RvNtpTime* thisPtr, RvUint32 fraction);}
	{params:
		{param: {n: thisPtr}  {d: The RvNtpTime object.}}
		{param: {n: fraction} {d: The new fration seconds value.}}
	}
}
$*/

/*$
{function:
	{name:  rvNtpTimeGetSecs}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method returns the integer seconds part of the NTP
            time value.}
	}
	{proto: RvUint32 rvNtpTimeGetSecs(const RvNtpTime* thisPtr);}
	{params:
		{param: {n: thisPtr} {d: The RvNtpTime object.}}
	}
	{returns: The integer seconds part of the NTP time value.}
}
$*/
 
/*$
{function:
	{name:  rvNtpTimeGetFraction}
	{class: RvNtpTime }
	{include: rvtime.h}
	{description:
		{p: This method returns the fractional seconds part of the NTP
            time value.}
	}
	{proto: RvUint32 rvNtpTimeGetFraction(const RvNtpTime* thisPtr);}
	{params:
		{param: {n: thisPtr} {d: The RvNtpTime object.}}
	}
	{returns: The fraction part of the NTP time value.}
}
$*/

#if defined(RV_TEST_CODE)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -