Skip to content

Build your personal knowledge base with Trilium Notes

in web version use local client time instead of server time for recording dateModified etc.

zadam586d6b4557a1
diff --git a/src/public/app/services/server.js b/src/public/app/services/server.js
index 8167622ae9..7d886eb4ea 100644
--- a/src/public/app/services/server.js
+++ b/src/public/app/services/server.js
@@ -8,6 +8,7 @@ function getHeaders(headers) {
// also avoiding using underscores instead of dashes since nginx filters them out by default
const allHeaders = {
'trilium-source-id': glob.sourceId,
+ 'trilium-local-now-datetime': utils.localNowDateTime(),
'x-csrf-token': glob.csrfToken
};
diff --git a/src/public/app/services/tab_context.js b/src/public/app/services/tab_context.js
index dec5ebf0c1..9d46e2f6d7 100644
--- a/src/public/app/services/tab_context.js
+++ b/src/public/app/services/tab_context.js
@@ -58,6 +58,7 @@ class TabContext extends Component {
this.autoBookDisabled = false;
this.textPreviewDisabled = false;
+ this.codePreviewDisabled = false;
setTimeout(async () => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
diff --git a/src/public/app/services/utils.js b/src/public/app/services/utils.js
index ad490c9825..51244f38f4 100644
--- a/src/public/app/services/utils.js
+++ b/src/public/app/services/utils.js
@@ -40,6 +40,10 @@ function formatDateTime(date) {
return formatDate(date) + " " + formatTime(date);
}
+function localNowDateTime() {
+ return dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ')
+}
+
function now() {
return formatTimeWithSeconds(new Date());
}
@@ -321,6 +325,7 @@ export default {
formatDate,
formatDateISO,
formatDateTime,
+ localNowDateTime,
now,
isElectron,
isMac,
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 02bf1cabc5..13783ec35d 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -1,3 +1,5 @@
+"use strict";
+
const setupRoute = require('./setup');
const loginRoute = require('./login');
const indexRoute = require('./index');
@@ -82,6 +84,7 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
try {
const result = await cls.init(async () => {
cls.namespace.set('sourceId', req.headers['trilium-source-id']);
+ cls.namespace.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
protectedSessionService.setProtectedSessionId(req);
if (transactional) {
diff --git a/src/routes/setup.js b/src/routes/setup.js
index 0d8c4f9686..d57b619b93 100644
--- a/src/routes/setup.js
+++ b/src/routes/setup.js
@@ -3,10 +3,11 @@
const sqlInit = require('../services/sql_init');
const setupService = require('../services/setup');
const utils = require('../services/utils');
-const windowService = require('../services/window');
async function setupPage(req, res) {
if (await sqlInit.isDbInitialized()) {
+ const windowService = require('../services/window');
+
if (utils.isElectron()) {
await windowService.createMainWindow();
windowService.closeSetupWindow();
diff --git a/src/services/cls.js b/src/services/cls.js
index d0d3c8a170..39092b7a0b 100644
--- a/src/services/cls.js
+++ b/src/services/cls.js
@@ -13,6 +13,10 @@ function getSourceId() {
return namespace.get('sourceId');
}
+function getLocalNowDateTime() {
+ return namespace.get('localNowDateTime');
+}
+
function disableEntityEvents() {
namespace.set('disableEntityEvents', true);
}
@@ -50,6 +54,7 @@ module.exports = {
wrap,
namespace,
getSourceId,
+ getLocalNowDateTime,
disableEntityEvents,
isEntityEventsDisabled,
reset,
diff --git a/src/services/date_utils.js b/src/services/date_utils.js
index 1ea14a87c3..cc84ca5b00 100644
--- a/src/services/date_utils.js
+++ b/src/services/date_utils.js
@@ -1,17 +1,29 @@
const dayjs = require('dayjs');
+const cls = require('./cls');
function utcNowDateTime() {
return utcDateStr(new Date());
}
+// CLS date time is important in web deployments - server often runs in different time zone than user is located in
+// so we'd prefer client timezone to be used to record local dates. For this reason requests from client contain
+// "trilium-local-now-datetime" header which is then stored in CLS
function localNowDateTime() {
- return dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ')
+ return cls.getLocalNowDateTime()
+ || dayjs().format('YYYY-MM-DD HH:mm:ss.SSSZZ')
}
function localNowDate() {
- const date = new Date();
+ const clsDateTime = cls.getLocalNowDateTime();
- return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate());
+ if (clsDateTime) {
+ return clsDateTime.substr(0, 10);
+ }
+ else {
+ const date = new Date();
+
+ return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate());
+ }
}
function pad(num) {
diff --git a/src/services/env.js b/src/services/env.js
index 22fa4cbee9..e7fa6caf81 100644
--- a/src/services/env.js
+++ b/src/services/env.js
@@ -1,5 +1,5 @@
module.exports = {
isDev: function () {
- return process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev';
+ return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev');
}
};
\ No newline at end of file