TimeProvider.cpp 769 B

123456789101112131415161718192021222324
  1. #include "TimeProvider.h"
  2. #include "BellLogger.h" // for AbstractLogger
  3. #include "Logger.h" // for CSPOT_LOG
  4. #include "Utils.h" // for extract, getCurrentTimestamp
  5. #ifndef _WIN32
  6. #include <arpa/inet.h>
  7. #endif
  8. using namespace cspot;
  9. TimeProvider::TimeProvider() {}
  10. void TimeProvider::syncWithPingPacket(const std::vector<uint8_t>& pongPacket) {
  11. CSPOT_LOG(debug, "Time synced with spotify servers");
  12. // Spotify's timestamp is in seconds since unix time - convert to millis.
  13. uint64_t remoteTimestamp =
  14. ((uint64_t)ntohl(extract<uint32_t>(pongPacket, 0))) * 1000;
  15. this->timestampDiff = remoteTimestamp - getCurrentTimestamp();
  16. }
  17. unsigned long long TimeProvider::getSyncedTimestamp() {
  18. return getCurrentTimestamp() + this->timestampDiff;
  19. }