From 1e4379f63ce7fde2e3bc8b2ee6c558cd7336ed51 Mon Sep 17 00:00:00 2001 From: Yaroslav Zotov <35657233+zotovy@users.noreply.github.com> Date: Sun, 27 Mar 2022 14:39:11 +0500 Subject: [PATCH] fix: add timezoneOffset to algorithm --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 ++++++ .../dev/zotov/phototime/solarized/Algorithm.kt | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index ef61796..860da66 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..4c6280e 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,5 +1,11 @@ + + + + + + diff --git a/library/src/main/java/dev/zotov/phototime/solarized/Algorithm.kt b/library/src/main/java/dev/zotov/phototime/solarized/Algorithm.kt index 5479396..fd23285 100644 --- a/library/src/main/java/dev/zotov/phototime/solarized/Algorithm.kt +++ b/library/src/main/java/dev/zotov/phototime/solarized/Algorithm.kt @@ -2,7 +2,11 @@ package dev.zotov.phototime.solarized import androidx.annotation.FloatRange import java.time.LocalDateTime +import java.time.ZoneId import java.time.ZoneOffset +import java.time.temporal.TemporalUnit +import java.util.* +import java.util.concurrent.TimeUnit import kotlin.math.* /** @@ -62,7 +66,8 @@ internal fun algorithm( val zenith = -1 * twilight.degrees + 90 // local hour angle - val cosH = (cos(zenith.radians) - (sinDec * sin(latitude.radians))) / (cosDec * cos(latitude.radians)) + val cosH = + (cos(zenith.radians) - (sinDec * sin(latitude.radians))) / (cosDec * cos(latitude.radians)) // no transition if (cosH > 1 || cosH < -1) return null @@ -86,7 +91,12 @@ internal fun algorithm( else -> date } - return setDate.withHour(hour).withMinute(minute).withSecond(second) + val timezoneOffset = TimeUnit.HOURS.convert( + TimeZone.getDefault().rawOffset.toLong(), + TimeUnit.MILLISECONDS + ) + + return setDate.withHour(hour).withMinute(minute).withSecond(second).plusHours(timezoneOffset) }