optimization
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
Some checks failed
Deploy MyApp on Same Server / build-and-deploy (push) Failing after 1s
This commit is contained in:
51
src/lib/utils/sun-utils.ts
Normal file
51
src/lib/utils/sun-utils.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* محاسبه زمان طلوع و غروب خورشید برای یک موقعیت جغرافیایی
|
||||
* @param latitude عرض جغرافیایی (درجه)
|
||||
* @param longitude طول جغرافیایی (درجه)
|
||||
* @param timezoneOffset افست زمانی از UTC (ساعت)
|
||||
* @returns اطلاعات طلوع و غروب خورشید
|
||||
*/
|
||||
export function calculateSunTimes(
|
||||
latitude: number = 34.39674800, // کهک قم
|
||||
longitude: number = 50.86594800,
|
||||
timezoneOffset: number = 3.5 // ایران
|
||||
) {
|
||||
const now = new Date()
|
||||
const dayOfYear = Math.floor((now.getTime() - new Date(now.getFullYear(), 0, 0).getTime()) / 86400000)
|
||||
|
||||
// محاسبه انحراف خورشید (Solar Declination)
|
||||
const declination = -23.44 * Math.cos((2 * Math.PI / 365) * (dayOfYear + 10))
|
||||
|
||||
// محاسبه زاویه ساعتی طلوع (Hour Angle)
|
||||
const latRad = latitude * Math.PI / 180
|
||||
const decRad = declination * Math.PI / 180
|
||||
const cosHourAngle = -Math.tan(latRad) * Math.tan(decRad)
|
||||
|
||||
// در صورتی که خورشید طلوع/غروب میکند
|
||||
if (Math.abs(cosHourAngle) <= 1) {
|
||||
const hourAngle = Math.acos(cosHourAngle) * 180 / Math.PI
|
||||
|
||||
// زمان طلوع و غروب به ساعت محلی (با دقیقه دقیق)
|
||||
const sunriseDecimal = 12 - hourAngle / 15 + (longitude / 15 - timezoneOffset)
|
||||
const sunsetDecimal = 12 + hourAngle / 15 + (longitude / 15 - timezoneOffset)
|
||||
|
||||
// تبدیل به ساعت و دقیقه
|
||||
const sunriseHour = Math.floor(sunriseDecimal)
|
||||
const sunriseMinute = Math.round((sunriseDecimal - sunriseHour) * 60)
|
||||
|
||||
const sunsetHour = Math.floor(sunsetDecimal)
|
||||
const sunsetMinute = Math.round((sunsetDecimal - sunsetHour) * 60)
|
||||
|
||||
return {
|
||||
sunrise: { hour: sunriseHour, minute: sunriseMinute, decimal: sunriseDecimal },
|
||||
sunset: { hour: sunsetHour, minute: sunsetMinute, decimal: sunsetDecimal }
|
||||
}
|
||||
}
|
||||
|
||||
// مقادیر پیشفرض (برای روزهای قطبی)
|
||||
return {
|
||||
sunrise: { hour: 6, minute: 0, decimal: 6 },
|
||||
sunset: { hour: 18, minute: 0, decimal: 18 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user