From 3ac21498f32d05d881b18949b4d8fd4f49c4333d Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 18 Aug 2024 13:04:51 +0200 Subject: [PATCH] Fixes #680 all times and dates are now local (to the environment) --- common/space.ts | 9 +++++---- lib/dates.test.ts | 5 +++++ lib/dates.ts | 25 ++++++++++++------------- plug-api/lib/json.ts | 4 +++- plugs/editor/complete.ts | 5 +++-- web/cm_plugins/editor_paste.ts | 4 ++-- 6 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 lib/dates.test.ts diff --git a/common/space.ts b/common/space.ts index b2dee266..72710025 100644 --- a/common/space.ts +++ b/common/space.ts @@ -4,6 +4,7 @@ import { plugPrefix } from "$common/spaces/constants.ts"; import type { AttachmentMeta, FileMeta, PageMeta } from "../plug-api/types.ts"; import type { EventHook } from "./hooks/event.ts"; import { safeRun } from "../lib/async.ts"; +import { localDateString } from "$lib/dates.ts"; const pageWatchInterval = 5000; @@ -195,8 +196,8 @@ export function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { ref: name, tag: "page", name, - created: new Date(fileMeta.created).toISOString(), - lastModified: new Date(fileMeta.lastModified).toISOString(), + created: localDateString(new Date(fileMeta.created)), + lastModified: localDateString(new Date(fileMeta.lastModified)), } as PageMeta; } catch (e) { console.error("Failed to convert fileMeta to pageMeta", fileMeta, e); @@ -212,8 +213,8 @@ export function fileMetaToAttachmentMeta( ...fileMeta, ref: fileMeta.name, tag: "attachment", - created: new Date(fileMeta.created).toISOString(), - lastModified: new Date(fileMeta.lastModified).toISOString(), + created: localDateString(new Date(fileMeta.created)), + lastModified: localDateString(new Date(fileMeta.lastModified)), } as AttachmentMeta; } catch (e) { console.error("Failed to convert fileMeta to attachmentMeta", fileMeta, e); diff --git a/lib/dates.test.ts b/lib/dates.test.ts new file mode 100644 index 00000000..51ee7b28 --- /dev/null +++ b/lib/dates.test.ts @@ -0,0 +1,5 @@ +import { localDateString } from "$lib/dates.ts"; + +Deno.test("Dates", () => { + console.log("Local date string", localDateString(new Date())); +}); diff --git a/lib/dates.ts b/lib/dates.ts index c6176b87..892b62fe 100644 --- a/lib/dates.ts +++ b/lib/dates.ts @@ -1,22 +1,21 @@ export function niceDate(d: Date): string { - function pad(n: number) { - let s = String(n); - if (s.length === 1) { - s = "0" + s; - } - return s; - } - - return d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate()); + return localDateString(d).split("T")[0]; } export function niceTime(d: Date): string { - const isoDate = d.toISOString(); - const [_, time] = isoDate.split("T"); - // hh:mm:ss - return time.split(".")[0]; + return localDateString(d).split("T")[1]; } export function safeTime(d: Date): string { return niceTime(d).replace(/:/g, "-"); } + +export function localDateString(d: Date): string { + return d.getFullYear() + + "-" + String(d.getMonth() + 1).padStart(2, "0") + + "-" + String(d.getDate()).padStart(2, "0") + + "T" + String(d.getHours()).padStart(2, "0") + + ":" + String(d.getMinutes()).padStart(2, "0") + + ":" + String(d.getSeconds()).padStart(2, "0") + + "." + String(d.getMilliseconds()).padStart(3, "0"); +} diff --git a/plug-api/lib/json.ts b/plug-api/lib/json.ts index 5e379258..f26b7000 100644 --- a/plug-api/lib/json.ts +++ b/plug-api/lib/json.ts @@ -1,3 +1,5 @@ +import { localDateString } from "$lib/dates.ts"; + /** * Performs a deep comparison of two objects, returning true if they are equal * @param a first object @@ -58,7 +60,7 @@ export function cleanStringDate(d: Date): string { String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0"); } else { - return d.toISOString(); + return localDateString(d); } } diff --git a/plugs/editor/complete.ts b/plugs/editor/complete.ts index 53d76213..31286a2c 100644 --- a/plugs/editor/complete.ts +++ b/plugs/editor/complete.ts @@ -9,6 +9,7 @@ import { listFilesCached } from "../federation/federation.ts"; import { queryObjects } from "../index/plug_api.ts"; import { folderName } from "@silverbulletmd/silverbullet/lib/resolve"; import type { LinkObject } from "../index/page_links.ts"; +import { localDateString } from "$lib/dates.ts"; // A meta page is a page tagged with either #template or #meta const isMetaPageFilter: QueryExpression = ["or", ["=", ["attr", "tags"], [ @@ -204,7 +205,7 @@ function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta { ref: fileMeta.name, tag: "page", name, - created: new Date(fileMeta.created).toISOString(), - lastModified: new Date(fileMeta.lastModified).toISOString(), + created: localDateString(new Date(fileMeta.created)), + lastModified: localDateString(new Date(fileMeta.lastModified)), } as PageMeta; } diff --git a/web/cm_plugins/editor_paste.ts b/web/cm_plugins/editor_paste.ts index f32dbde1..54b39189 100644 --- a/web/cm_plugins/editor_paste.ts +++ b/web/cm_plugins/editor_paste.ts @@ -17,6 +17,7 @@ import { import { defaultLinkStyle, maximumAttachmentSize } from "../constants.ts"; import { safeRun } from "$lib/async.ts"; import { resolvePath } from "@silverbulletmd/silverbullet/lib/resolve"; +import { localDateString } from "$lib/dates.ts"; const turndownService = new TurndownService({ hr: "---", @@ -187,8 +188,7 @@ export function attachmentExtension(editor: Client) { } const fileType = file.type; const ext = fileType.split("/")[1]; - const fileName = new Date() - .toISOString() + const fileName = localDateString(new Date()) .split(".")[0] .replace("T", "_") .replaceAll(":", "-");