Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Import: read localized month names in CSV dates
sTOrM2202f0f9ab06b6ec
diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.jsindex 114529ef1..4a5edb238 100644--- a/frontend/src/lib/import-csv.js+++ b/frontend/src/lib/import-csv.js@@ -320,17 +320,45 @@ const effortNum = (raw, zeroMeansRated) => {}const LB_TO_KG = 0.45359237const p2 = n => String(n).padStart(2, '0')-const MON = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12 }+// Month names as the exporting app wrote them. Hevy and Strong localize the date on every+// row to the language the app was set to, so an English-only table silently dropped seven+// months of a French history: `févr.`/`août`/`déc.` never even matched, because the accent+// falls inside the first three letters, and `avr.`/`mai`/`juin`/`juil.` matched the shape+// but stood for nothing. Only janv./mars/sept./oct./nov. came through, by looking exactly+// like their English counterparts. Keys are lower-case and stripped of diacritics.+const MON = {+ // English+ jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12,+ // French — juin/juil. differ only at the fourth letter, which is why the lookup below+ // tries the whole word and a 4-letter prefix before falling back to three.+ janv: 1, fevr: 2, mars: 3, avri: 4, avr: 4, mai: 5, juin: 6, juil: 7, aout: 8,+ sept: 9, octo: 10, nove: 11, dece: 12,+ // Spanish · Portuguese+ ene: 1, fev: 2, abr: 4, ago: 8, set: 9, out: 10, dic: 12,+ // German · Dutch+ mrz: 3, mei: 5, okt: 10, dez: 12,+ // Italian+ gen: 1, mag: 5, giu: 6, lug: 7, ott: 10,+}++/** A month word in any of the languages above -> 1-12, or null. */+const monthOf = w => {+ const k = String(w || '').normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase()+ return MON[k] || MON[k.slice(0, 4)] || MON[k.slice(0, 3)] || null+}-/** "2020-12-30 18:51:52" · "2024-03-07" · "2024/03/07" · "2024.03.07" · "22 Dec 2025, 08:00" · "07/03/2024" -> { d, t } */+/** "2020-12-30 18:51:52" · "2024-03-07" · "2024/03/07" · "2024.03.07" · "22 Dec 2025, 08:00" · "21 août 2024, 18:00" · "07/03/2024" -> { d, t } */export function parseWhen(s) {const v = String(s || '').trim()let m = v.match(/^(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})(?:[T ](\d{1,2}):(\d{2}))?/)if (m) return { d: `${m[1]}-${p2(m[2])}-${p2(m[3])}`, t: hm(m[4], m[5]) }- m = v.match(/^(\d{1,2})\s+([A-Za-z]{3})[a-z]*\.?\s+(\d{4})(?:,?\s+(\d{1,2}):(\d{2}))?/)- if (m && MON[m[2].toLowerCase()]) return { d: `${m[3]}-${p2(MON[m[2].toLowerCase()])}-${p2(m[1])}`, t: hm(m[4], m[5]) }- m = v.match(/^([A-Za-z]{3})[a-z]*\.?\s+(\d{1,2}),?\s+(\d{4})(?:,?\s+(\d{1,2}):(\d{2}))?/)- if (m && MON[m[1].toLowerCase()]) return { d: `${m[3]}-${p2(MON[m[1].toLowerCase()])}-${p2(m[2])}`, t: hm(m[4], m[5]) }+ // \p{L} rather than [A-Za-z]: the month word carries an accent in most languages.+ m = v.match(/^(\d{1,2})\s+(\p{L}{3,})\.?\s+(\d{4})(?:,?\s+(\d{1,2}):(\d{2}))?/u)+ let mon = m && monthOf(m[2])+ if (mon) return { d: `${m[3]}-${p2(mon)}-${p2(m[1])}`, t: hm(m[4], m[5]) }+ m = v.match(/^(\p{L}{3,})\.?\s+(\d{1,2}),?\s+(\d{4})(?:,?\s+(\d{1,2}):(\d{2}))?/u)+ mon = m && monthOf(m[1])+ if (mon) return { d: `${m[3]}-${p2(mon)}-${p2(m[2])}`, t: hm(m[4], m[5]) }// Day-first when ambiguous: FitNotes/Strong/Hevy all write unambiguous dates, so a// bare numeric one came through a spreadsheet, and those are usually European.m = v.match(/^(\d{1,2})[/.](\d{1,2})[/.](\d{4})(?:[, ]+(\d{1,2}):(\d{2}))?/)diff --git a/frontend/src/lib/import-csv.test.js b/frontend/src/lib/import-csv.test.jsindex a7fd749de..2df6ad480 100644--- a/frontend/src/lib/import-csv.test.js+++ b/frontend/src/lib/import-csv.test.js@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'-import { parseWorkoutCSV, mergeImport } from './import-csv.js'import { workoutVolume } from './history.js'+import { parseWhen, parseWorkoutCSV, mergeImport } from './import-csv.js'const CSV = ['Date,Exercise,Weight,Reps,Set Type',@@ -105,3 +105,70 @@ describe('mergeImport and custom exercises', () => {expect(S.workouts[0].entries.map(e => e.id)).toEqual(['mine', '0025'])})})++// Hevy (and Strong) localize the date on every CSV row to the language the app was set to.+// A French history imported as five months out of twelve: janv./mars/sept./oct./nov. happen+// to look English, while `févr.`/`août`/`déc.` carry an accent inside the first three letters+// and `avr.`/`mai`/`juin`/`juil.` are simply other words. Every rejected row was dropped as+// `skipped`, so 778 Hevy workouts came in as 323.+const FR_MONTHS = [+ ['20 janv. 2024, 18:00', '2024-01-20'],+ ['12 févr. 2024, 18:00', '2024-02-12'],+ ['3 mars 2024, 18:00', '2024-03-03'],+ ['8 avr. 2024, 18:00', '2024-04-08'],+ ['14 mai 2024, 18:00', '2024-05-14'],+ ['2 juin 2024, 18:00', '2024-06-02'],+ ['9 juil. 2024, 18:00', '2024-07-09'],+ ['21 août 2024, 18:00', '2024-08-21'],+ ['5 sept. 2024, 18:00', '2024-09-05'],+ ['7 oct. 2024, 18:00', '2024-10-07'],+ ['11 nov. 2024, 18:00', '2024-11-11'],+ ['23 déc. 2024, 18:00', '2024-12-23'],+]++describe('localized month names', () => {+ it('reads all twelve French months', () => {+ for (const [input, day] of FR_MONTHS) {+ expect(parseWhen(input), input).toMatchObject({ d: day })+ }+ })++ // juin and juil. share their first three letters, so a 3-letter key cannot tell them apart.+ it('tells June from July past the third letter', () => {+ expect(parseWhen('2 juin 2024').d).toBe('2024-06-02')+ expect(parseWhen('2 juillet 2024').d).toBe('2024-07-02')+ expect(parseWhen('2 giugno 2024').d).toBe('2024-06-02')+ expect(parseWhen('2 luglio 2024').d).toBe('2024-07-02')+ })++ it('still reads English, month-first and numeric dates', () => {+ expect(parseWhen('18 Sep 2022, 10:30')).toMatchObject({ d: '2022-09-18' })+ expect(parseWhen('22 Dec 2025, 08:00')).toMatchObject({ d: '2025-12-22' })+ expect(parseWhen('Aug 8, 2026')).toMatchObject({ d: '2026-08-08' })+ expect(parseWhen('2026-08-08')).toMatchObject({ d: '2026-08-08' })+ expect(parseWhen('07/03/2024')).toMatchObject({ d: '2024-03-07' })+ })++ // A word that is not a month must not be read as one: the row falls through and is skipped+ // rather than landing on an invented date.+ it('rejects a word that names no month', () => {+ expect(parseWhen('1 blah 2024')).toBe(null)+ expect(parseWhen('')).toBe(null)+ })++ it('imports a French Hevy export whole', () => {+ const csv = [+ 'title,start_time,end_time,exercise_title,set_index,set_type,weight_kg,reps',+ ...FR_MONTHS.map(([when], i) =>+ `Séance,"${when}","${when}",Bench Press,${i + 1},normal,80,5`),+ ].join('\n')+ const parsed = parseWorkoutCSV(csv, { unit: 'kg' })++ expect(parsed.source).toBe('Hevy')+ expect(parsed.skipped).toBe(0)+ expect(parsed.sets).toBe(12)+ expect(parsed.workouts).toHaveLength(12) // one per month, none dropped+ expect(parsed.from).toBe('2024-01-20')+ expect(parsed.to).toBe('2024-12-23')+ })+})