Added Time methods for 32bit NTP time conversion.

git-svn-id: http://yate.null.ro/svn/yate/trunk@6338 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2019-01-14 09:19:22 +00:00
parent 671478a79a
commit f3a58864c0
2 changed files with 62 additions and 0 deletions

View File

@ -112,6 +112,12 @@ namespace TelEngine {
#define REFOBJECT_MUTEX_COUNT 47
#endif
// Number of seconds from 1900 to 1970
#define SECONDS_1900_TO_1970 2208988800u // 0x83AA7E80
// UNIX time of NTP time 07 Feb. 2036 6h 28m 16s
// NTP time will overflow at this UNIX time
#define UNIX_NTP_MAX 2085978496u // 0x7C558180
static int s_debug = DebugDef;
static int s_indent = 0;
static bool s_debugging = true;
@ -833,6 +839,34 @@ bool Time::toDateTime(unsigned int epochTimeSec, int& year, unsigned int& month,
return true;
}
uint32_t Time::toNtp(uint32_t sec, uint32_t* over, bool rfc2030)
{
if (sec < UNIX_NTP_MAX)
return sec + SECONDS_1900_TO_1970;
if (rfc2030) {
sec -= UNIX_NTP_MAX;
if (sec <= 0x7fffffff)
return sec;
if (over)
*over = sec - 0x7fffffff;
return 0x7fffffff;
}
if (over)
*over = (uint32_t)(((uint64_t)sec + SECONDS_1900_TO_1970) - 0xffffffff);
return 0xffffffff;
}
uint32_t Time::fromNtp(uint32_t val, uint32_t* under, bool rfc2030)
{
if (rfc2030 && val <= 0x7fffffff)
return val + UNIX_NTP_MAX;
if (val >= SECONDS_1900_TO_1970)
return val - SECONDS_1900_TO_1970;
if (under)
*under = SECONDS_1900_TO_1970 - val;
return 0;
}
int Time::timeZone()
{
#ifdef _WINDOWS

View File

@ -3773,6 +3773,34 @@ public:
unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& sec,
unsigned int* wDay = 0);
/**
* Convert system time to 32bit NTP (seconds since 1900)
* @param sec Time in seconds
* @param over Optional destination for overflow value. Must be reset before calling this method
* @param rfc2030 Use time extension as specified in RFC 2030 Section 3
* @return NTP time value in seconds
*/
static uint32_t toNtp(uint32_t sec, uint32_t* over = 0, bool rfc2030 = true);
/**
* Convert this time to 32bit NTP (seconds since 1900)
* @param over Optional destination for overflow value. Must be reset before calling this method
* @param rfc2030 Use time extension as specified in RFC 2030 Section 3
* @return NTP time value in seconds
*/
inline uint32_t toNtp(uint32_t* over = 0, bool rfc2030 = true)
{ return toNtp(sec(),over,rfc2030); }
/**
* Convert 32bit NTP (seconds since 1900) to system time
* @param val NTP time in seconds
* @param under Optional destination for underflow value (given time is before EPOCH).
* Must be reset before calling this method
* @param rfc2030 Handle time extension as specified in RFC 2030 Section 3
* @return System time value in seconds. 0 if underflow
*/
static uint32_t fromNtp(uint32_t val, uint32_t* under = 0, bool rfc2030 = true);
/**
* Check if an year is a leap one
* @param year The year to check