From 4542c6aaff18d6149b56047104b566f6bef21e82 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 2 Oct 2024 13:54:39 +0200 Subject: [PATCH] Rename standalone formatDateTime to formatSortableDateTime --- src/sidebar/services/annotations-exporter.tsx | 16 +++++++++------ .../test/annotations-exporter-test.js | 4 ++-- src/sidebar/util/test/time-test.js | 20 +++++++++---------- src/sidebar/util/time.ts | 7 ++++++- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/sidebar/services/annotations-exporter.tsx b/src/sidebar/services/annotations-exporter.tsx index c86bca1a836..b3f5a3955aa 100644 --- a/src/sidebar/services/annotations-exporter.tsx +++ b/src/sidebar/services/annotations-exporter.tsx @@ -15,7 +15,7 @@ import { annotationDisplayName } from '../helpers/annotation-user'; import { stripInternalProperties } from '../helpers/strip-internal-properties'; import { VersionData } from '../helpers/version-data'; import { renderMathAndMarkdown } from '../render-markdown'; -import { formatDateTime } from '../util/time'; +import { formatSortableDateTime } from '../util/time'; export type JSONExportContent = { export_date: string; @@ -91,7 +91,7 @@ export class AnnotationsExporter { const page = pageLabel(annotation); const annotationQuote = quote(annotation); const lines = [ - `Created at: ${formatDateTime(new Date(annotation.created))}`, + `Created at: ${formatSortableDateTime(new Date(annotation.created))}`, `Author: ${extractUsername(annotation)}`, page ? `Page: ${page}` : undefined, `Type: ${annotationRole(annotation)}`, @@ -108,7 +108,7 @@ export class AnnotationsExporter { }); return trimAndDedent` - ${formatDateTime(now)} + ${formatSortableDateTime(now)} ${title} ${uri} Group: ${groupName} @@ -135,7 +135,7 @@ export class AnnotationsExporter { }); const annotationToRow = (annotation: APIAnnotationData) => [ - formatDateTime(new Date(annotation.created)), + formatSortableDateTime(new Date(annotation.created)), extractUsername(annotation), pageLabel(annotation) ?? '', uri, @@ -192,7 +192,9 @@ export class AnnotationsExporter {

Summary

- +

{title} @@ -253,7 +255,9 @@ export class AnnotationsExporter { Created at: diff --git a/src/sidebar/services/test/annotations-exporter-test.js b/src/sidebar/services/test/annotations-exporter-test.js index 8b8da842eb8..e5704dbab9f 100644 --- a/src/sidebar/services/test/annotations-exporter-test.js +++ b/src/sidebar/services/test/annotations-exporter-test.js @@ -4,7 +4,7 @@ import { newReply, publicAnnotation, } from '../../test/annotation-fixtures'; -import { formatDateTime } from '../../util/time'; +import { formatSortableDateTime } from '../../util/time'; import { AnnotationsExporter } from '../annotations-exporter'; describe('AnnotationsExporter', () => { @@ -30,7 +30,7 @@ describe('AnnotationsExporter', () => { beforeEach(() => { now = new Date(); - formattedNow = formatDateTime(now); + formattedNow = formatSortableDateTime(now); baseAnnotation = { ...newAnnotation(), ...publicAnnotation(), diff --git a/src/sidebar/util/test/time-test.js b/src/sidebar/util/test/time-test.js index f24295324c3..9dc3a21af37 100644 --- a/src/sidebar/util/test/time-test.js +++ b/src/sidebar/util/test/time-test.js @@ -1,15 +1,13 @@ -import { formatDateTime } from '../time'; +import { formatSortableDateTime } from '../time'; -describe('sidebar/util/time', () => { - describe('formatDateTime', () => { - [ - new Date(Date.UTC(2023, 11, 20, 3, 5, 38)), - new Date('2020-05-04T23:02:01+05:00'), - ].forEach(date => { - it('returns right format for provided date', () => { - const expectedDateRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/; - assert.match(formatDateTime(date), expectedDateRegex); - }); +describe('formatSortableDateTime', () => { + [ + new Date(Date.UTC(2023, 11, 20, 3, 5, 38)), + new Date('2020-05-04T23:02:01+05:00'), + ].forEach(date => { + it('returns right format for provided date', () => { + const expectedDateRegex = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/; + assert.match(formatSortableDateTime(date), expectedDateRegex); }); }); }); diff --git a/src/sidebar/util/time.ts b/src/sidebar/util/time.ts index 26056618df6..95b5b5c0690 100644 --- a/src/sidebar/util/time.ts +++ b/src/sidebar/util/time.ts @@ -1,7 +1,12 @@ /** * Formats a date as `YYYY-MM-DD hh:mm`, using 24h and system timezone. + * + * This format has the next characteristics: + * - Lexicographic and chronological order match. + * - It is detected as "date" when pasted in common spreadsheet editors, making + * it useful when exporting dates for CSV documents. */ -export function formatDateTime(date: Date): string { +export function formatSortableDateTime(date: Date): string { const year = date.getFullYear(); const month = `${date.getMonth() + 1}`.padStart(2, '0'); const day = `${date.getDate()}`.padStart(2, '0');