summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/format-time-string.ts
diff options
context:
space:
mode:
authorsyuilo <4439005+syuilo@users.noreply.github.com>2025-03-09 14:28:01 +0900
committersyuilo <4439005+syuilo@users.noreply.github.com>2025-03-09 14:28:01 +0900
commitbe7e3b9a0cb81b78a744993fef2fa2fd2833fa9c (patch)
treec82e18ce93ec0a24c57d7e36eb54a09266b3a25b /packages/frontend/src/scripts/format-time-string.ts
parentenhnace(frontend): 文字列比較のためのローマナイズを強化(... (diff)
downloadsharkey-be7e3b9a0cb81b78a744993fef2fa2fd2833fa9c.tar.gz
sharkey-be7e3b9a0cb81b78a744993fef2fa2fd2833fa9c.tar.bz2
sharkey-be7e3b9a0cb81b78a744993fef2fa2fd2833fa9c.zip
refactor(frontend): scripts -> utility
Diffstat (limited to 'packages/frontend/src/scripts/format-time-string.ts')
-rw-r--r--packages/frontend/src/scripts/format-time-string.ts55
1 files changed, 0 insertions, 55 deletions
diff --git a/packages/frontend/src/scripts/format-time-string.ts b/packages/frontend/src/scripts/format-time-string.ts
deleted file mode 100644
index d383f143e1..0000000000
--- a/packages/frontend/src/scripts/format-time-string.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * SPDX-FileCopyrightText: syuilo and misskey-project
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-const defaultLocaleStringFormats: { [index: string]: string } = {
- 'weekday': 'narrow',
- 'era': 'narrow',
- 'year': 'numeric',
- 'month': 'numeric',
- 'day': 'numeric',
- 'hour': 'numeric',
- 'minute': 'numeric',
- 'second': 'numeric',
- 'timeZoneName': 'short',
-};
-
-function formatLocaleString(date: Date, format: string): string {
- return format.replace(/\{\{(\w+)(:(\w+))?\}\}/g, (match: string, kind: string, unused?, option?: string) => {
- if (['weekday', 'era', 'year', 'month', 'day', 'hour', 'minute', 'second', 'timeZoneName'].includes(kind)) {
- return date.toLocaleString(window.navigator.language, { [kind]: option ? option : defaultLocaleStringFormats[kind] });
- } else {
- return match;
- }
- });
-}
-
-export function formatDateTimeString(date: Date, format: string): string {
- return format
- .replace(/yyyy/g, date.getFullYear().toString())
- .replace(/yy/g, date.getFullYear().toString().slice(-2))
- .replace(/MMMM/g, date.toLocaleString(window.navigator.language, { month: 'long' }))
- .replace(/MMM/g, date.toLocaleString(window.navigator.language, { month: 'short' }))
- .replace(/MM/g, (`0${date.getMonth() + 1}`).slice(-2))
- .replace(/M/g, (date.getMonth() + 1).toString())
- .replace(/dd/g, (`0${date.getDate()}`).slice(-2))
- .replace(/d/g, date.getDate().toString())
- .replace(/HH/g, (`0${date.getHours()}`).slice(-2))
- .replace(/H/g, date.getHours().toString())
- .replace(/hh/g, (`0${(date.getHours() % 12) || 12}`).slice(-2))
- .replace(/h/g, ((date.getHours() % 12) || 12).toString())
- .replace(/mm/g, (`0${date.getMinutes()}`).slice(-2))
- .replace(/m/g, date.getMinutes().toString())
- .replace(/ss/g, (`0${date.getSeconds()}`).slice(-2))
- .replace(/s/g, date.getSeconds().toString())
- .replace(/tt/g, date.getHours() >= 12 ? 'PM' : 'AM');
-}
-
-export function formatTimeString(date: Date, format: string): string {
- return format.replace(/\[(([^\[]|\[\])*)\]|(([yMdHhmst])\4{0,3})/g, (match: string, localeformat?: string, unused?, datetimeformat?: string) => {
- if (localeformat) return formatLocaleString(date, localeformat);
- if (datetimeformat) return formatDateTimeString(date, datetimeformat);
- return match;
- });
-}