:i	static char d_time_sccs_id[] = "%W% %G%";
:1#include "V.h"
/*
* This module contains some useful timestamp routines.  d_time() is the debug
* wrapper around the Unix time() routine, which is no longer a system call on
* most extant Unices.  The Vtime()  routine  is  widely  called  to  get  a
* printfable version of the current timestamp. You can also call Vtimstr(t)
* to get an arbitrary timestamp converted to ASCII, but note  that  doing  so
* overwrites Vtime's buffer, Vtims.
*/
extern TIMV Vtimev;
extern TIMZ Vtimez;
extern char *ctime();
extern struct tm *gmtime();

/*
* Debug wrapper for the time() system call.  Actually, this  has  been
* converted  to  call Gettimeofday(), which is the real system call on
* most Unices now.  This was done primarily so we can easily hunt down
* all calls to get the time.
*/
time_t d_time(t)
	time_t*t;
{	time_t v;
	char  *p;
:8	V8 "time(%08X)",t D;
	v = Gettimeofday(&Vtimev,&Vtimez);
	v = Vtimev.tv_sec;
	if (t) *t = v;
	if (Vlvl > 4) {
		p = ctime(&v);
		p[24] = 0;
:7		V7 "time(%08X)=%u=%s",t,v,p D;
	}
	return v;
}

/*
* This routine returns a printable version of  the  current  date+time  in  a
* standardized, compact format. It leaves the string in the Vtimp variable,
* and the Vtiml variable contains the 32-bit Unix time() value.
*/
char* Vtime()
{	char *ctime();
	struct tm *gmtime();

	Gettimeofday(&Vtimev, &Vtimez);

	return Vtimstr(Vtimev.tv_sec);
}

/*
* This routine returns a  printable  date+time  in  a  standardized,  compact
* format.   It  leaves  the  string  in the Vtimp variable, and the Vtiml
* variable contains the 32-bit Unix time()  value  that  was  passed  to  us.
* Multiple  calls  of  this routine will overwrite the Vtims array where we
* build the string.  We set up more than one pointer into the ASCII time, and
* return  one  of  them; which one may vary from time to time, but Vtimp is
* always the same as the last string we returned.
*/
char* Vtimstr(t)
	time_t t;
{	char *ctime();
	struct tm *gmtime();

	Vtimep = Vtimezone? localtime(&t): gmtime(&t);
	sprintf(Vtims,"%4d/%02d/%02d %02d:%02d:%02d",
		Vtimep->tm_year+1900,
		Vtimep->tm_mon+1,
		Vtimep->tm_mday,
		Vtimep->tm_hour,
		Vtimep->tm_min,
		Vtimep->tm_sec);
	Vtimp =	/* Whichever time you want as the default */
	Vtimy = Vtims + 2;	/* Year (2-digit) */
	Vtimm = Vtims + 5;	/* Month */
	Vtimh = Vtims +11;	/* Hour */
	return Vtimh;
}
