Fixes #680 all times and dates are now local (to the environment)

pull/1054/head
Zef Hemel 2024-08-18 13:04:51 +02:00
parent 6ef9dc8b99
commit 3ac21498f3
6 changed files with 30 additions and 22 deletions

View File

@ -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);

5
lib/dates.test.ts Normal file
View File

@ -0,0 +1,5 @@
import { localDateString } from "$lib/dates.ts";
Deno.test("Dates", () => {
console.log("Local date string", localDateString(new Date()));
});

View File

@ -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");
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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(":", "-");