niXman / binapi

Binance API C++ implementation
Apache License 2.0
253 stars 85 forks source link

Timestamp issue #9

Closed vasquezito closed 3 years ago

vasquezito commented 3 years ago

Thanks for this library niXman! I'm having issues with receiving (I think!) the correct timestamp. The date is in the future and it doesn't contain the last 3 digits for the ms, it almost looks like it's missing data. Here is my output

c:\Users\vasquez\source\repos\ConsoleApplication1\Debug>ConsoleApplication1.exe server_time=3841850106

local_time=1610159618498

price: {"symbol":"BTCUSDT","price":"40066"}

Any ideas?

niXman commented 3 years ago

hi,

please provide me the complete example because I can't confirm this using my code...

vasquezito commented 3 years ago

Here is the code. Not much going on, just working from your example. I'm building it in Visual Studio 2019 Community Edition as an x86 console application, MSVC 14.28.29333 and compiler version below

c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x86>cl Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29335 for x86 Copyright (C) Microsoft Corporation. All rights reserved.

Code

include <binapi/api.hpp>

include <boost/asio/io_context.hpp>

include

include

include

/*****/ / fault breaks /

define PRINT_IF_ERROR(res) \

if ( !static_cast<bool>(res) ) { \
    std::cout << __FILE__ << "(" << __LINE__ << "): msg=" << res.errmsg << std::endl; \
}

/*****/ / fault breaks /

define BREAK_IF_ERROR(res) \

PRINT_IF_ERROR(res); \
if ( !res ) { \
    return EXIT_FAILURE; \
}

/*****/ / local epoch time /

std::uint64_t get_current_ms_epoch() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count()); }

int main() {

auto t1 = std::chrono::high_resolution_clock::now();

boost::asio::io_context ioctx;
binapi::rest::api api{
     ioctx
    ,"api1.binance.com"
    ,"443"
    ,"" // can be empty for non USER_DATA reqs
    ,"" // can be empty for non USER_DATA reqs
    ,10000 // recvWindow
};

auto server_time_char = api.server_time();
auto server_time = server_time_char.v.serverTime - '0';

auto local_time = get_current_ms_epoch();

BREAK_IF_ERROR(server_time_char);

std::cout << "server_time test=" << server_time_char.v << std::endl << std::endl;
std::cout << "local_time=" << (local_time) << std::endl << std::endl;

auto res = api.price("BTCUSDT");
if (!res) {
    std::cerr << "get price error: " << res.errmsg << std::endl;

    return EXIT_FAILURE;
}

std::cout << "price: " << res.v << std::endl << std::endl;

auto t2 = std::chrono::high_resolution_clock::now();

auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

std::cout << "parse time: " << duration << " ms" << std::endl;

return EXIT_SUCCESS;

}

niXman commented 3 years ago

please remove all unnecessary code that is not related to the essence of the problem.

vasquezito commented 3 years ago

After some debugging I've found the problem originating in types.hpp, specifically in the struct server_time_t where I replaced std::size_t serverTime; with std::int64_t serverTime;. Seems to be working correct now and I receive the correct format of the timestamp.

niXman commented 3 years ago

that is, you made changes to the code that you did not tell me about, and I had to waste my time on the problem you created?!

fck!