-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Future Timestamp Error #1
Comments
Better Solution to the Bug: Using the Network Time Protocol (NTP) The best approach to solve this bug would be to request the "Network Time Protocol" (NTP). However, making an NTP request is a bit challenging. Instead, I found the Play Services Time API by Google, which provides an easier way to get accurate time. Thanks to TrustedTime, we can obtain the real time from Google Services even "without internet" (though not always, I suppose). This solution seems much more convenient than creating an NTP request or querying an external API. Here's the updated version of my code to fix the issue: class MainActivity
//...
protected void onCreate(Bundle savedInstanceState) {
//...code...
Task<TrustedTimeClient> initializeTrustedTimeClientTask =
TrustedTime.createClient(this); <<<< This fixed all
SharedPreferences sp = SharedPreferencesKeys.getSharedPreferences(this);
retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
initializeTrustedTimeClientTask.addOnCompleteListener(task -> {
var dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC+2"));
if(task.isSuccessful()){
isTrueTime = true;
Long tms = task.getResult().computeCurrentUnixEpochMillis();
trueTimeNow = dateFormat.format(new Date(tms));//1730001052195L
//...code...
}else {
//true - the mistake was already there
if(!sp.getBoolean(SharedPreferencesKeys.ConstKeys.TRUE_TIME_FAIL, false)){
//show warning
}
isTrueTime = false;
trueTimeNow = dateFormat.format(new Date(System.currentTimeMillis()));
}
CurrencyRoot data = loadCurrenciesFromLocalStorage();
UserTempData user = loadUserDataFromLocalStorage();
if(data != null && (user == null ||
user.getLastFirstCurrency() == null ||
user.getLastSecondCurrency() == null)){
user = new UserTempData(data.getExchangeRate().get(0), data.getExchangeRate().get(1));
}
if(data == null){//first start
loadDataFromAPI();
}else if(!data.getDate().equals(trueTimeNow)){//just old data
this.userTempData = user;
this.currencyRoot = data;
isOldData = true;
loadDataFromAPI();
}else {//data is exists and new ones
openMainFragment(data, user);
}
});
}
//... |
Currently, this bug has only been fixed in the Play Store version of the app. |
Error:
Current real Date & Time: 25.12.2024. However, the user can change the Date & Time on their device, for example, to 26.12.2025.
Request to API: https://api.privatbank.ua/p24api/exchange_rates?date=26.12.2025
The text was updated successfully, but these errors were encountered: