Skip to content

Commit 29da131

Browse files
authored
Add support for different time formats in the receipts API (#252)
1 parent 8b1e2e4 commit 29da131

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
77
### Removed
88
### Fixed
99

10+
## [0.80.16]
11+
### Added
12+
* core: Add support for different time formats in the receipts API
13+
1014
## [0.80.15]
1115
### Changed
1216
* ui: Migrate custom GooglePay button to Google's PayButton

core/src/main/java/io/snabble/sdk/ReceiptsApi.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.text.ParseException;
66
import java.text.SimpleDateFormat;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.Collections;
910
import java.util.Date;
1011
import java.util.HashMap;
@@ -61,11 +62,18 @@ public interface ReceiptUpdateCallback {
6162
void failure();
6263
}
6364

64-
private final SimpleDateFormat simpleDateFormat;
65+
private final List<SimpleDateFormat> formats = Arrays.asList(
66+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US),
67+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US),
68+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US)
69+
);
6570

6671
public ReceiptsApi() {
67-
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
68-
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
72+
TimeZone utc = TimeZone.getTimeZone("UTC");
73+
for (SimpleDateFormat format : formats) {
74+
format.setTimeZone(utc);
75+
format.setLenient(false);
76+
}
6977
}
7078

7179
/**
@@ -147,7 +155,7 @@ public void success(ApiReceipt apiReceipt) {
147155
ReceiptInfo receiptInfo = new ReceiptInfo(
148156
apiOrder.id,
149157
apiOrder.project,
150-
simpleDateFormat.parse(apiOrder.date).getTime(),
158+
parseDate(apiOrder.date).getTime(),
151159
url != null ? snabble.absoluteUrl(url) : null,
152160
apiOrder.shopName,
153161
priceFormatter.format(apiOrder.price),
@@ -178,4 +186,17 @@ public void error(Throwable t) {
178186
receiptUpdateCallback.failure();
179187
}
180188
}
189+
190+
private Date parseDate(String dateStr) throws ParseException {
191+
ParseException lastException = null;
192+
for (SimpleDateFormat format : formats) {
193+
try {
194+
return format.parse(dateStr);
195+
} catch (ParseException e) {
196+
lastException = e;
197+
}
198+
}
199+
assert lastException != null;
200+
throw lastException;
201+
}
181202
}

0 commit comments

Comments
 (0)