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

📄 rvtime.c

📁 基于h323协议的软phone
💻 C
字号:
/***********************************************************************
Filename   : rvtime.c
Description: basic time structure definition and manipulation
************************************************************************
        Copyright (c) 2001 RADVISION Inc. and RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Inc. and RADVISION Ltd.. No part of this document may be
reproduced in any form whatsoever without written prior approval by
RADVISION Inc. or RADVISION Ltd..

RADVISION Inc. and RADVISION Ltd. reserve the right to revise this
publication and make changes without obligation to notify any person of
such revisions or changes.
***********************************************************************/
#include "rvstdio.h"
#include "rvtime.h"

/* Create a timespec from base information: seconds and nanoseconds. */
/* The nanoseconds value must be valid ( > 0, < 1 second). */
RVCOREAPI RvTime * RVCALLCONV RvTimeConstruct(RvTime *t, RvInt32 secs, RvInt32 nanosecs)
{
#if defined(RV_NULLCHECK)
    if(t == NULL) return NULL;
#endif
#if defined(RV_RANGECHECK)
    if((nanosecs < 0) || (nanosecs >= RV_TIME_NSECPERSEC)) return NULL;
#endif

    t->sec = secs;
    t->nsec = nanosecs;
    return t;
}

/* Create a timespec from base information: nanoseconds */
RVCOREAPI RvTime * RVCALLCONV RvTimeConstructFrom64(RvTime *t, RvInt64 nanosecs)
{
#if defined(RV_NULLCHECK)
    if(t == NULL) return NULL;
#endif

    t->sec = (RvInt32)Rv64Divide(nanosecs, RV_TIME64_NSECPERSEC);
    t->nsec = (RvInt32)Rv64Modulu(nanosecs, RV_TIME64_NSECPERSEC);
    return t;
}

/* Subtract two times (newtime - oldtime). If newtime is actually */
/* older than oldtime the resulting seconds field will be negative. */
RvTime *RvTimeSubtract(RvTime *result, const RvTime *newtime, const RvTime *oldtime)
{
#if defined(RV_NULLCHECK)
    if ((result == NULL) || (newtime == NULL) || (oldtime == NULL)) return NULL;
#endif

    result->sec = newtime->sec - oldtime->sec;
    result->nsec = newtime->nsec - oldtime->nsec;
    if(result->nsec < RvInt32Const(0)) {
        result->nsec += RV_TIME_NSECPERSEC;
        result->sec -= RvInt32Const(1);
    }
    return result;
}

/* Add two times. Times that go past max time (year 2038) will */
/* produce invalid results. */
RvTime *RvTimeAdd(RvTime *result, const RvTime *time1, const RvTime *time2)
{
#if defined(RV_NULLCHECK)
    if ((result == NULL) || (time1 == NULL) || (time2 == NULL)) return NULL;
#endif

    result->sec = time1->sec + time2->sec;
    result->nsec = time1->nsec + time2->nsec;
    if(result->nsec >= RV_TIME_NSECPERSEC) {
        result->nsec -= RV_TIME_NSECPERSEC;
        result->sec += RvInt32Const(1);
    }
    return result;
}

/* Convert a RvTime time to a 64 bit number containing the equivalent */
/* total number of nanoseconds. */
RvInt64 RvTimeConvertTo64(const RvTime *t)
{
    RvInt64 result;

#if defined(RV_NULLCHECK)
    if(t == NULL) return RvInt64Const(0);
#endif

    result = Rv64Multiply((RvInt64)t->sec, RV_TIME64_NSECPERSEC) + (RvInt64)t->nsec;
    return result;
}

#if defined(RV_TEST_CODE)
#include "rvstdio.h"
#include "rv64ascii.h"

#define TESTTIME_SEC1 997380342  /* seconds since 1970 */
#define TESTTIME_NSEC1 750000000 /* in nanoseconds */

#define TESTTIME_SEC2 998061863  /* seconds since 1970 */
#define TESTTIME_NSEC2 17578125 /* in nanoseconds */

void RvTimeTest(void)
{
    RvTime t1, t2, t3, t4, *tresult;
    RvInt64 time64;
    RvChar buf[RV_64TOASCII_BUFSIZE];

    RvPrintf("Starting test of rvntptime.\n");
    RvPrintf("Time#1: Secs=%d NSecs=%d\n", TESTTIME_SEC1, TESTTIME_NSEC1);
    RvPrintf("Time#2: Secs=%d NSecs=%d\n", TESTTIME_SEC2, TESTTIME_NSEC2);

    RvPrintf("RvTimeConstruct Time#2: ");
    tresult = RvTimeConstruct(&t1, TESTTIME_SEC2, TESTTIME_NSEC2);
    if(tresult != NULL) {
        RvPrintf("%d %d\n", RvTimeGetSecs(tresult), RvTimeGetNsecs(tresult));
    } else RvPrintf("ERROR!\n");

    time64 = RvTimeConvertTo64(&t1);
    Rv64toA(buf, time64);
    RvPrintf("RvTimeConvertTo64 Time#2: %s\n", buf);

    RvPrintf("RvTimeConstructFrom64 Time#2: ");
    tresult = RvTimeConstructFrom64(&t2, time64);
    if(tresult != NULL) {
        RvPrintf("%d %d\n", RvTimeGetSecs(tresult), RvTimeGetNsecs(tresult));
    } else RvPrintf("ERROR!\n");

    RvTimeSetSecs(&t1, TESTTIME_SEC1);
    RvTimeSetNsecs(&t1, TESTTIME_NSEC1);
    RvPrintf("RvTimeSetSecs and RvTimeSetFraction Time#1: %d %d\n", RvTimeGetSecs(&t1), RvTimeGetNsecs(&t1));

    RvPrintf("RvTimeSubtract Time#3 = Time#2 - Time#1: ");
    tresult = RvTimeSubtract(&t3, &t2, &t1);
    if(tresult != NULL) {
        RvPrintf("%d %d\n", RvTimeGetSecs(tresult), RvTimeGetNsecs(tresult));
    } else RvPrintf("ERROR!\n");

    RvPrintf("RvTimeAdd Time#4 = Time#3 + Time#1 = Time#2: ");
    tresult = RvTimeAdd(&t4, &t3, &t1);
    if(tresult != NULL) {
        RvPrintf("%d %d\n", RvTimeGetSecs(tresult), RvTimeGetNsecs(tresult));
    } else RvPrintf("ERROR!\n");
}

#endif /* RV_TEST_CODE */

⌨️ 快捷键说明

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