Skip to content

Commit

Permalink
fix: Better date parsing.
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Sabadello <markus@danubetech.com>
  • Loading branch information
peacekeeper committed May 13, 2020
1 parent b5ce142 commit f12cd20
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.TimeZone;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
Expand All @@ -31,6 +32,18 @@ public class BlockcypherAPIBitcoinConnection extends AbstractBitcoinConnection i

private static final BlockcypherAPIBitcoinConnection instance = new BlockcypherAPIBitcoinConnection();

public static final SimpleDateFormat DATE_FORMAT;
public static final SimpleDateFormat DATE_FORMAT_MILLIS;

static {

DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));

DATE_FORMAT_MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
DATE_FORMAT_MILLIS.setTimeZone(TimeZone.getTimeZone("UTC"));
}

public BlockcypherAPIBitcoinConnection() {

}
Expand Down Expand Up @@ -74,8 +87,6 @@ public ChainAndLocationData lookupChainAndLocationData(ChainAndTxid chainAndTxid
return new ChainAndLocationData(chainAndTxid.getChain(), blockHeight, transactionPosition, chainAndTxid.getTxoIndex());
}

private static final SimpleDateFormat RFC_339_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

@Override
public DidBtcrData getDidBtcrData(ChainAndTxid chainAndTxid) throws IOException {

Expand Down Expand Up @@ -193,10 +204,16 @@ public DidBtcrData getDidBtcrData(ChainAndTxid chainAndTxid) throws IOException

try {

transactionTime = RFC_339_DATE_FORMAT.parse(received.getAsString()).getTime() / 1000L;
transactionTime = DATE_FORMAT.parse(received.getAsString()).getTime() / 1000L;
} catch (ParseException ex) {

throw new IOException("Cannot parse receive date '" + received.getAsString() + "': " + ex.getMessage(), ex);
try {

transactionTime = DATE_FORMAT_MILLIS.parse(received.getAsString()).getTime() / 1000L;
} catch (ParseException ex2) {

throw new IOException("Cannot parse receive date '" + received.getAsString() + "': " + ex2.getMessage(), ex2);
}
}

// done
Expand Down

0 comments on commit f12cd20

Please sign in to comment.