first commit
This commit is contained in:
40
app/api/sun-times/route.ts
Normal file
40
app/api/sun-times/route.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export interface SunTimes {
|
||||
date: string;
|
||||
sunrise: string;
|
||||
sunset: string;
|
||||
}
|
||||
|
||||
// Calcul approximatif du lever/coucher du soleil pour Fakarava
|
||||
// Coordonnées de Fakarava (Rotoava) : -16.3167, -145.6167
|
||||
// En production, utiliser une API comme https://sunrise-sunset.org/api
|
||||
const calculateSunTimes = (date: Date, lat: number = -16.3167, lng: number = -145.6167): SunTimes => {
|
||||
// Calcul simplifié (formule approximative)
|
||||
const dayOfYear = Math.floor((date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000);
|
||||
const declination = 23.45 * Math.sin((360 * (284 + dayOfYear) / 365) * Math.PI / 180);
|
||||
const hourAngle = Math.acos(-Math.tan(lat * Math.PI / 180) * Math.tan(declination * Math.PI / 180)) * 180 / Math.PI;
|
||||
|
||||
const sunriseHour = 12 - hourAngle / 15 - (lng / 15);
|
||||
const sunsetHour = 12 + hourAngle / 15 - (lng / 15);
|
||||
|
||||
const formatTime = (hour: number) => {
|
||||
const h = Math.floor(hour);
|
||||
const m = Math.floor((hour - h) * 60);
|
||||
return `${h.toString().padStart(2, "0")}:${m.toString().padStart(2, "0")}`;
|
||||
};
|
||||
|
||||
return {
|
||||
date: date.toISOString().split("T")[0],
|
||||
sunrise: formatTime(sunriseHour),
|
||||
sunset: formatTime(sunsetHour),
|
||||
};
|
||||
};
|
||||
|
||||
export async function GET() {
|
||||
const today = new Date();
|
||||
const sunTimes = calculateSunTimes(today);
|
||||
|
||||
return NextResponse.json(sunTimes);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user