Browse Source

日づけ不詳の表示修正

feature/061
みてるぞ 1 week ago
parent
commit
790f39e95b
1 changed files with 56 additions and 9 deletions
  1. +56
    -9
      frontend/src/lib/utils.ts

+ 56
- 9
frontend/src/lib/utils.ts View File

@@ -10,17 +10,64 @@ export const toDate = (d: string | Date): Date => typeof d === 'string' ? new Da
export const cn = (...inputs: ClassValue[]) => twMerge (clsx (...inputs))


export const dateString = (d: string | Date): string =>
toDate (d).toLocaleString ('ja-JP-u-ca-japanese')
export const dateString = (
d: string | Date,
unknown: 'month' | 'day' | 'hour' | 'minute' | 'second' | null = null,
): string =>
toDate (d).toLocaleString (
'ja-JP-u-ca-japanese',
{ era: 'long',
year: 'numeric',
month: (unknown === 'month' ? undefined : 'long'),
day: unknown != null && ['month', 'day'].includes (unknown) ? undefined : 'numeric',
weekday: unknown != null && ['month', 'day'].includes (unknown) ? undefined : 'short',
hour: unknown == null || ['second', 'minute'].includes (unknown) ? 'numeric' : undefined,
minute: unknown == null || unknown === 'second' ? 'numeric' : undefined,
second: unknown == null ? 'numeric' : undefined })


// TODO: 表示形式きしょすぎるので何とかする
export const originalCreatedAtString = (
f: string | Date | null,
b: string | Date | null,
): string =>
([f ? `${ dateString (f) } 以降` : '',
b ? `${ dateString (b) } より前` : '']
.filter (Boolean)
.join (' '))
|| '不明'
): string => {
const from = f ? toDate (f) : null
const before = b ? toDate (b) : null

if (from && before)
{
const diff = before.getTime () - from.getTime ()

if (diff <= 60_000 /* 1 分 */)
return dateString (from, 'second')

if (from.getMinutes () === 0 && before.getMinutes () === 0)
{
if (Math.abs (diff - 3_600_000 /* 1 時間 */) < 60_000)
return dateString (from, 'minute') + ' (分不詳)'

if (from.getHours () === 0 && before.getHours () === 0)
{
if (Math.abs (diff - 86_400_000) < 60_000)
return dateString (from, 'hour') + ' (時刻不詳)'

if (from.getDate () === 1 && before.getDate () === 1)
{
if (2_419_200_000 /* 28 日 */ <= diff && diff < 2_764_800_000 /* 32 日 */)
return dateString (from, 'day') + ' (日不詳)'

if (from.getMonth () === 0 && before.getMonth () === 0
&& (31_536_000_000 /* 365 日 */ <= diff
&& diff < 31_708_800_000 /* 367 日 */))
return dateString (from, 'month') + ' (月日不詳)'
}
}
}
}

const rtn = ([from ? `${ dateString (from, 'second') }` : '',
'~',
before ? `${ dateString (new Date (before.getTime () - 60_000), 'second') }` : '']
.filter (Boolean)
.join (' '))
return rtn === '~' ? '年月日不詳' : rtn
}

Loading…
Cancel
Save