Example #1
0
TimeStamp TimeStamp::getCurrentTimeStamp()
{
	TimeStamp result;

	// NS: We round generated timestamps to whole millisecond.
	// Not many applications can deal with fractional milliseconds properly and
	// we do not use high resolution timers either so actual time granularity
	// is going to to be somewhere in range between 1 ms (like on UNIX/Risc)
	// and 53 ms (such as Win9X)

	time_t seconds; // UTC time
	int milliseconds;

#ifdef HAVE_GETTIMEOFDAY
	struct timeval tp;
	GETTIMEOFDAY(&tp);
	seconds = tp.tv_sec;
	milliseconds = tp.tv_usec / 1000;
#else
	struct timeb time_buffer;
	ftime(&time_buffer);
	seconds = time_buffer.time;
	milliseconds = time_buffer.millitm;
#endif

	// NS: Current FB behavior of using server time zone is not appropriate for
	// distributed applications. We should be storing UTC times everywhere and
	// convert timestamps to client timezone as necessary. Replace localtime stuff
	// with these lines as soon as the appropriate functionality is implemented
	//
	// mValue.timestamp_date = seconds / 86400 + GDS_EPOCH_START;
	// mValue.timestamp_time = (seconds % 86400) * ISC_TIME_SECONDS_PRECISION;

	const int fractions = milliseconds * ISC_TIME_SECONDS_PRECISION / 1000;

#ifdef HAVE_LOCALTIME_R
	struct tm times;
	if (!localtime_r(&seconds, &times))
	{
		report_error("localtime_r");
		return result;
	}

	result.encode(&times, fractions);
#else
	struct tm *times = localtime(&seconds);
	if (!times)
	{
		report_error("localtime");
		return result;
	}

	result.encode(times, fractions);
#endif

	return result;
}