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

fix: mixed ISO / non-ISO date & time formats #1222

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 2 additions & 6 deletions src/components/settings/GeneralSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export default class GeneralSettings extends Mixins(StateMixin) {
return Object.entries(DateFormats)
.map(([key, entry]) => ({
value: key,
text: `${date.toLocaleDateString(entry.locale ?? this.$i18n.locale, entry.options)}${entry.suffix ?? ''}`
text: `${date.toLocaleDateString(entry.locales ?? this.$filters.getNavigatorLocales(), entry.options)}${entry.suffix ?? ''}`
}))
}

Expand All @@ -282,7 +282,7 @@ export default class GeneralSettings extends Mixins(StateMixin) {
return Object.entries(TimeFormats)
.map(([key, entry]) => ({
value: key,
text: `${date.toLocaleTimeString(entry.locale ?? this.$i18n.locale, entry.options)}${entry.suffix ?? ''}`
text: `${date.toLocaleTimeString(entry.locales ?? this.$filters.getNavigatorLocales(), entry.options)}${entry.suffix ?? ''}`
}))
}

Expand Down Expand Up @@ -433,9 +433,5 @@ export default class GeneralSettings extends Mixins(StateMixin) {
server: true
})
}

get current_time () {
return Math.floor(Date.now() / 1000)
}
}
</script>
8 changes: 4 additions & 4 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ export const SupportedVideoFormats = Object.freeze([
'.mpg'
])

type DateTimeFormat = {
locale?: string,
export type DateTimeFormat = {
locales?: Intl.LocalesArgument,
options: Intl.DateTimeFormatOptions,
suffix?: string
}

export const DateFormats = Object.freeze<Record<string, DateTimeFormat>>({
iso: {
locale: 'lt',
locales: 'lt',
options: { day: '2-digit', month: '2-digit', year: 'numeric' },
suffix: ' (ISO 8601)'
},
Expand All @@ -516,7 +516,7 @@ export const DateFormats = Object.freeze<Record<string, DateTimeFormat>>({

export const TimeFormats = Object.freeze<Record<string, DateTimeFormat>>({
iso: {
locale: 'lt',
locales: 'lt',
options: { hour: '2-digit', minute: '2-digit', hour12: false },
suffix: ' (ISO 8601)'
},
Expand Down
29 changes: 19 additions & 10 deletions src/plugins/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import VueRouter from 'vue-router'
import { camelCase, startCase, capitalize, isFinite } from 'lodash-es'
import type { ApiConfig, TextSortOrder } from '@/store/config/types'
import { TinyColor } from '@ctrl/tinycolor'
import { DateFormats, Globals, TimeFormats, Waits } from '@/globals'
import { DateFormats, Globals, TimeFormats, Waits, type DateTimeFormat } from '@/globals'
import i18n from '@/plugins/i18n'
import type { TranslateResult } from 'vue-i18n'
import store from '@/store'
Expand Down Expand Up @@ -67,16 +67,20 @@ export const Filters = {
return (isNeg) ? '-' + r : r
},

getDateFormat: (override?: string) => {
getNavigatorLocales: () => {
return navigator.languages ?? [navigator.language]
},

getDateFormat: (override?: string): DateTimeFormat => {
return {
locale: i18n.locale,
locales: Filters.getNavigatorLocales(),
...DateFormats[override ?? store.state.config.uiSettings.general.dateFormat]
}
},

getTimeFormat: (override?: string) => {
getTimeFormat: (override?: string): DateTimeFormat => {
return {
locale: i18n.locale,
locales: Filters.getNavigatorLocales(),
...TimeFormats[override ?? store.state.config.uiSettings.general.timeFormat]
}
},
Expand All @@ -85,7 +89,7 @@ export const Filters = {
const date = new Date(value)
const dateFormat = Filters.getDateFormat()

return date.toLocaleDateString(dateFormat.locale, {
return date.toLocaleDateString(dateFormat.locales, {
...dateFormat.options,
...options
})
Expand All @@ -95,7 +99,7 @@ export const Filters = {
const date = new Date(value)
const timeFormat = Filters.getTimeFormat()

return date.toLocaleTimeString(timeFormat.locale, {
return date.toLocaleTimeString(timeFormat.locales, {
...timeFormat.options,
...options
})
Expand All @@ -109,11 +113,16 @@ export const Filters = {
},

formatDateTime: (value: number | string | Date, options?: Intl.DateTimeFormatOptions) => {
const date = new Date(value)
const timeFormat = Filters.getTimeFormat()
const dateFormat = Filters.getDateFormat()

return date.toLocaleDateString(dateFormat.locale, {
if (timeFormat.locales !== dateFormat.locales) {
return Filters.formatDate(value, options) + ' ' + Filters.formatTime(value, options)
}

const date = new Date(value)

return date.toLocaleDateString(dateFormat.locales, {
...dateFormat.options,
...timeFormat.options,
...options
Expand Down Expand Up @@ -148,7 +157,7 @@ export const Filters = {
},

formatRelativeTime (value: number, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions) {
const rtf = new Intl.RelativeTimeFormat(i18n.locale, {
const rtf = new Intl.RelativeTimeFormat(Filters.getNavigatorLocales() as string[], {
numeric: 'auto',
...options
})
Expand Down