1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-28 12:43:35 +03:00

refactor(server): date helper to singleton

This commit is contained in:
louistiti 2022-09-21 20:43:50 +08:00
parent 4f1683dc55
commit f21e43c16a
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
4 changed files with 52 additions and 34 deletions

View File

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

View File

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

View File

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

View File

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