Skip to content

Instantly share code, notes, and snippets.

@polyvertex
Last active May 25, 2018 07:58
Show Gist options
  • Save polyvertex/24b119b7f7105fbfe6861e70a42f6786 to your computer and use it in GitHub Desktop.
Save polyvertex/24b119b7f7105fbfe6861e70a42f6786 to your computer and use it in GitHub Desktop.
Imitate the file naming of Garmin devices
import datetime
import string
def name_file(dt):
"""
Return a name according to the given `datetime.datetime` object.
This is how Garmin devices seem to name their activity files, as explained
`here <https://forums.garmin.com/forum/into-sports/running/forerunner-220-aa/53766->`_.
Quote::
I think i have worked out the activity naming for the .FIT files.
I did a run on 16 Feb 2014 at 7:40PM (OZ DST Time).
The .FIT file is "42GJ4011.FIT"
The first 4 characters are easy. 1 to 9, A towards Z (A=10, B=11...,
G=16).
The last 4 digits are the last 4 digits (with in a few 10s of seconds)
of the number of seconds since 00:00 31 Dec 1989.
Its not exactly the time the watch was put into GPS mode or the time the
START button was pushed but its close.
File create time = 761474409
START time = 761474411
End time = 761474969
4 2 G J 4 0 1 1
Year Month Date Hour << last 4 digits of number of seconds since 00:00 Dec 31 1989 UTC >>
4=2014 2=Feb G=16 7=7
* Year
1=2011
2=2012
3=2013
4=2014
5=2015
...
* Month:
1=Jan 7=Ju1
2=Feb 8=Aug
3=Mar 9=Sep
4=Apr A=Oct
5=May B=Nov
6=Jun C=Dec
* Date
1=1 9=9 H=17 P=25
2=2 A=10 I=18 Q=26
3=3 B=11 J=19 R=27
4=4 C=12 K=20 S=28
5=5 D=13 L=21 T=29
6=6 E=14 M=22 U=30
7=7 F=15 N=23 V=31
8=8 G=16 O=24
* Hour
1=1
2=2
...
"""
table = string.digits + string.ascii_uppercase
epoch = datetime.datetime(
year=1989, month=12, day=31,
tzinfo=datetime.timezone.utc if dt.tzinfo else None)
name = str(dt.year - 2010)
name += table[dt.month]
name += table[dt.day]
name += table[dt.hour]
name += str(int((dt - epoch).total_seconds()))[-4:]
return name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment