Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #414 from Vishwa-Raghavendra/fix-issue-357
Browse files Browse the repository at this point in the history
Bug-Fix #357: Timezone error fixed
  • Loading branch information
ILIYANGERMANOV authored Jan 9, 2022
2 parents f49693e + 8dca9b3 commit 0edbfbc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
21 changes: 21 additions & 0 deletions app/src/main/java/com/ivy/wallet/base/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,32 @@ fun LocalTime.convertLocalToUTC(): LocalTime {
return this.minusSeconds(offset)
}

fun LocalTime.convertUTCToLocal(): LocalTime {
val offset = timeNowLocal().atZone(ZoneOffset.systemDefault()).offset.totalSeconds.toLong()
return this.plusSeconds(offset)
}

fun LocalDateTime.convertLocalToUTC(): LocalDateTime {
val offset = timeNowLocal().atZone(ZoneOffset.systemDefault()).offset.totalSeconds.toLong()
return this.minusSeconds(offset)
}

// The timepicker returns time in UTC, but the date picker returns date in LocalTimeZone
// hence use this method to get both date & time in UTC
fun getTrueDate(date: LocalDate, time: LocalTime, convert: Boolean = true): LocalDateTime {
val timeLocal = if (convert) time.convertUTCToLocal() else time

return timeNowUTC()
.withYear(date.year)
.withMonth(date.monthValue)
.withDayOfMonth(date.dayOfMonth)
.withHour(timeLocal.hour)
.withMinute(timeLocal.minute)
.withSecond(0)
.withNano(0)
.convertLocalToUTC()
}


fun LocalDate.formatLocal(
pattern: String = "dd MMM yyyy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding
import com.ivy.wallet.R
import com.ivy.wallet.base.convertUTCtoLocal
import com.ivy.wallet.base.getTrueDate
import com.ivy.wallet.base.onScreenStart
import com.ivy.wallet.base.timeNowLocal
import com.ivy.wallet.logic.model.CreateAccountData
Expand Down Expand Up @@ -242,10 +244,10 @@ private fun BoxWithConstraintsScope.UI(
dueDateTime = dueDate,
) {
ivyContext.datePicker(
initialDate = dateTime?.toLocalDate(),
initialDate = dateTime?.convertUTCtoLocal()?.toLocalDate(),
) { date ->
ivyContext.timePicker { time ->
onSetDateTime(date.atTime(time.hour, time.minute, time.second))
onSetDateTime(getTrueDate(date, time))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ private fun DateTimeRow(
iconStart = R.drawable.ic_date
) {
ivyContext.datePicker(
initialDate = dateTime.toLocalDate()
initialDate = dateTime.convertUTCtoLocal().toLocalDate()
) {
onSetDateTime(it.atTime(dateTime.toLocalTime()))
onSetDateTime(getTrueDate(it, dateTime.toLocalTime()))
}
}

Expand All @@ -192,7 +192,7 @@ private fun DateTimeRow(
iconStart = R.drawable.ic_date
) {
ivyContext.timePicker {
onSetDateTime(dateTime.toLocalDate().atTime(it))
onSetDateTime(getTrueDate(dateTime.convertUTCtoLocal().toLocalDate(), it))
}
}

Expand Down

0 comments on commit 0edbfbc

Please sign in to comment.