Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Feature/Optional time supplier#130

Open
TuxedoFish wants to merge 1 commit intobinance:masterbinance/binance-connector-java:masterfrom
TuxedoFish:feature/time-supplierTuxedoFish/binance-connector-java:feature/time-supplierCopy head branch name to clipboard
Open

Feature/Optional time supplier#130
TuxedoFish wants to merge 1 commit intobinance:masterbinance/binance-connector-java:masterfrom
TuxedoFish:feature/time-supplierTuxedoFish/binance-connector-java:feature/time-supplierCopy head branch name to clipboard

Conversation

@TuxedoFish
Copy link
Copy Markdown

Background

When developing locally my clock gets out of sync pretty fast and it was getting unwieldly to develop. I forked and used this locally to get around my problem. I thought it might be of use to other people as well.

local_development_clocks

Solution

Add a TimeSupplier class which can be passed a custom long supplier which can be used to adjust the time.

Potential usage

public class BinanceRestAdapter implements VenueAdapter {
    private final SpotClient client;

    public BinanceRestAdapter(String baseUrl, String apiKey, String privateKey, ObjectMapper objectMapper, List<String> tradingCoins) {
        client = new SpotClientImpl(apiKey, privateKey, baseUrl);
        client.setTimeSupplier(SysTime.INSTANCE::currentTimeMillis);
        syncTime();
    }

    ...

    private void syncTime() {
        String timeResult = client.createMarket().time();
        Long serverTimerTime = Long.valueOf(timeResult.substring(14, timeResult.length() - 1));
        Long clientTime = System.currentTimeMillis();
        long delta = clientTime - serverTimerTime;
        SysTime.INSTANCE.setDelta(-delta);
        Long updatedTime = SysTime.INSTANCE.currentTimeMillis();
        LOG.info(String.format("Updating @ local time %s | external time %s (%s ms) | new time delta = %s", clientTime, serverTimerTime, delta, updatedTime - serverTimerTime));
    }
}

With a class something like this

public class SysTime {
    public static SysTime INSTANCE = new SysTime();
    private long delta;

    public long currentTimeMillis() {
        return System.currentTimeMillis() + delta;
    }

    public void setDelta(long delta) {
        this.delta = delta;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.