[6783] in Kerberos

home help back first fref pref prev next nref lref last post

even better(?) krb5_us_timeofday idea

daemon@ATHENA.MIT.EDU (Jim Miller)
Wed Feb 28 01:51:00 1996

From: jim@bilbo.suite.com (Jim Miller)
Date: Wed, 28 Feb 96 00:44:20 -0600
To: krb5-bugs@MIT.EDU
Cc: kerberos@MIT.EDU
Reply-To: Jim_Miller@bilbo.suite.com


How about this instead (might need to adjust indentation):

static struct timeval last_tv = {0, 0};
static krb5_int32 last_tv_count = 0;

#define tv_eq(t1, t2)	( ((t1).tv_sec == (t2).tv_sec) &&      \
					  ((t1).tv_usec == (t2).tv_usec) )

#define tv_lt(t1, t2)		( ((t1).tv_sec < (t2).tv_sec) ||      \
					   ( ((t1).tv_sec == (t2).tv_sec) &&      \
					     ((t1).tv_usec < (t2).tv_usec) ) )

#define tv_gt(t1, t2)	( ((t1).tv_sec > (t2).tv_sec) ||      \
					  ( ((t1).tv_sec == (t2).tv_sec) &&      \
					    ((t1).tv_usec > (t2).tv_usec) ) )

#define tv_le(t1, t2)	(tv_lt(t1,t2) || tv_eq(t1, t2))
#define tv_ge(t1, t2)	(tv_gt(t1,t2) || tv_eq(t1, t2))

krb5_error_code
krb5_us_timeofday(seconds, microseconds)
register krb5_int32 *seconds, *microseconds;
{
    struct timeval tv;
    struct timeval pseudo_time;

    if (gettimeofday(&tv, (struct timezone *)0) == -1) {
	/* failed, return errno */
	return (krb5_error_code) errno;
    }

    pseudo_time = last_tv;
    pseudo_time. tv_usec += (++last_tv_count);
    if (pseudo_time. tv_usec >= 1000000) {
	// tv_usec wrapped around
	pseudo_time. tv_usec = 0;
	pseudo_time. tv_sec++;
    }

    if ( tv_ge(tv, last_tv) &&
	 tv_le(tv, pseudo_time) ) {
	    tv = pseudo_time;

    } else if ( tv_lt(tv, last_tv) ||
	           tv_gt(tv, pseudo_time) ) {
	    last_tv = tv;
	    last_tv_count = 0;
    }

    *seconds = tv.tv_sec;
    *microseconds = tv.tv_usec;
    return 0;
}


I haven't actually tried it, but I think it should cover all cases.  The only way to get the  
same time from the function is if somebody sets the clock back while you are running.   
Other than that, you will get a value that is at least one u-sec greater that all previous  
times obtained since starting the process.

Jim_Miller@suite.com

home help back first fref pref prev next nref lref last post