eaf80c6bb4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import { TIME_ZONE } from "./format";
|
||
|
||
/**
|
||
* İstanbul günü yardımcıları. Veritabanında UTC saklanır; gün sınırları İstanbul'a göredir.
|
||
* Türkiye 2016'dan beri sabit UTC+3 kullanır; yine de dönüşüm Intl ile yapılır (kural değişirse
|
||
* yalnızca ofset hesabı güncellenir).
|
||
*/
|
||
|
||
const ymdFmt = new Intl.DateTimeFormat("en-CA", {
|
||
timeZone: TIME_ZONE,
|
||
year: "numeric",
|
||
month: "2-digit",
|
||
day: "2-digit",
|
||
});
|
||
|
||
/** Bir anın İstanbul'daki takvim günü: "2026-11-05". */
|
||
export function toIstanbulYmd(date: Date | string | number): string {
|
||
return ymdFmt.format(new Date(date));
|
||
}
|
||
|
||
/** Bugünün İstanbul günü. */
|
||
export function todayIstanbul(now: Date = new Date()): string {
|
||
return toIstanbulYmd(now);
|
||
}
|
||
|
||
/** İstanbul'un verilen andaki UTC ofseti (dakika; +180). */
|
||
function istanbulOffsetMinutes(at: Date): number {
|
||
const parts = new Intl.DateTimeFormat("en-US", {
|
||
timeZone: TIME_ZONE,
|
||
hourCycle: "h23",
|
||
year: "numeric",
|
||
month: "2-digit",
|
||
day: "2-digit",
|
||
hour: "2-digit",
|
||
minute: "2-digit",
|
||
}).formatToParts(at);
|
||
const get = (t: string) => Number(parts.find((p) => p.type === t)?.value);
|
||
const asUtc = Date.UTC(get("year"), get("month") - 1, get("day"), get("hour"), get("minute"));
|
||
return Math.round((asUtc - at.getTime()) / 60_000);
|
||
}
|
||
|
||
/** "2026-11-05" → o günün İstanbul'daki başlangıç anı (UTC Date). Geçersizse null. */
|
||
export function istanbulDayStart(ymd: string): Date | null {
|
||
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(ymd);
|
||
if (!m) return null;
|
||
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
||
const guess = new Date(Date.UTC(y, mo - 1, d));
|
||
if (guess.getUTCFullYear() !== y || guess.getUTCMonth() !== mo - 1 || guess.getUTCDate() !== d)
|
||
return null;
|
||
return new Date(guess.getTime() - istanbulOffsetMinutes(guess) * 60_000);
|
||
}
|