diff --git a/CHANGELOG.md b/CHANGELOG.md index 7026792c4..e202f39e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. ### Removed ### Fixed +## [0.80.16] +### Added +* core: Add support for different time formats in the receipts API + ## [0.80.15] ### Changed * ui: Migrate custom GooglePay button to Google's PayButton diff --git a/core/src/main/java/io/snabble/sdk/ReceiptsApi.java b/core/src/main/java/io/snabble/sdk/ReceiptsApi.java index 41a81f282..08610b69b 100644 --- a/core/src/main/java/io/snabble/sdk/ReceiptsApi.java +++ b/core/src/main/java/io/snabble/sdk/ReceiptsApi.java @@ -5,6 +5,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -61,11 +62,18 @@ public interface ReceiptUpdateCallback { void failure(); } - private final SimpleDateFormat simpleDateFormat; + private final List formats = Arrays.asList( + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US) + ); public ReceiptsApi() { - simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); - simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + TimeZone utc = TimeZone.getTimeZone("UTC"); + for (SimpleDateFormat format : formats) { + format.setTimeZone(utc); + format.setLenient(false); + } } /** @@ -147,7 +155,7 @@ public void success(ApiReceipt apiReceipt) { ReceiptInfo receiptInfo = new ReceiptInfo( apiOrder.id, apiOrder.project, - simpleDateFormat.parse(apiOrder.date).getTime(), + parseDate(apiOrder.date).getTime(), url != null ? snabble.absoluteUrl(url) : null, apiOrder.shopName, priceFormatter.format(apiOrder.price), @@ -178,4 +186,17 @@ public void error(Throwable t) { receiptUpdateCallback.failure(); } } + + private Date parseDate(String dateStr) throws ParseException { + ParseException lastException = null; + for (SimpleDateFormat format : formats) { + try { + return format.parse(dateStr); + } catch (ParseException e) { + lastException = e; + } + } + assert lastException != null; + throw lastException; + } }