diff --git a/app/src/main/java/com/ivy/wallet/base/DateExt.kt b/app/src/main/java/com/ivy/wallet/base/DateExt.kt index 63905fd93a..a4e7d511cb 100644 --- a/app/src/main/java/com/ivy/wallet/base/DateExt.kt +++ b/app/src/main/java/com/ivy/wallet/base/DateExt.kt @@ -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", diff --git a/app/src/main/java/com/ivy/wallet/ui/edit/EditTransactionScreen.kt b/app/src/main/java/com/ivy/wallet/ui/edit/EditTransactionScreen.kt index 0c8b367050..ec760c87ec 100644 --- a/app/src/main/java/com/ivy/wallet/ui/edit/EditTransactionScreen.kt +++ b/app/src/main/java/com/ivy/wallet/ui/edit/EditTransactionScreen.kt @@ -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 @@ -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)) } } } diff --git a/app/src/main/java/com/ivy/wallet/ui/theme/modal/LoanRecordModal.kt b/app/src/main/java/com/ivy/wallet/ui/theme/modal/LoanRecordModal.kt index 1d77bc2c3a..f63a333c69 100644 --- a/app/src/main/java/com/ivy/wallet/ui/theme/modal/LoanRecordModal.kt +++ b/app/src/main/java/com/ivy/wallet/ui/theme/modal/LoanRecordModal.kt @@ -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())) } } @@ -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)) } }