Skip to content

Commit

Permalink
feat: better time format inference
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Jun 15, 2024
1 parent 7b53414 commit 3c23225
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ All of these parameters are optional.
- `temperature`: `celsius` (default) / `fahrenheit`
- `wind_speed`: `mps` (default) / `kmph` / `mph` / `knot`
Note that `mph` means miles per hour, while `mps` means meters per second.
- `time_format`: `auto` (default) / `12h` / `24h` / `24hn`
`24hn` means 24-hour notation with no leading zero.
- `time_format`: `auto` (default) / `12h` / `24h` / `24hn` / `native`
`24hn` means 24-hour notation with no leading zero.
`native` means the native datetime format (including year) of the language.
- `lang`: `auto` (default) / BCP 47 tag, e.g., `en-US`
Specifies the language of the weather information, the datetime format, and the location name if `location` not provided.
Detected automatically from `Accept-Language` header if `auto` is specified.
Expand Down
40 changes: 37 additions & 3 deletions src/widget/WeatherWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,45 @@ function OpacityAnimation({
);
}

function detectTimeFormat(language: string): "12h" | "24h" | "24hn" | null {
const strDate1 = new Date("2024-01-03T17:07:09Z").toLocaleTimeString(
language,
{
timeZone: "UTC",
timeStyle: "short",
}
);

const has5 = strDate1.includes("5");
const has17 = strDate1.includes("17");
if (has5 === has17) {
return null;
}

if (has5) {
return "12h";
}

const strDate2 = new Date("2024-01-03T05:07:09Z").toLocaleTimeString(
language,
{
timeZone: "UTC",
timeStyle: "short",
}
);
return strDate2.includes("05") ? "24h" : "24hn";
}

function formatDateTime(
datetime: string,
language: string,
prefTimeFormat: PreferencesSchema["time_format"]
): [weekday: string, dateTime: string] {
const timeFormat =
prefTimeFormat === "auto"
? detectTimeFormat(language) ?? "native"
: prefTimeFormat;

const date = new Date(datetime + "Z");
return [
date.toLocaleString(language, {
Expand All @@ -86,13 +120,13 @@ function formatDateTime(
}),
date.toLocaleString(language, {
timeZone: "UTC",
...(prefTimeFormat === "auto"
...(timeFormat === "native"
? { dateStyle: "medium", timeStyle: "short" }
: {
month: "short",
day: "numeric",
hour: prefTimeFormat === "24h" ? "2-digit" : "numeric",
hour12: prefTimeFormat === "12h",
hour: timeFormat === "24h" ? "2-digit" : "numeric",
hour12: timeFormat === "12h",
minute: "2-digit",
}),
}),
Expand Down
5 changes: 4 additions & 1 deletion src/widget/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const preferencesSchema = object({
),
"mps"
),
time_format: nullish(picklist(["auto", "12h", "24h", "24hn"]), "auto"),
time_format: nullish(
picklist(["auto", "12h", "24h", "24hn", "native"]),
"auto"
),
lang: pipe(
string("lang must be a string"),
custom(isValidLanguageTag, "invalid lang string specified")
Expand Down

0 comments on commit 3c23225

Please sign in to comment.