Repository navigation
Open
Description
Currently in FIT we have timestamps which are 32bit and timestamp_16 which are 16bit difference from previous timestamp and are currently not read into nice values.
I wrote generator function based on ParseVivosmartHR. Here seems to be offical function how to do it (in Java):
mesgTimestamp += ( timestamp_16 - ( mesgTimestamp & 0xFFFF ) ) & 0xFFFF; It works but it definitely isn't written as it should, but I don't know fitparse enough to improve it.
#Input is what we get in FitFile.get_messages
def fix_times(messages):
timestamp=None
timestamp_16=None
real_time=0
UTC_REFERENCE = 631065600 # timestamp for UTC 00:00 Dec 31 1989
for message in messages:
#print (message)
#message["full_timestamp"]="TIMESTAMP"
field = message.get("timestamp")
if field is not None:
timestamp = field.raw_value
#I'm not sure if this is correct to set this to None. Without it records with just timestamp doesn't have same new timestamp anymore which could be a bug or it should be that way
timestamp_16 = None
tm_16 = message.get("timestamp_16")
if tm_16 is not None:
timestamp_16 = tm_16.raw_value
#print(timestamp, timestamp_16)
if timestamp_16 is None:
ts_value = timestamp
else:
new_time = int( timestamp/2**16) * 2**16 + timestamp_16
if new_time >= real_time:
real_time = new_time
else:
real_time = new_time + 2**16
ts_value = real_time
if ts_value is not None and ts_value >= 0x10000000:
value = datetime.datetime.utcfromtimestamp(UTC_REFERENCE
+ ts_value)
else:
value = None
message.fields.append(
FieldData(
field_def=None,
field=FIELD_TYPE_TIMESTAMP_1,
parent_field=None,
value=value,
raw_value=ts_value
))
yield messageMetadata
Metadata
Assignees
Labels
No labels
Activity
pR0Ps commentedon Dec 1, 2017
As an FYI for testing this there is already a file that has these
timestamp_16fields in the repo:tests/files/MonitoringFile.fitdanpatdav commentedon Nov 18, 2018
Any progress on this issue? I am struggling to make sense of heartrate records which utilize timestamp_16.
brandonStell commentedon Dec 29, 2018
Hi @buma, I seem to be doing something wrong. How should fix_times be called?
danpatdav commentedon Dec 30, 2018
Happy to test any updated code.
francescolosterzo commentedon Aug 22, 2019
Hello! Happy to see this discussed here :)
I am struggling with the same problem, and for me the formula in the top post doesn't work! Following the instructions from here, I tried to use that formula by hand in order to see if things made sense.
I downloaded my heart rate data recorded on May 1st 2019 from the Garmin Connect web page, i.e. the first data point is at 12AM.
Looping through the
FitFile.get_messages(), the first heart-rate-related entry I find is this, including the message right before it:Following the instructions linked above, I set
but as a result of the above formula I get
which is wrong!
Has anyone found a fix to this?
francescolosterzo commentedon Aug 23, 2019
I tried to look at things in detail going step by step in the above formula:
and, as before the result is wrong by more than 12 hours.
(note that
step2is negative!)Also, the instructions say
but the final result is not a 32-bit variable in which the first 16 are from the original
mesgTimestampand the last 16 are fromtimestamp_16, some bits are flipped!On the other hand I tried to build it by hand, and I get the final binary variable as:
which then translates to this datetime:
which, again, is wrong :\ but different from before!
tuxinaut commentedon Sep 4, 2019
Does this maybe help?
adjust_time.R
francescolosterzo commentedon Sep 5, 2019
thanks @tuxinaut ! I tried it but I still get a sizeable difference, i.e.:
datetime.datetime(2019, 4, 30, 18, 36, 44)On the other hand I browsed the details of the code and I see that the UTC offset shift due to Garmin's different starting time is applied to
timestamp, but I cannot find anything similar fortimestamp_16.Following this hypothesis I tried to work with raw
timestamp*values (I guess they are the values before this UTC offset correction) and here is what I get:The result is
datetime.datetime(2019, 4, 30, 23, 1)[here is the full notebook, just search for "925596000"]
This is the closest I got so far to the right result, and it is shifted only by one hour (the minute might be due to the granularity of the data) which might now be a timezone related issue.
What do you think @dtcooper ?
tuxinaut commentedon Sep 5, 2019
@francescolosterzo really cool work 👍 and yes I'm pretty sure that's timezone related (take a look on the code below)
mszczepanczyk commentedon Oct 25, 2023
This is what I came up with after reading this thread. The generated timestamps and bpms are in line with the data in Garming Connect.