Skip to content

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.

Import: skip impossible dates and broken numbers

Duarte Santosb6a9cc5c84c0
diff --git a/frontend/src/lib/import-csv.hostile.test.js b/frontend/src/lib/import-csv.hostile.test.js
index ce7678fa8..2e424f680 100644
--- a/frontend/src/lib/import-csv.hostile.test.js
+++ b/frontend/src/lib/import-csv.hostile.test.js
@@ -82,8 +82,8 @@ describe('numbers that are not numbers', () => {
expect(one({ w: '"52,5"', u: 'kg', r: 5 }).workouts[0].entries[0].sets[0].w).toBe(52.5)
})
- it('reps: a billion is kept, text is 0', () => {
- expect(one({ w: 50, u: 'kg', r: '1e9' }).workouts[0].entries[0].sets[0].r).toBe(1e9)
+ it('reps: a billion is a broken cell and skipped, text is 0', () => {
+ expect(one({ w: 50, u: 'kg', r: '1e9' })).toMatchObject({ workouts: [], skipped: 1 })
expect(one({ w: 50, u: 'kg', r: 'ten' }).workouts[0].entries[0].sets[0].r).toBe(0)
})
@@ -232,7 +232,7 @@ describe('mergeImport adds and never overwrites', () => {
it('weigh-ins: existing days win and the list stays sorted', () => {
const S = state()
- const p = parseImport('Date,Weight (kg)\n2024-03-07,999\n2024-03-05,79\n2024-03-09,81', kg)
+ const p = parseImport('Date,Weight (kg)\n2024-03-07,99\n2024-03-05,79\n2024-03-09,81', kg)
expect(p.kind).toBe('bodyweight')
expect(mergeImport(S, p)).toEqual({ added: 2, skipped: 1 })
expect(S.bodyweight.map(b => [b.d, b.w])).toEqual([['2024-03-05', 79], ['2024-03-07', 80], ['2024-03-09', 81]])
diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js
index bebab92af..086e9af9d 100644
--- a/frontend/src/lib/import-csv.js
+++ b/frontend/src/lib/import-csv.js
@@ -344,6 +344,8 @@ const effortNum = (raw, zeroMeansRated) => {
return Math.min(10, Math.round(n * 100) / 100)
}
const LB_TO_KG = 0.45359237
+// Past these a cell is broken, not a record (the heaviest lifts ever logged stay well under).
+const MAX_KG = 2000, MAX_REPS = 1000, MAX_KM = 1000, MAX_BODY_KG = 650
const p2 = n => String(n).padStart(2, '0')
// 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
@@ -377,14 +379,14 @@ const monthOf = w => {
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]) }
+ if (m) return calendarDay(m[1], m[2], m[3], 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]) }
+ if (mon) return calendarDay(m[3], mon, m[1], 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]) }
+ if (mon) return calendarDay(m[3], mon, m[2], 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}))?/)
@@ -392,7 +394,7 @@ export function parseWhen(s) {
const [, a, b, y] = m
const day = +a > 12 ? a : +b > 12 ? b : a
const mon = day === a ? b : a
- return { d: `${y}-${p2(mon)}-${p2(day)}`, t: hm(m[4], m[5]) }
+ return calendarDay(y, mon, day, m[4], m[5])
}
return null
}
@@ -401,6 +403,19 @@ export const daysBetween = (a, b) => {
const n = (Date.UTC(+b.slice(0, 4), +b.slice(5, 7) - 1, +b.slice(8, 10)) - Date.UTC(+a.slice(0, 4), +a.slice(5, 7) - 1, +a.slice(8, 10))) / 86400000
return isFinite(n) && n > 0 && n < 2 ? n : 0
}
+// A date only counts when it is one: a real calendar day (no 31 February, no month 13), not
+// before 1900 and not in the future. A day of slack covers a file written in a time zone
+// ahead of this one. Anything else is a broken cell, and filed as a workout it would sit
+// in the history as "Invalid Date" or a session from the year 9999.
+function calendarDay(y, mo, d, h, mi) {
+ y = +y; mo = +mo; d = +d
+ const at = new Date(Date.UTC(y, mo - 1, d))
+ if (y < 1900 || at.getUTCFullYear() !== y || at.getUTCMonth() !== mo - 1 || at.getUTCDate() !== d) return null
+ if (h !== undefined && (+h > 23 || +mi > 59)) return null
+ const now = new Date()
+ if (at.getTime() > Date.UTC(now.getFullYear(), now.getMonth(), now.getDate() + 1)) return null
+ return { d: `${y}-${p2(mo)}-${p2(d)}`, t: hm(h, mi) }
+}
const hm = (h, mi) => (h === undefined ? null : (parseInt(h, 10) || 0) * 3600000 + (parseInt(mi, 10) || 0) * 60000)
/** "HH:MM:SS" · "MM:SS" · "90" -> minutes */
@@ -484,6 +499,10 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) {
? toKm(cell(r, 'distanceM'), 'm')
: toKm(cell(r, 'distance'), cell(r, 'distanceUnit'))
if (!w && !reps && !mins && !km) { skipped++; continue }
+ // Nobody lifts a negative weight or 2 t, nor does 1,000 reps in a set; a cell like that is
+ // broken, and kept it would show as an infinite PR or a NaN chart. Skip the row, say so.
+ const maxW = rowUnit === 'lb' || (!rowUnit && unit === 'lb') ? MAX_KG / LB_TO_KG : MAX_KG
+ if (w < 0 || reps < 0 || secs < 0 || mins < 0 || km < 0 || w > maxW || reps > MAX_REPS || mins > 24 * 60 || km > MAX_KM) { skipped++; continue }
const warmup = /warm/i.test(cell(r, 'setType'))
if (warmup) warmups++
@@ -657,7 +676,7 @@ export function parseBodyweight(text, { unit = 'kg' } = {}) {
// `[\d.]+` lets a bare "." through, which parseFloat reads as NaN, and a zeroed record is
// no weigh-in either — the same gate the CSV branch below applies with `!w`.
const w = parseFloat(val[1])
- if (!isFinite(w) || !w) continue
+ if (!isFinite(w) || !w || !bodyOk(w, fileUnit || unit)) continue
out.set(when.d, { w, t: new Date(dt[1]).getTime() || null })
}
} else {
@@ -673,7 +692,7 @@ export function parseBodyweight(text, { unit = 'kg' } = {}) {
for (let i = 1; i < rows.length; i++) {
const when = parseWhen(String(rows[i][dCol] ?? ''))
const w = num(rows[i][wCol])
- if (!when || !w) continue
+ if (!when || !w || !bodyOk(w, fileUnit || unit)) continue
out.set(when.d, { w, t: new Date(when.d).getTime() + (when.t ?? 0) })
}
}
@@ -691,6 +710,9 @@ export function parseBodyweight(text, { unit = 'kg' } = {}) {
}
}
+/** A weigh-in a person could have: above zero and below MAX_BODY_KG in the given unit. */
+const bodyOk = (w, u) => w > 0 && w <= (u === 'lb' ? MAX_BODY_KG / LB_TO_KG : MAX_BODY_KG)
+
/** Sniff the file and parse it as whatever it is. */
export function parseImport(text, opts) {
const s = String(text)
diff --git a/frontend/src/lib/import-csv.test.js b/frontend/src/lib/import-csv.test.js
index f13d4600c..613862830 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 { workoutVolume } from './history.js'
-import { parseWhen, parseWorkoutCSV, mergeImport } from './import-csv.js'
+import { parseWhen, parseWorkoutCSV, parseBodyweight, mergeImport } from './import-csv.js'
const CSV = [
'Date,Exercise,Weight,Reps,Set Type',
@@ -226,3 +226,35 @@ describe('session past midnight', () => {
expect((w.end - w.start) / 60000).toBe(50)
})
})
+
+describe('impossible dates and numbers', () => {
+ it('parseWhen refuses days that do not exist, are ancient or in the future', () => {
+ for (const s of ['0001-01-01', '9999-12-31', '2026-02-31', '2026-13-45', '31/02/2024', '2024-03-07 25:00']) {
+ expect(parseWhen(s), s).toBeNull()
+ }
+ expect(parseWhen('2024-02-29')).toEqual({ d: '2024-02-29', t: null })
+ })
+
+ it('skips broken rows instead of filing them', () => {
+ const csv = [
+ 'Date,Exercise,Category,Weight,Weight Unit,Reps',
+ '2026-09-10,Squat,Legs,100,kgs,5',
+ '2026-09-10,Leg Press,Legs,1e308,kgs,10',
+ '2026-09-10,Leg Press,Legs,-50,kgs,-5',
+ '2026-09-10,Leg Press,Legs,99999999,kgs,999999',
+ '2026-09-10,Leg Press,Legs,200,kgs,1001',
+ '0001-01-01,Ancient Lift,Legs,50,kgs,5',
+ '9999-12-31,Future Lift,Legs,50,kgs,5',
+ '2026-02-31,Bad Date Lift,Legs,50,kgs,5',
+ ].join('\n')
+ const parsed = parseWorkoutCSV(csv, { unit: 'kg' })
+ expect(parsed.skipped).toBe(7)
+ expect(parsed.workouts).toHaveLength(1)
+ expect(parsed.workouts[0].entries.flatMap(e => e.sets)).toEqual([{ w: 100, r: 5, done: true }])
+ })
+
+ it('body weight drops future days and impossible weights', () => {
+ const parsed = parseBodyweight('Date,Weight (kg)\n2026-01-01,70\n2026-01-02,-70\n2026-01-03,9000\n2099-01-01,70', { unit: 'kg' })
+ expect(parsed.bodyweight.map(b => b.d)).toEqual(['2026-01-01'])
+ })
+})
diff --git a/frontend/src/sheets.import-summary.test.jsx b/frontend/src/sheets.import-summary.test.jsx
index 3bc193c13..ac942ad6c 100644
--- a/frontend/src/sheets.import-summary.test.jsx
+++ b/frontend/src/sheets.import-summary.test.jsx
@@ -35,7 +35,7 @@ describe('the import summary’s dates', () => {
afterEach(() => { if (root) act(() => root.unmount()); root = null; document.body.innerHTML = '' })
it('carry the year when the history spans two years', async () => {
- const text = await summaryFor(csv('2025-02-03', '2026-12-21'))
+ const text = await summaryFor(csv('2025-02-03', '2026-01-21'))
expect(text).toContain('2025')
expect(text).toContain('2026')
})