Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mimir 1873 lib time more formats #2115

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/main/resources/lib/ssb/utils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ export function sameDay(d1: Date, d2: Date): boolean {
export function formatDate(date: string | undefined, formatType: string, language?: string): string | undefined {
if (date) {
const parsedDate: Date = parseISO(date)
let parsedDateWithTimeZone = parsedDate

// If date has no specified timezone, add server offset so it become Europe/Oslo.
// If no timezone is specified, the parseISO function will assume the date is in local time, which for the server is UTC (Z, GMT+0)
if (!date.match(/(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/gm)) {
const serverOffsetInMs: number = app.config?.['serverOffsetInMs'] ? parseInt(app.config['serverOffsetInMs']) : 0
parsedDateWithTimeZone = new Date(parsedDateWithTimeZone.getTime() + serverOffsetInMs)
}
const locale: object = language
? {
locale: language === 'en' ? enGB : language === 'nn' ? nn : nb,
}
: {}

let dateFnsResult

try {
dateFnsResult = format(parsedDate, formatType, locale)
} catch (e) {
Expand All @@ -55,12 +62,22 @@ export function formatDate(date: string | undefined, formatType: string, languag
let libTimePattern = formatType
// https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/time/format/DateTimeFormatter.html#patterns
if (formatType === 'PPP') libTimePattern = language === 'en' ? 'd MMMM u' : 'd. MMMM u'
const libTimeResult = libTimeFormatDate({
date: parsedDate.toISOString(),
pattern: libTimePattern,
locale: language,
timezoneId: 'Europe/Oslo',
})
if (formatType === 'PPpp') libTimePattern = language === 'en' ? 'd MMM u, HH:mm:ss' : 'd. MMM u HH:mm:ss'

let libTimeResult
try {
libTimeResult = libTimeFormatDate({
date: parsedDateWithTimeZone.toISOString(),
pattern: libTimePattern,
locale: language,
timezoneId: 'Europe/Oslo',
})
} catch (e) {
log.error(`Error in formatDate with Lib time, tried to format ${parsedDate} to ${libTimePattern}`)
return dateFnsResult
}

// log.info(`${date} -- ${formatType} -- date-fns: ${dateFnsResult}, lib-time: ${libTimeResult}`)

// Track errors in logs
if (dateFnsResult && dateFnsResult !== libTimeResult) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skulle ikke dateFnsResult brukes hvis disse 2 datoene var ulike?

Expand Down