Working with Date Time Values

The FIT Profile defines the date_time type as an uint32 that represents the number of seconds since midnight on December 31, 1989 UTC*. This date is often referred to as the FIT Epoch.

If you have used the FitCSVTool to convert FIT files to text, then you have already seen FIT Epoch values. In the following CSV snippet the time_created, timestamp, and start_time fields are all FIT Epoch values.

Data,0,file_id,time_created,"938622559",,m
Data,3,record,timestamp,"938622574",s,distance,"54.14",m,speed,"4.759",m/s
Data,3,record,timestamp,"938622579",s,distance,"77.79",m,speed,"4.73",m/s
Data,3,record,timestamp,"938622584",s,distance,"101.98",m,speed,"4.838",m/s
Data,2,session,timestamp,"938622584",s,start_time,"938622559",s
Data,1,activity,timestamp,"938622584",s

Most programming languages provide a way to create dates based on the Unix Epoch, which is 1970–01–01T00:00:00Z.

C#
public static DateTimeOffset FromUnixTimeSeconds (long seconds);

Java
Date(long milliseconds)

Swift
Date(timeIntervalSinceReferenceDate ti: TimeInterval)

Objective-C
[NSDate initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti];

JavaScript
Date(milliseconds)

To use FIT Epoch values with any of these methods, the value needs to be offset by either 631065600 seconds or 631065600000 milliseconds, depending on the method.

FIT Epoch - Unix Epoch = 631065600000 milliseconds = ~20 years

Using this offset and the JavaScript Date class, a method can be written for converting FIT date_time values to JavaScript Dates.

function dateFromFitDateTime(seconds) {
	const OFFSET_MS = 631065600000; // Offset between the FIT Epoch and Unix Epoch
	return new Date(seconds * 1000 + OFFSET_MS);
}

Try It!

Sun, 31 Dec 1989 00:00:00 GMT

Sat Dec 30 1989 17:00:00 GMT-0700 (Mountain Standard Time)

## FIT SDK DateTime Class

The FIT SDK DateTime class handles the conversion between date_time values and the language’s own Date class. The FIT DateTime class is available in the Java, C#, and Objective-C SDKs.

// Get the start time of the session as a FIT DateTime object
Dynastream.Fit.DateTime fitDate = session.GetStartTime();

// Get the start time of the session as the number of seconds since the FIT Epoch
uint timestamp = session.GetStartTime().GetTimeStamp();

// Get the start time of the session as a C# DateTime object
System.DateTime systemDate = session.GetStartTime().GetDateTime();

Local Date Time

The FIT SDK also defines a local_date_time type as an uint32, which represents the number of seconds since midnight on December 31, 1989 local time. When both a date_time value and corresponding local_date_time value are provided, the time zone offset for the FIT file can be calculated.

var tzOffsetHours = (activity.GetLocalTimestamp() - (int)activity.GetTimestamp().GetTimeStamp()) / 3600

*date_time values may not always represent the number of seconds since the FIT Epoch. If a date_time value is less than 0x10000000, then the value represents a relative number of seconds. The constant 0x10000000 is defined in the FIT Profile as date_time.min, and is available as a constant in each FIT SDK. The same is true for local_date_time_values.