Skip to content

Commit

Permalink
Simplify date handling in API
Browse files Browse the repository at this point in the history
  • Loading branch information
brookback committed Mar 15, 2024
1 parent ddd18be commit 2af2f6a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 704 deletions.
25 changes: 16 additions & 9 deletions api/date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DateTimeFormatterTemporal } from './vendor/datetime-format-temporal.ts';

// => 'yyyy-MM-dd HH:mm:ss'
// fileName: true => 'yyyy-MM-dd-HH-mm-ss'
export const formatDate = (date: Date, fileName = false) => {
Expand All @@ -19,13 +17,22 @@ export const formatDate = (date: Date, fileName = false) => {
return datePart + ' ' + timePart;
};

// Temporal proposal doesn't include nice strftime stuff yet:
// https://github.com/js-temporal/proposal-temporal-v2/issues/5
// This function only supports "simple" formats, like `yyyy-MM-dd HH:mm:ss`
export const formatTemporalDate = (thing: Temporal.PlainDateTimeLike, format: string): string => {
const formatter = new DateTimeFormatterTemporal(format);
return formatter.format(thing);
};
/** Formats into `2024-03-15T10:24:35+01:00`. */
export const formatISO = (d: Temporal.ZonedDateTime): string =>
d.toString({
timeZoneName: 'never',
smallestUnit: 'seconds',
});

/** Format appropriate for file name: `2024-03-15-10-24-35`. */
export const formatFileName = (d: Temporal.ZonedDateTime): string =>
d.toString({
timeZoneName: 'never',
smallestUnit: 'seconds',
offset: 'never',
})
.replace('T', '-')
.replaceAll(':', '-');

export const safeTemporalZonedDateTime = (str: string): Temporal.ZonedDateTime | null => {
try {
Expand Down
7 changes: 0 additions & 7 deletions api/routes/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ interface Body {

// TODO abstract
const finishBookParseBody = (json: any): Body => {
if (typeof json.finishedAt != 'string') {
throw new ProblemError(
ProblemKind.BodyParseError,
`"finishedAt" must be a string, got ${typeof json.finishedAt}`,
);
}

const zonedDateTime = ((): Temporal.ZonedDateTime => {
if (typeof json.finishedAt != 'string') {
throw new ProblemError(
Expand Down
11 changes: 5 additions & 6 deletions api/routes/note.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta } from './index.ts';
import { formatTemporalDate, safeTemporalZonedDateTime } from '../date.ts';
import { formatFileName, formatISO, safeTemporalZonedDateTime } from '../date.ts';
import { ProblemError, ProblemKind } from '../problem.ts';
import { join } from 'std/path/mod.ts';
import * as Yaml from 'std/yaml/mod.ts';
Expand Down Expand Up @@ -69,13 +69,12 @@ const inputOf = (json: any): Input => {

// I want both of these to be in the *local* time:

// Will go into the frontmatter:
const metaDate = formatTemporalDate(
// Will go into the frontmatter: "2024-03-15T10:24:35+01:00"
const metaDate = formatISO(
zonedDateTime,
`yyyy-MM-dd'T'HH:mm:ss${zonedDateTime.offset}`,
);
// Will be the filename:
const fileDate = formatTemporalDate(zonedDateTime, 'yyyy-MM-dd-HH-mm-ss');
// Will be the filename: "2024-03-15-10-24-35"
const fileDate = formatFileName(zonedDateTime);

return {
contents: json.contents.trim(),
Expand Down
Loading

0 comments on commit 2af2f6a

Please sign in to comment.