-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import * as proto from "@bufbuild/protobuf";
import { TZDate, tz } from "@date-fns/tz";
import { DateSchema } from "@yamori/proto/yamori/type/v1/date_pb.js";
import { startOfDay, type DateArg } from "date-fns";
// 全ての日付は JST で計算される
const TZ = "Asia/Tokyo";
export function fromProtoDate(d: proto.MessageShape<typeof DateSchema>): Date {
return TZDate.tz(TZ, d.year, d.month - 1, d.day, 0, 0, 0);
}
export function toProtoDate<D extends Date>(
d: DateArg<D>,
): proto.MessageShape<typeof DateSchema> {
const start = startOfDay(d, { in: tz(TZ) });
return proto.create(DateSchema, {
year: start.getFullYear(),
month: start.getMonth() + 1,
day: start.getDate(),
});
}
export function isSameDate(
a: proto.MessageShape<typeof DateSchema>,
b: proto.MessageShape<typeof DateSchema>,
): boolean {
return a.year === b.year && a.month === b.month && a.day === b.day;
}