From ee1d700a9bc764c86502978b5538df44aaa66905 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 20 Apr 2020 16:37:24 +0300 Subject: [core] Add platform abstractions for utils::now() Some platforms might require special permissions or custom APIs to access the current time of the day. --- include/mbgl/platform/time.hpp | 13 +++++++++++++ include/mbgl/util/chrono.hpp | 4 +++- platform/android/android.cmake | 1 + platform/default/src/mbgl/platform/time.cpp | 11 +++++++++++ platform/ios/ios.cmake | 1 + platform/linux/linux.cmake | 1 + platform/macos/macos.cmake | 1 + platform/qt/qt.cmake | 1 + src/mbgl/util/http_header.cpp | 3 ++- 9 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 include/mbgl/platform/time.hpp create mode 100644 platform/default/src/mbgl/platform/time.cpp diff --git a/include/mbgl/platform/time.hpp b/include/mbgl/platform/time.hpp new file mode 100644 index 0000000000..8769524514 --- /dev/null +++ b/include/mbgl/platform/time.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace mbgl { +namespace platform { + +// Returns the current time. Abstracted because some platforms +// will not allow direct access to the current time via syscall. +std::chrono::time_point now(); + +} // namespace platform +} // namespace mbgl diff --git a/include/mbgl/util/chrono.hpp b/include/mbgl/util/chrono.hpp index 4de3c2d3a2..d736b4f826 100644 --- a/include/mbgl/util/chrono.hpp +++ b/include/mbgl/util/chrono.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -19,7 +21,7 @@ using Timestamp = std::chrono::time_point; namespace util { inline Timestamp now() { - return std::chrono::time_point_cast(std::chrono::system_clock::now()); + return platform::now(); } // Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT" diff --git a/platform/android/android.cmake b/platform/android/android.cmake index fd6d9e7348..6547496bce 100644 --- a/platform/android/android.cmake +++ b/platform/android/android.cmake @@ -50,6 +50,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/gfx/headless_frontend.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/gl/headless_backend.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/map/map_snapshotter.cpp + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/platform/time.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/asset_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/database_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_manager.cpp diff --git a/platform/default/src/mbgl/platform/time.cpp b/platform/default/src/mbgl/platform/time.cpp new file mode 100644 index 0000000000..647464bb91 --- /dev/null +++ b/platform/default/src/mbgl/platform/time.cpp @@ -0,0 +1,11 @@ +#include + +namespace mbgl { +namespace platform { + +std::chrono::time_point now() { + return std::chrono::time_point_cast(std::chrono::system_clock::now()); +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/ios/ios.cmake b/platform/ios/ios.cmake index 984b15840f..cab63e2824 100644 --- a/platform/ios/ios.cmake +++ b/platform/ios/ios.cmake @@ -49,6 +49,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/gfx/headless_frontend.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/layermanager/layer_manager.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/map/map_snapshotter.cpp + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/platform/time.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/asset_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/database_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_manager.cpp diff --git a/platform/linux/linux.cmake b/platform/linux/linux.cmake index 7412b09f8a..47395df7c6 100644 --- a/platform/linux/linux.cmake +++ b/platform/linux/linux.cmake @@ -18,6 +18,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/i18n/collator.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/i18n/number_format.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/layermanager/layer_manager.cpp + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/platform/time.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/asset_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/database_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_manager.cpp diff --git a/platform/macos/macos.cmake b/platform/macos/macos.cmake index a0eb3eae55..38c6e41a01 100644 --- a/platform/macos/macos.cmake +++ b/platform/macos/macos.cmake @@ -42,6 +42,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/gfx/headless_frontend.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/layermanager/layer_manager.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/map/map_snapshotter.cpp + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/platform/time.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/asset_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/database_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_manager.cpp diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index a7d209a077..fdb26e5308 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -31,6 +31,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/gl/headless_backend.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/i18n/collator.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/layermanager/layer_manager.cpp + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/platform/time.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/asset_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/database_file_source.cpp ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_manager.cpp diff --git a/src/mbgl/util/http_header.cpp b/src/mbgl/util/http_header.cpp index cdb4759d51..9777a2c806 100644 --- a/src/mbgl/util/http_header.cpp +++ b/src/mbgl/util/http_header.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -31,7 +32,7 @@ optional parseRetryHeaders(const optional& retryAfter, if (retryAfter) { try { auto secs = std::chrono::seconds(std::stoi(*retryAfter)); - return std::chrono::time_point_cast(std::chrono::system_clock::now() + secs); + return std::chrono::time_point_cast(util::now() + secs); } catch (...) { return util::parseTimestamp((*retryAfter).c_str()); } -- cgit v1.2.1