Skip to content

Commit 652e51d

Browse files
authored
Implement limited callable API for vroom (#213)
* Implement limited callable API for vroom * Remove `clock_build()` in favor of using `date.h` tools directly * Remove indirection around `date::time_zone*` It seems like the forward declaration of `class time_zone` in `date/tz.h` is enough to allow us to use it as a return value and an argument * Bump development version for vroom * Allow runtime errors to be thrown from `clock_locate_zone()` vroom will possibly call this in parallel, and will catch any runtime errors. However, you cannot throw a cpp11 error, or allocate any R memory, while in a thread. * Document exported helpers * Export `clock_get_local_info()` for maximum flexibility This way the client can choose exactly how to handle nonexistent and ambiguous times, and we don't have to return a custom struct for nonexistent times * NEWS
1 parent 700ba68 commit 652e51d

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: clock
22
Title: Date-Time Types and Tools
3-
Version: 0.1.0.9000
3+
Version: 0.1.0.9001
44
Authors@R:
55
c(person(given = "Davis",
66
family = "Vaughan",

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
* Linking against cpp11 0.2.7 is now required to fix a rare memory leak issue.
4848

49+
* Exposed an extremely experimental and limited C++ API for vroom (#322).
50+
4951
# clock 0.1.0
5052

5153
* Added a `NEWS.md` file to track changes to the package.

inst/include/clock/clock.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef CLOCK_CLOCK_H
2+
#define CLOCK_CLOCK_H
3+
4+
#include <R_ext/Rdynload.h>
5+
6+
#include <chrono>
7+
#include <string>
8+
9+
#include <date/date.h>
10+
#include <date/tz.h>
11+
12+
namespace rclock {
13+
14+
static
15+
inline
16+
const date::time_zone*
17+
locate_zone(const std::string& zone_name) {
18+
typedef const date::time_zone* fn_t(const std::string&);
19+
static fn_t *fn = (fn_t*) R_GetCCallable("clock", "clock_locate_zone");
20+
return fn(zone_name);
21+
}
22+
23+
static
24+
inline
25+
date::local_info
26+
get_local_info(const date::local_seconds& lt,
27+
const date::time_zone* p_time_zone) {
28+
typedef date::local_info fn_t(const date::local_seconds&, const date::time_zone*);
29+
static fn_t *fn = (fn_t*) R_GetCCallable("clock", "clock_get_local_info");
30+
return fn(lt, p_time_zone);
31+
}
32+
33+
} // namespace rclock
34+
35+
#endif

src/cpp11.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,8 +1139,11 @@ static const R_CallMethodDef CallEntries[] = {
11391139
};
11401140
}
11411141

1142+
void export_clock_callables(DllInfo* dll);
1143+
11421144
extern "C" void R_init_clock(DllInfo* dll){
11431145
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
11441146
R_useDynamicSymbols(dll, FALSE);
1147+
export_clock_callables(dll);
11451148
R_forceSymbols(dll, TRUE);
11461149
}

src/exported.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "clock.h"
2+
#include <R_ext/Rdynload.h> // For DllInfo on R 3.3
3+
4+
// -----------------------------------------------------------------------------
5+
6+
/*
7+
* Look up a time zone by name
8+
*
9+
* Returns a `time_zone` pointer for use in `clock_get_local_info()`.
10+
*
11+
* `""` should not be passed through here. If you need to pass through the
12+
* system time zone, materialize its value with `Sys.timezone()` first.
13+
*
14+
* Throws a `std::runtime_error()` with an informative message if the
15+
* `zone_name` does not exist in the database.
16+
*/
17+
const date::time_zone*
18+
clock_locate_zone(const std::string& zone_name) {
19+
return date::locate_zone(zone_name);
20+
}
21+
22+
// -----------------------------------------------------------------------------
23+
24+
/*
25+
* Pair a local time with a time zone to compute all local time information
26+
*/
27+
date::local_info
28+
clock_get_local_info(const date::local_seconds& lt,
29+
const date::time_zone* p_time_zone) {
30+
return p_time_zone->get_info(lt);
31+
}
32+
33+
// -----------------------------------------------------------------------------
34+
35+
[[cpp11::init]]
36+
void export_clock_callables(DllInfo* dll){
37+
R_RegisterCCallable("clock", "clock_locate_zone", (DL_FUNC)clock_locate_zone);
38+
R_RegisterCCallable("clock", "clock_get_local_info", (DL_FUNC)clock_get_local_info);
39+
}

0 commit comments

Comments
 (0)