[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-dev] Timestamps on OSC bundles



Hi James and all.

Should OSC bundles be timestamped based on local time or GMT?

If it should be GMT, i'm appending a modified version of
syncOSCOffsetWithTimeOfDay that does this:

- sekhar

PyrSched.cpp:356

void syncOSCOffsetWithTimeOfDay()
{
	// generate a value gHostOSCoffset such that  
	// (gHostOSCoffset + systemTimeInOSCunits) 
	// is equal to gettimeofday time in OSCunits.
	// Then if this machine is synced via NTP, we are synced with the world.
	// more accurate way to do this??

	struct timeval tv;
    struct timezone tz;
	
	int64 systemTimeBefore, systemTimeAfter, diff;
	int64 minDiff = 0x7fffFFFFffffFFFFLL;
	
	// take best of several tries
	const int numberOfTries = 8;
	int64 newOffset = gHostOSCoffset;
	for (int i=0; i<numberOfTries; ++i) {
		systemTimeBefore = AudioGetCurrentHostTime();
		gettimeofday(&tv, &tz);
		systemTimeAfter = AudioGetCurrentHostTime();
		
		diff = systemTimeAfter - systemTimeBefore;
		if (diff < minDiff) {
			minDiff = diff;
			// assume that gettimeofday happens halfway between AudioGetCurrentHostTime calls
			int64 systemTimeBetween = systemTimeBefore + diff/2;
			int64 systemTimeInOSCunits = (int64)((double)AudioConvertHostTimeToNanos(systemTimeBetween) * kNanosToOSC);
// Doesn't account for time zone or Daylight Savings Time
//			int64 timeOfDayInOSCunits  = ((int64)(tv.tv_sec + kSECONDS_FROM_1900_to_1970) << 32) 
//                                            + (int64)(tv.tv_usec * kMicrosToOSC);

			int64 secondsSince1900 = tv.tv_sec + kSECONDS_FROM_1900_to_1970 // time in local timezone
				- (60 * tz.tz_minuteswest)     // correction for timezone
				+ (tz.tz_dsttime ? 3600 : 0);  // correction for Daylight Savings Time
			int64 timeOfDayInOSCunits = (secondsSince1900 << 32) + (int64)(tv.tv_usec * kMicrosToOSC);
											
			newOffset = timeOfDayInOSCunits - systemTimeInOSCunits;
		}
	}
	
	gHostOSCoffset = newOffset;
	//postfl("gHostOSCoffset %016llX\n", gHostOSCoffset);
}


--
C. Ramakrishnan        cramakrishnan@xxxxxxx