mirror of
https://github.com/leon-ai/leon.git
synced 2024-11-28 12:43:35 +03:00
refactor(server): date helper to static class
This commit is contained in:
parent
9065d713e9
commit
afec850fc9
@ -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 { DATE } from '@/helpers/date'
|
||||
import { DateHelper } from '@/helpers/date-helper'
|
||||
|
||||
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 ${DATE.getTimeZone()}`)
|
||||
LOG.success(`The current time zone is ${DateHelper.getTimeZone()}`)
|
||||
|
||||
const sLogger = !HAS_LOGGER ? 'disabled' : 'enabled'
|
||||
LOG.success(`Collaborative logger ${sLogger}`)
|
||||
|
@ -8,20 +8,12 @@ import { LOG } from '@/helpers/log'
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
|
||||
class DateHelper {
|
||||
private static instance: DateHelper
|
||||
|
||||
constructor() {
|
||||
if (DateHelper.instance == null) {
|
||||
DateHelper.instance = this
|
||||
}
|
||||
}
|
||||
|
||||
export class DateHelper {
|
||||
/**
|
||||
* Get date time
|
||||
* @example getDateTime() // 2022-09-12T12:42:57+08:00
|
||||
*/
|
||||
public getDateTime() {
|
||||
public static getDateTime() {
|
||||
return dayjs().tz(this.getTimeZone()).format()
|
||||
}
|
||||
|
||||
@ -29,7 +21,7 @@ class DateHelper {
|
||||
* Get time zone
|
||||
* @example getTimeZone() // Asia/Shanghai
|
||||
*/
|
||||
public getTimeZone() {
|
||||
public static getTimeZone() {
|
||||
let { timeZone } = Intl.DateTimeFormat().resolvedOptions()
|
||||
|
||||
if (TIME_ZONE) {
|
||||
@ -47,5 +39,3 @@ class DateHelper {
|
||||
return timeZone as string
|
||||
}
|
||||
}
|
||||
|
||||
export const DATE = new DateHelper()
|
@ -2,7 +2,7 @@ import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { IS_TESTING_ENV } from '@/constants'
|
||||
import { DATE } from '@/helpers/date'
|
||||
import { DateHelper } from '@/helpers/date-helper'
|
||||
|
||||
class LogHelper {
|
||||
static readonly ERRORS_PATH = path.join(
|
||||
@ -54,7 +54,7 @@ class LogHelper {
|
||||
* Log message on stderr and write in error log file
|
||||
*/
|
||||
public error(value: string) {
|
||||
const data = `${DATE.getDateTime()} - ${value}`
|
||||
const data = `${DateHelper.getDateTime()} - ${value}`
|
||||
|
||||
if (!IS_TESTING_ENV) {
|
||||
if (fs.existsSync(LogHelper.ERRORS_PATH)) {
|
||||
|
@ -1,11 +1,11 @@
|
||||
import moment from 'moment-timezone'
|
||||
|
||||
import { DATE } from '@/helpers/date'
|
||||
import { DateHelper } from '@/helpers/date-helper'
|
||||
|
||||
describe('date helper', () => {
|
||||
describe('dateTime()', () => {
|
||||
test('returns date time with UTC', () => {
|
||||
expect(DATE.getDateTime()).toBe(
|
||||
expect(DateHelper.getDateTime()).toBe(
|
||||
moment().tz(global.date.time_zone).format()
|
||||
)
|
||||
})
|
||||
@ -13,7 +13,7 @@ describe('date helper', () => {
|
||||
|
||||
describe('timeZone()', () => {
|
||||
test('returns time zone', () => {
|
||||
expect(DATE.getTimeZone()).toBe(global.date.time_zone)
|
||||
expect(DateHelper.getTimeZone()).toBe(global.date.time_zone)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user