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

Commit

Permalink
Merge pull request #29 from AitorDB/bugfix/fix-fi-time-format
Browse files Browse the repository at this point in the history
Fix for Finnish time format and fallback
  • Loading branch information
AitorDB committed May 18, 2021
2 parents c90054b + 800e69d commit 60dabd8
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit 60dabd8

Please sign in to comment.