Skip to content
Snippets Groups Projects
Commit 721f1092 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

Fixed #670 - fetch transactions from database without timezone (use UTC)

parent 9133e1bb
No related branches found
No related tags found
No related merge requests found
......@@ -20,7 +20,7 @@ public class ChartSettings
public static ChartSettings getDefault(FilterConfiguration filterConfiguration)
{
return new ChartSettings(ChartDisplayType.BAR, ChartGroupType.MONTH, null, DateHelper.getCurrentDate().withDayOfMonth(1), DateHelper.getCurrentDate().dayOfMonth().withMaximumValue(), filterConfiguration);
return new ChartSettings(ChartDisplayType.BAR, ChartGroupType.MONTH, null, DateHelper.getCurrentDateWithUTC().withDayOfMonth(1), DateHelper.getCurrentDateWithUTC().dayOfMonth().withMaximumValue(), filterConfiguration);
}
public ChartSettings()
......
......@@ -25,6 +25,7 @@ import de.deadlocker8.budgetmaster.utils.DateHelper;
import de.deadlocker8.budgetmaster.utils.LanguageType;
import de.thecodelabs.utils.util.ColorUtilsNonJavaFX;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -32,8 +33,10 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;
@Service
public class HelpersService
{
......@@ -175,7 +178,8 @@ public class HelpersService
public int getAccountBudget()
{
Account currentAccount = getCurrentAccount();
List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(currentAccount, DateHelper.getCurrentDate(), FilterConfiguration.DEFAULT);
final DateTime endDate = DateHelper.getCurrentDateWithUTC();
List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(currentAccount, endDate, FilterConfiguration.DEFAULT);
int sum = 0;
for(Transaction transaction : transactions)
......
......@@ -19,7 +19,6 @@ import de.deadlocker8.budgetmaster.utils.DateHelper;
import de.deadlocker8.budgetmaster.utils.Strings;
import de.thecodelabs.utils.util.Localization;
import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -79,13 +78,13 @@ public class TransactionService implements Resettable
private List<Transaction> getTransactionsForMonthAndYearWithRest(Account account, int month, int year, FilterConfiguration filterConfiguration)
{
DateTime startDate = DateHelper.getCurrentDate().withYear(year).withMonthOfYear(month).minusMonths(1).dayOfMonth().withMaximumValue();
DateTime startDate = DateHelper.getCurrentDateWithUTC().withYear(year).withMonthOfYear(month).minusMonths(1).dayOfMonth().withMaximumValue();
List<Transaction> transactions = getTransactionsForMonthAndYearWithoutRest(account, month, year, filterConfiguration);
Transaction transactionRest = new Transaction();
transactionRest.setCategory(categoryService.findByType(CategoryType.REST));
transactionRest.setName(Localization.getString(Strings.CATEGORY_REST));
transactionRest.setDate(DateHelper.getCurrentDate().withYear(year).withMonthOfYear(month).withDayOfMonth(1));
transactionRest.setDate(DateHelper.getCurrentDateWithUTC().withYear(year).withMonthOfYear(month).withDayOfMonth(1));
transactionRest.setAmount(getRest(account, startDate));
transactionRest.setTags(new ArrayList<>());
transactions.add(transactionRest);
......@@ -95,14 +94,23 @@ public class TransactionService implements Resettable
private List<Transaction> getTransactionsForMonthAndYearWithoutRest(Account account, int month, int year, FilterConfiguration filterConfiguration)
{
DateTime startDate = DateHelper.getCurrentDate().withYear(year).withMonthOfYear(month).minusMonths(1).dayOfMonth().withMaximumValue();
DateTime endDate = DateHelper.getCurrentDate().withYear(year).withMonthOfYear(month).dayOfMonth().withMaximumValue();
// dayOfMonth = 10 --> arbitrary day not too close to the edges (0 or max day of month)
final DateTime referenceTime = new DateTime(year, month, 10, 0, 0, 0, 0);
final DateTime startDate = referenceTime
.dayOfMonth()
.withMinimumValue();
final DateTime endDate = referenceTime
.dayOfMonth()
.withMaximumValue();
return getTransactionsForAccount(account, startDate, endDate, filterConfiguration);
}
public List<Transaction> getTransactionsForAccountUntilDate(Account account, DateTime date, FilterConfiguration filterConfiguration)
{
DateTime startDate = DateHelper.getCurrentDate().withYear(1900).withMonthOfYear(1).withDayOfMonth(1);
DateTime startDate = DateHelper.getCurrentDateWithUTC().withYear(1900).withMonthOfYear(1).withDayOfMonth(1);
return getTransactionsForAccount(account, startDate, date, filterConfiguration);
}
......@@ -125,7 +133,7 @@ public class TransactionService implements Resettable
private int getRest(Account account, DateTime endDate)
{
DateTime startDate = DateHelper.getCurrentDate().withYear(2000).withMonthOfYear(1).withDayOfMonth(1);
DateTime startDate = DateHelper.getCurrentDateWithUTC().withYear(2000).withMonthOfYear(1).withDayOfMonth(1);
Integer restForNormalAndRepeating = transactionRepository.getRestForNormalAndRepeating(account.getID(), startDate, endDate);
Integer restForTransferSource = transactionRepository.getRestForTransferSource(account.getID(), startDate, endDate);
Integer restForTransferDestination = transactionRepository.getRestForTransferDestination(account.getID(), startDate, endDate);
......
......@@ -15,4 +15,9 @@ public class DateHelper
{
return DateTime.now(DateTimeZone.forTimeZone(TimeZone.getDefault()));
}
public static DateTime getCurrentDateWithUTC()
{
return getCurrentDate().toDateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));
}
}
......@@ -9,6 +9,7 @@ import de.deadlocker8.budgetmaster.transactions.Transaction;
import de.deadlocker8.budgetmaster.transactions.TransactionService;
import de.deadlocker8.budgetmaster.utils.DateHelper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.format.DateTimeFormat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -86,7 +87,7 @@ class TransactionServiceDatabaseTest
FilterConfiguration filterConfiguration = new FilterConfiguration(true, true, true, true, true, null, null, "");
List<Transaction> transactions = transactionService.getTransactionsForAccount(accountService.getRepository().findAllByType(AccountType.ALL).get(0), date1, DateHelper.getCurrentDate(), filterConfiguration);
assertThat(transactions).hasSize(7);
assertThat(transactions).hasSize(8);
}
@Test
......@@ -105,7 +106,31 @@ class TransactionServiceDatabaseTest
{
FilterConfiguration filterConfiguration = new FilterConfiguration(true, true, true, true, true, null, null, "");
List<Transaction> transactions = transactionService.getTransactionsForMonthAndYear(accountService.getRepository().findByName("Default Account"), 6, 2020, false, filterConfiguration);
List<Transaction> transactions = transactionService.getTransactionsForMonthAndYear(accountService.getRepository().findByName("Default Account"), 6, 2021, false, filterConfiguration);
assertThat(transactions).hasSize(1);
assertThat(transactions.get(0).getDate())
.isEqualTo(new DateTime(2021, 6, 30, 0, 0, 0, 0));
}
@Test
void test_getTransactionsForMonthAndYear_CloseToMidnight()
{
// override system time to setup midnight scenario
// DateTime.now() will return the time in UTC --> shortly before midnight
DateTimeUtils.setCurrentMillisFixed(new DateTime(2021, 2, 5, 21, 45, 0).getMillis());
try
{
FilterConfiguration filterConfiguration = new FilterConfiguration(true, true, true, true, true, null, null, "");
List<Transaction> transactions = transactionService.getTransactionsForMonthAndYear(accountService.getRepository().findByName("Default Account"), 6, 2021, false, filterConfiguration);
assertThat(transactions).hasSize(1);
assertThat(transactions.get(0).getDate())
.isEqualTo(new DateTime(2021, 6, 30, 0, 0, 0, 0));
}
finally
{
DateTimeUtils.setCurrentMillisSystem();
}
}
}
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment