Skip to content

Commit

Permalink
fix date related issues in table and mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiion committed Sep 30, 2024
1 parent 2a0e27e commit dbaeb3d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
16 changes: 11 additions & 5 deletions apps/exhibition-live/components/config/tableConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ const dateColDef: (
const columnDef: MRT_ColumnDef<any> = {
header: t(p([...path, key])),
id: p([...path, key, "single"]),
accessorFn: mkAccessor(`${p([...path, key, "single"])}.value`, "", (v) =>
typeof v === "string" || (typeof v === "number" && String(v).length === 8)
? specialDate2LocalDate(Number(v), "de")
: String(v),
),
accessorFn: mkAccessor(`${p([...path, key, "single"])}.value`, "", (v) => {
try {
return typeof v === "string" ||
(typeof v === "number" && String(v).length === 8)
? specialDate2LocalDate(Number(v), "de")
: String(v);
} catch (error) {
console.error("Error processing date:", error);
return String(v);
}
}),
filterVariant: "date",
filterFn: "betweenInclusive",
sortingFn: "datetime",
Expand Down
16 changes: 16 additions & 0 deletions packages/core-utils/src/specialDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ export const getDatePartAsString = (
const maxLength = part === "year" ? 4 : 2;
return leftpad(value, maxLength);
};

/**
* convert year, month, day to a number
* @param year
* @param month
* @param day
* @returns
*/
export const makeSpecialDate = (
year: number,
month?: number | undefined,
day?: number | undefined,
) => {
const specialDateString = `${year}${month ? leftpad(month, 2) : "00"}${day ? leftpad(day, 2) : "00"}`;
return Number(specialDateString);
};
25 changes: 15 additions & 10 deletions packages/data-mapping/src/mappingStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { mapByConfig } from "./mapByConfig";
import isNil from "lodash-es/isNil";
import set from "lodash-es/set";
import get from "lodash-es/get";
import { getPaddedDate } from "@slub/edb-core-utils";
import { getPaddedDate, makeSpecialDate } from "@slub/edb-core-utils";
import {
IRIToStringFn,
NormDataMappings,
PrimaryFieldDeclaration,
} from "@slub/edb-core-types";
import { JSONSchema7 } from "json-schema";
import { isNaN } from "lodash-es";

dayjs.extend(customParseFormat);

Expand Down Expand Up @@ -899,21 +900,25 @@ export const arrayToAdbDate = (
dateValue: number;
dateModifier: number;
} => {
const { logger } = context || {};
const { offset = 0 } = options || {};
if (sourceData.length < offset + 3)
throw new Error(
`Not enough data (${sourceData.length}) to convert to date`,
);
const day = sourceData[offset];
const month = sourceData[offset + 1];
const year = sourceData[offset + 2];
if (!year) return null;
const date = dayjs();
date.year(year);
if (month) date.month(month - 1);
if (day) date.date(day);

const day = Number(sourceData[offset]);
const month = Number(sourceData[offset + 1] || "0");
const year = Number(sourceData[offset + 2] || "0");
const dateValue = makeSpecialDate(
isNaN(year) ? 0 : year,
isNaN(month) ? 0 : month,
isNaN(day) ? 0 : day,
);
logger.log(`Converted date ${dateValue} from ${sourceData}`);

return {
dateValue: dayJsDateToSpecialInt(date),
dateValue: Number(dateValue),
dateModifier: 0,
};
};
Expand Down
12 changes: 9 additions & 3 deletions packages/ui-utils/src/specialDate2LocalDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ export const specialDate2LocalDate = (date: number, locale: string) => {
const { year, month, day } = getDateParts(date);
const jsdate = numeric2JSDate(date);
if (!year && month && day) {
return dayjs(jsdate).format("MM.DD.");
return locale === "de"
? dayjs(jsdate).format("DD.MM.")
: dayjs(jsdate).format("MM/DD");
}
if (!day && month && year) {
return dayjs(jsdate).format("YYYY.MM.") + "–-";
return locale === "de"
? dayjs(jsdate).format("MMMM YYYY")
: dayjs(jsdate).format("MMMM/YYYY");
}
if (day && month && year) {
return dayjs(jsdate).format("l");
return locale === "de"
? dayjs(jsdate).format("DD.MM.YYYY")
: dayjs(jsdate).format("MM/DD/YYYY");
}
if (year && !month && !day) {
return dayjs(jsdate).format("YYYY");
Expand Down

0 comments on commit dbaeb3d

Please sign in to comment.