From f21e43c16a27a6746ef7ec9e50faf59df405d0fd Mon Sep 17 00:00:00 2001 From: louistiti Date: Wed, 21 Sep 2022 20:43:50 +0800 Subject: [PATCH] refactor(server): date helper to singleton --- server/src/core/http-server/server.js | 4 +- server/src/helpers/date.ts | 70 ++++++++++++++++----------- server/src/helpers/log.ts | 4 +- test/unit/server/helpers/date.spec.js | 8 +-- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/server/src/core/http-server/server.js b/server/src/core/http-server/server.js index f890484d..388a6067 100644 --- a/server/src/core/http-server/server.js +++ b/server/src/core/http-server/server.js @@ -27,7 +27,7 @@ import keyMidd from '@/core/http-server/plugins/key' import infoPlugin from '@/core/http-server/api/info' import downloadsPlugin from '@/core/http-server/api/downloads' import { LOG } from '@/helpers/log' -import { getTimeZone } from '@/helpers/date' +import { DATE } from '@/helpers/date' const server = {} @@ -378,7 +378,7 @@ server.init = async () => { LOG.success(`The current env is ${process.env.LEON_NODE_ENV}`) LOG.success(`The current version is ${version}`) - LOG.success(`The current time zone is ${getTimeZone()}`) + LOG.success(`The current time zone is ${DATE.getTimeZone()}`) const sLogger = !HAS_LOGGER ? 'disabled' : 'enabled' LOG.success(`Collaborative logger ${sLogger}`) diff --git a/server/src/helpers/date.ts b/server/src/helpers/date.ts index 29efced9..ac4b0e58 100644 --- a/server/src/helpers/date.ts +++ b/server/src/helpers/date.ts @@ -8,34 +8,50 @@ import { LOG } from '@/helpers/log' dayjs.extend(utc) dayjs.extend(timezone) -/** - * Get date time - * - * @example getDateTime() // 2022-09-12T12:42:57+08:00 - */ -export function getDateTime() { - return dayjs().tz(getTimeZone()).format() -} +class Date { + private static instance: Date -/** - * Get time zone - * - * @example getTimeZone() // Asia/Shanghai - */ -export function getTimeZone() { - let { timeZone } = Intl.DateTimeFormat().resolvedOptions() - - if (TIME_ZONE) { - // Verify if the time zone is valid - try { - Intl.DateTimeFormat(undefined, { timeZone: TIME_ZONE }) - timeZone = TIME_ZONE - } catch (e) { - LOG.warning( - `The time zone "${TIME_ZONE}" is not valid. Falling back to "${timeZone}"` - ) - } + private constructor() { + // Singleton } - return timeZone + public static getInstance() { + if (Date.instance == null) { + Date.instance = new Date() + } + + return Date.instance + } + + /** + * Get date time + * @example getDateTime() // 2022-09-12T12:42:57+08:00 + */ + public getDateTime() { + return dayjs().tz(this.getTimeZone()).format() + } + + /** + * Get time zone + * @example getTimeZone() // Asia/Shanghai + */ + public getTimeZone() { + let { timeZone } = Intl.DateTimeFormat().resolvedOptions() + + if (TIME_ZONE) { + // Verify if the time zone is valid + try { + Intl.DateTimeFormat(undefined, { timeZone: TIME_ZONE }) + timeZone = TIME_ZONE + } catch (e) { + LOG.warning( + `The time zone "${TIME_ZONE}" is not valid. Falling back to "${timeZone}"` + ) + } + } + + return timeZone as string + } } + +export const DATE = Date.getInstance() diff --git a/server/src/helpers/log.ts b/server/src/helpers/log.ts index da8ceab9..562baf16 100644 --- a/server/src/helpers/log.ts +++ b/server/src/helpers/log.ts @@ -2,7 +2,7 @@ import fs from 'node:fs' import path from 'node:path' import { IS_TESTING_ENV } from '@/constants' -import { getDateTime } from '@/helpers/date' +import { DATE } from '@/helpers/date' class Log { static readonly ERRORS_PATH = path.join( @@ -63,7 +63,7 @@ class Log { * TODO */ public error(value: string) { - const data = `${getDateTime()} - ${value}` + const data = `${DATE.getDateTime()} - ${value}` if (!IS_TESTING_ENV) { if (fs.existsSync(Log.ERRORS_PATH)) { diff --git a/test/unit/server/helpers/date.spec.js b/test/unit/server/helpers/date.spec.js index ad5a57bd..e3e633b0 100644 --- a/test/unit/server/helpers/date.spec.js +++ b/test/unit/server/helpers/date.spec.js @@ -1,17 +1,19 @@ import moment from 'moment-timezone' -import { getDateTime, getTimeZone } from '@/helpers/date' +import { DATE } from '@/helpers/date' describe('date helper', () => { describe('dateTime()', () => { test('returns date time with UTC', () => { - expect(getDateTime()).toBe(moment().tz(global.date.time_zone).format()) + expect(DATE.getDateTime()).toBe( + moment().tz(global.date.time_zone).format() + ) }) }) describe('timeZone()', () => { test('returns time zone', () => { - expect(getTimeZone()).toBe(global.date.time_zone) + expect(DATE.getTimeZone()).toBe(global.date.time_zone) }) }) })