Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Fix for Finnish time format and fallback #29

Merged
merged 1 commit into from
May 18, 2021
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
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ class SunCard extends LitElement {
return (date.getHours() * 60) + date.getMinutes()
}

parseTime (timeText: string) {
const regex = /\d{1,2}:\d{1,2}|[AMP]+/g
parseTime (timeText: string, locale?: string) {
const regex = /\d{1,2}[:.]\d{1,2}|[AMP]+/g
const date = new Date(timeText)
const { language, timeFormat } = this.getConfig()
const [time, period] = date.toLocaleTimeString(language, { hour12: timeFormat === '12h' }).match(regex) as [string, ('AM' | 'PM')?]
const result = date.toLocaleTimeString(locale ?? language, { hour12: timeFormat === '12h' }).match(regex) as [string, ('AM' | 'PM')?]

if (!result && !locale) {
return this.parseTime(timeText, Constants.DEFAULT_CONFIG.language)
}

const [time, period] = result
return { time, period }
}

Expand Down