diff --git a/DESCRIPTION b/DESCRIPTION index 82a15505..64ecf66b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: clock Title: Date-Time Types and Tools -Version: 0.1.0.9000 +Version: 0.1.0.9001 Authors@R: c(person(given = "Davis", family = "Vaughan", diff --git a/NEWS.md b/NEWS.md index 115c6505..02b499be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -46,6 +46,8 @@ * Linking against cpp11 0.2.7 is now required to fix a rare memory leak issue. +* Exposed an extremely experimental and limited C++ API for vroom (#322). + # clock 0.1.0 * Added a `NEWS.md` file to track changes to the package. diff --git a/inst/include/clock/clock.h b/inst/include/clock/clock.h new file mode 100644 index 00000000..7efd94f3 --- /dev/null +++ b/inst/include/clock/clock.h @@ -0,0 +1,35 @@ +#ifndef CLOCK_CLOCK_H +#define CLOCK_CLOCK_H + +#include + +#include +#include + +#include +#include + +namespace rclock { + +static +inline +const date::time_zone* +locate_zone(const std::string& zone_name) { + typedef const date::time_zone* fn_t(const std::string&); + static fn_t *fn = (fn_t*) R_GetCCallable("clock", "clock_locate_zone"); + return fn(zone_name); +} + +static +inline +date::local_info +get_local_info(const date::local_seconds& lt, + const date::time_zone* p_time_zone) { + typedef date::local_info fn_t(const date::local_seconds&, const date::time_zone*); + static fn_t *fn = (fn_t*) R_GetCCallable("clock", "clock_get_local_info"); + return fn(lt, p_time_zone); +} + +} // namespace rclock + +#endif diff --git a/src/cpp11.cpp b/src/cpp11.cpp index 5c6b9dd3..4bccc2c9 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -1139,8 +1139,11 @@ static const R_CallMethodDef CallEntries[] = { }; } +void export_clock_callables(DllInfo* dll); + extern "C" void R_init_clock(DllInfo* dll){ R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); R_useDynamicSymbols(dll, FALSE); + export_clock_callables(dll); R_forceSymbols(dll, TRUE); } diff --git a/src/exported.cpp b/src/exported.cpp new file mode 100644 index 00000000..2ab595c4 --- /dev/null +++ b/src/exported.cpp @@ -0,0 +1,39 @@ +#include "clock.h" +#include // For DllInfo on R 3.3 + +// ----------------------------------------------------------------------------- + +/* + * Look up a time zone by name + * + * Returns a `time_zone` pointer for use in `clock_get_local_info()`. + * + * `""` should not be passed through here. If you need to pass through the + * system time zone, materialize its value with `Sys.timezone()` first. + * + * Throws a `std::runtime_error()` with an informative message if the + * `zone_name` does not exist in the database. + */ +const date::time_zone* +clock_locate_zone(const std::string& zone_name) { + return date::locate_zone(zone_name); +} + +// ----------------------------------------------------------------------------- + +/* + * Pair a local time with a time zone to compute all local time information + */ +date::local_info +clock_get_local_info(const date::local_seconds& lt, + const date::time_zone* p_time_zone) { + return p_time_zone->get_info(lt); +} + +// ----------------------------------------------------------------------------- + +[[cpp11::init]] +void export_clock_callables(DllInfo* dll){ + R_RegisterCCallable("clock", "clock_locate_zone", (DL_FUNC)clock_locate_zone); + R_RegisterCCallable("clock", "clock_get_local_info", (DL_FUNC)clock_get_local_info); +}