Format inventory expiration dates instead of epoch timestamps (#901)

Legacy inventory items stored date fields as Unix epoch seconds. The table showed the raw numbers, and sorting and expired highlighting did not treat them as dates.

- Frontend parses date values as YYYY-MM-DD or legacy epoch seconds; display, mobile view, export, search, sort and the expired check all use calendar dates with no timezone shift.
- The legacy migration converts epoch seconds to YYYY-MM-DD, and import repairs already-migrated files once.
- Jest runs with a pinned timezone; the expiry tests pass under any process timezone.

Closes #41

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Joel Shearon 2026-10-31 14:24:18 -04:00 • committed by Roland
parent d69f8317aa
commit fe85815c35
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
11 changed files with 389 additions and 23 deletions

View file

@ -1,5 +1,6 @@
import React from "react";
import {Select, TextInput} from "../ui";
import {formatInventoryDateValue} from "./inventoryDates";
import {ALL_UNITS, evaluateExpression, NUMERIC_TYPES, UNIT_GROUPS} from "./units";
// Renders the correct editor for a single field/value, wiring up keyboard navigation. The `inputRef` is attached
@ -102,7 +103,7 @@ export function FieldCell({field, value, unitValue, onChange, onUnitChange, onEn
}
if (field.type === 'date') {
return <TextInput {...common} type='date' ref={setRef}/>;
return <TextInput {...common} type='date' value={formatInventoryDateValue(value)} ref={setRef}/>;
}
if (field.type === 'number' || field.type === 'calories') {

View file

@ -2,6 +2,7 @@ import React, {useMemo, useRef, useState} from "react";
import {Button, Checkbox, Icon, Table} from "../ui";
import {FieldCell} from "./FieldCell";
import {applyComputedCounts, computedCountConfigs} from "./computeFields";
import {formatInventoryDateValue, localTodayYmd, parseInventoryDateMs} from "./inventoryDates";
import {NUMERIC_TYPES} from "./units";
const LOCATION_LIST_ID = 'inventory-location-suggestions';
@ -25,22 +26,37 @@ function nextId(items) {
return items.reduce((max, i) => (typeof i.id === 'number' && i.id > max ? i.id : max), 0) + 1;
}
// Type-aware comparison for sorting: numeric for number/quantity/calories, chronological for date, locale string
// otherwise. Blank values sort last (ascending).
function compareByField(a, b, field) {
const av = a[field.key], bv = b[field.key];
const aBlank = av === '' || av == null;
const bBlank = bv === '' || bv == null;
if (aBlank || bBlank) {
return aBlank === bBlank ? 0 : (aBlank ? 1 : -1);
// Blank values sort last (ascending). Sort keys are precomputed once per row before sorting.
function fieldSortKey(item, field) {
const av = item[field.key];
if (av === '' || av == null) {
return {kind: 'blank'};
}
if (NUMERIC_TYPES.includes(field.type)) {
return (Number(av) || 0) - (Number(bv) || 0);
return {kind: 'number', v: Number(av) || 0};
}
if (field.type === 'date') {
return (Date.parse(av) || 0) - (Date.parse(bv) || 0);
return {kind: 'date', v: parseInventoryDateMs(av) || 0};
}
return String(av).localeCompare(String(bv));
return {kind: 'string', v: String(av)};
}
function compareFieldSortKeys(ak, bk) {
if (ak.kind === 'blank' || bk.kind === 'blank') {
const aBlank = ak.kind === 'blank';
const bBlank = bk.kind === 'blank';
return aBlank === bBlank ? 0 : (aBlank ? 1 : -1);
}
if (ak.kind === 'number' && bk.kind === 'number') {
return ak.v - bk.v;
}
if (ak.kind === 'date' && bk.kind === 'date') {
return ak.v - bk.v;
}
if (ak.kind === 'string' && bk.kind === 'string') {
return ak.v.localeCompare(bk.v);
}
return 0;
}
/**
@ -120,7 +136,10 @@ export function InventoryTable({slug, fields, items, locations, catalog, search,
if (!field) {
return list;
}
const sorted = [...list].sort((a, b) => compareByField(a, b, field));
const withKeys = list.map(item => ({item, key: fieldSortKey(item, field)}));
const sorted = withKeys
.sort((a, b) => compareFieldSortKeys(a.key, b.key))
.map(({item}) => item);
return sort.dir === 'desc' ? sorted.reverse() : sorted;
}, [items, fields, sort]);
@ -296,19 +315,25 @@ export function formatValue(item, field) {
const unit = item[`${field.key}_unit`] || field.unit || '';
return `${value} ${unit}`.trim();
}
if (field.type === 'date') {
return formatInventoryDateValue(value);
}
return String(value);
}
// True when any `date`-type field is before the start of today (item is expired).
// True when any `date`-type field is before today's local calendar date (item is expired).
export function isItemExpired(item, fields) {
const start = new Date();
start.setHours(0, 0, 0, 0);
const today = localTodayYmd();
return (fields || []).some(f => {
if (f.type !== 'date') {
return false;
}
const t = Date.parse(item[f.key]);
return !isNaN(t) && t < start.getTime();
const raw = item[f.key];
if (raw === '' || raw == null) {
return false;
}
const ymd = formatInventoryDateValue(raw);
return /^\d{4}-\d{2}-\d{2}$/.test(ymd) && ymd < today;
});
}

View file

@ -205,6 +205,22 @@ describe('InventoryTable sorting', () => {
// 5 < 12 < 20 numerically (lexical would put '12','20','5').
expect(bodyNames()).toEqual(['Rice', 'Apples', 'Beans']);
});
test('date column sorts chronologically, including legacy epoch values', () => {
const dateFields = [
{key: 'name', label: 'Name', type: 'text', order: 0},
{key: 'expiration_date', label: 'Expires', type: 'date', order: 1},
];
const datedItems = [
{id: 1, name: 'Late', expiration_date: '1893456000'}, // 2030-01-01
{id: 2, name: 'Early', expiration_date: '2000-01-01'},
];
render(<InventoryTable slug='s' fields={dateFields} items={datedItems} locations={[]}
onChange={jest.fn()}/>);
fireEvent.click(screen.getByText('Expires'));
expect(bodyNames()).toEqual(['Early', 'Late']);
expect(screen.getByText('2030-01-01')).toBeTruthy();
});
});
describe('InventoryTable catalog pre-fill', () => {

View file

@ -0,0 +1,106 @@
const DATE_ONLY_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
/** Legacy inventory dates stored as Unix epoch seconds (not arbitrary digit strings). */
const EPOCH_SECONDS_RE = /^\d{9,10}(\.0+)?$/;
function digitsToEpochMs(value) {
const n = Number(value);
if (!Number.isFinite(n)) {
return null;
}
return n * 1000;
}
function isEpochSecondsNumber(value) {
if (typeof value !== 'number' || !Number.isFinite(value)) {
return false;
}
const digits = String(Math.trunc(Math.abs(value))).length;
return digits >= 9 && digits <= 10;
}
function formatMsUtc(ms) {
const d = new Date(ms);
const y = d.getUTCFullYear();
const m = String(d.getUTCMonth() + 1).padStart(2, '0');
const day = String(d.getUTCDate()).padStart(2, '0');
return `${y}-${m}-${day}`;
}
function formatMsLocal(ms) {
const d = new Date(ms);
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${y}-${m}-${day}`;
}
function parseDateOnlyLocalMs(value) {
const m = DATE_ONLY_RE.exec(String(value).trim());
if (!m) {
return null;
}
const y = Number(m[1]);
const mo = Number(m[2]);
const day = Number(m[3]);
const ms = new Date(y, mo - 1, day).getTime();
return Number.isNaN(ms) ? null : ms;
}
/** Today's calendar date in the local timezone as YYYY-MM-DD. */
export function localTodayYmd() {
return formatMsLocal(Date.now());
}
// Parse inventory `date`-field values (YYYY-MM-DD or legacy Unix epoch seconds) to epoch ms, or null.
export function parseInventoryDateMs(value) {
if (value === '' || value == null) {
return null;
}
if (isEpochSecondsNumber(value)) {
return digitsToEpochMs(value);
}
const s = String(value).trim();
if (EPOCH_SECONDS_RE.test(s)) {
return digitsToEpochMs(s);
}
const dateOnlyMs = parseDateOnlyLocalMs(s);
if (dateOnlyMs != null) {
return dateOnlyMs;
}
if (/^\d+$/.test(s)) {
return null;
}
const parsed = Date.parse(s);
return Number.isNaN(parsed) ? null : parsed;
}
// Format for display and `<input type="date">` (calendar date; never shifted by timezone).
export function formatInventoryDateValue(value) {
if (value === '' || value == null) {
return value == null ? '' : String(value);
}
if (typeof value === 'number' && Number.isFinite(value)) {
// A YAML number is either legacy epoch seconds or something we can't interpret; never treat it as epoch
// milliseconds, so `2030` and `'2030'` display the same.
return isEpochSecondsNumber(value) ? formatMsUtc(digitsToEpochMs(value)) : String(value);
}
const s = String(value).trim();
if (DATE_ONLY_RE.test(s)) {
return s;
}
if (EPOCH_SECONDS_RE.test(s)) {
return formatMsUtc(digitsToEpochMs(s));
}
if (/^\d+$/.test(s)) {
return s;
}
const dateOnlyMs = parseDateOnlyLocalMs(s);
if (dateOnlyMs != null) {
return formatMsLocal(dateOnlyMs);
}
const parsed = Date.parse(s);
if (Number.isNaN(parsed)) {
return s;
}
return formatMsLocal(parsed);
}

View file

@ -0,0 +1,96 @@
import {formatInventoryDateValue, localTodayYmd, parseInventoryDateMs} from "./inventoryDates";
import {filterItems, formatValue, isItemExpired} from "./InventoryTable";
describe('inventoryDates', () => {
test('parseInventoryDateMs accepts ISO dates and epoch seconds', () => {
expect(parseInventoryDateMs('2030-01-01')).toBe(new Date(2030, 0, 1).getTime());
expect(parseInventoryDateMs('1893456000')).toBe(1893456000 * 1000);
});
test('formatInventoryDateValue renders a calendar date', () => {
expect(formatInventoryDateValue('2030-01-01')).toBe('2030-01-01');
expect(formatInventoryDateValue('1893456000')).toBe('2030-01-01');
});
test('ambiguous digit strings are not treated as epoch seconds', () => {
expect(formatInventoryDateValue('2030')).toBe('2030');
expect(formatInventoryDateValue('20300101')).toBe('20300101');
expect(parseInventoryDateMs('2030')).toBeNull();
expect(parseInventoryDateMs('20300101')).toBeNull();
expect(formatInventoryDateValue('2030')).not.toMatch(/^1970/);
expect(formatInventoryDateValue('20300101')).not.toMatch(/^1970/);
// Unquoted YAML numbers display the same as the equivalent string.
expect(formatInventoryDateValue(2030)).toBe('2030');
expect(formatInventoryDateValue(20300101)).toBe('20300101');
expect(formatInventoryDateValue(1893456000)).toBe('2030-01-01');
});
test('epoch seconds with a .0 suffix are accepted', () => {
expect(formatInventoryDateValue('1893456000.0')).toBe('2030-01-01');
expect(parseInventoryDateMs('1893456000.0')).toBe(1893456000 * 1000);
});
test('date-only values round-trip without timezone shift (US Eastern)', () => {
const samples = ['2030-01-01', '2000-06-15', '2999-01-01'];
for (const stored of samples) {
const displayed = formatInventoryDateValue(stored);
expect(displayed).toBe(stored);
const fromInput = displayed;
expect(fromInput).toBe(stored);
expect(formatInventoryDateValue(fromInput)).toBe(stored);
const [y, m, d] = stored.split('-').map(Number);
expect(parseInventoryDateMs(fromInput)).toBe(new Date(y, m - 1, d).getTime());
}
});
});
describe('isItemExpired', () => {
const fields = [{key: 'expiration_date', label: 'Expires', type: 'date', order: 0}];
afterEach(() => jest.useRealTimers());
// The local calendar date of the (possibly faked) clock, independent of the process TZ.
const localYmd = () => {
const d = new Date();
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
};
test('a date that is still today or later is not expired late in the evening', () => {
jest.useFakeTimers();
// 2030-01-01T02:00:00Z is 9 PM on 2029-12-31 in US Eastern, but already 2030-01-01 in UTC and east of it.
// Either way 2030-01-01 is not before the local date, so it must not be expired.
jest.setSystemTime(new Date('2030-01-01T02:00:00.000Z'));
expect(localTodayYmd()).toBe(localYmd());
expect(isItemExpired({expiration_date: '2030-01-01'}, fields)).toBe(false);
expect(isItemExpired({expiration_date: '1893456000'}, fields)).toBe(false);
});
test('yesterday\'s calendar date is expired', () => {
// 2030-01-02T15:00:00Z is 2030-01-02 in every timezone from UTC-9 through UTC+8.
jest.useFakeTimers();
jest.setSystemTime(new Date('2030-01-02T15:00:00.000Z'));
expect(localTodayYmd()).toBe(localYmd());
expect(isItemExpired({expiration_date: '2030-01-01'}, fields)).toBe(true);
expect(isItemExpired({expiration_date: '1893456000'}, fields)).toBe(true);
});
});
describe('formatValue date fields', () => {
const field = {key: 'expiration_date', label: 'Expires', type: 'date', order: 0};
test('legacy epoch seconds display as YYYY-MM-DD', () => {
expect(formatValue({expiration_date: '1893456000'}, field)).toBe('2030-01-01');
});
});
describe('filterItems epoch dates', () => {
const fields = [
{key: 'name', label: 'Name', type: 'text', order: 0},
{key: 'expiration_date', label: 'Expires', type: 'date', order: 1},
];
const items = [{id: 1, name: 'Rice', expiration_date: '1893456000'}];
test('search matches formatted expiration date', () => {
expect(filterItems(items, fields, '2030-01-01')).toHaveLength(1);
});
});