Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C level function that returns the offset at a given time-point in a given timezone #32

Merged
merged 3 commits into from Sep 7, 2019

Conversation

@lsilvest
Copy link
Contributor

@lsilvest lsilvest commented Sep 7, 2019

For nanotime's period type, we need access to the timezone data so that adding and subtracting a period is correctly handled. The operation look like this:

    nt <- nanotime("2018-05-01T00:00:00.000000000-04:00")
    p  <- as.period("4m")
    tz <- "America/New_York"
    expected <- nanotime("2018-01-01T00:00:00.000000000-05:00")
    checkIdentical(minus(nt, p, tz), expected)

In order to do this, we need a function to get the offset for a particular timezone at a given time-point, the function getOffset.

This function is then registered as a C function using R_RegisterCCallable so that it can be called at C level by another package.

This is an ad-hoc change for nanotime, but it could make sense to add the set of functions exported in R (or one could wait until there's a specific request of course!).

…offset at a given time-point in a given timezone
Copy link
Owner

@eddelbuettel eddelbuettel left a comment

Nice PR. Please also add ChangeLog and NEWS as usual. Maybe even a test :)

@@ -163,6 +163,10 @@ BEGIN_RCPP
END_RCPP
}


// export this function so it can be called at the C level by other packages:
int _RcppCCTZ_getOffset(long long s, const char* tzstr);

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

int64_t, maybe, or uint64_t, or ...int128_t ? long long is less determined across architectures methinks.

Also: do we really need more than, say uint64_t ?

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Indeed, I pulled that from older code; I have changed that to std::int_fast64_t as that is what's used throughout the library.

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

I tend to use plain int64_t more but I trust the CCTZ folks on this :)

{NULL, NULL, 0}
};

RcppExport void R_init_RcppCCTZ(DllInfo *dll) {
R_RegisterCCallable("RcppCCTZ", "_RcppCCTZ_getOffset", (DL_FUNC) &_RcppCCTZ_getOffset);

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Registering at the C level for other functions is such a good idea that I am really embarrassed I did not think of it myself. There is probably scope for one or two more so yes, let's get this started!

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Yes, will try to think about a coherent set that would make sense. Shall I create an issue to keep track of this?

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Yes please! Issues are best for that.

const auto tp = time_point<seconds>(seconds(s));
auto abs_lookup = tz.lookup(tp);
return abs_lookup.offset;
}

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Nice work.

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Thanks; I have added a NEWS and ChangeLog and also bumped the minor version number from 0.2.6 to 0.2.7.

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Test is a little difficult, as we can't directly call this function from R, so we'd need a C++ only test. Not at all sure how to do this; shall we leave it as-is right now and look into this when we add additional C low-level routines?

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Good point about the test (esp from another package) -- that is doable but not worth it.

But can we test the basic offset computation? Maybe from a wrapper?

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

I think the only way we can test the call from R is if we make an R function for it or for a function that calls it (which is for example what nanotime does. Then we would have to document it at the R level, right? Or can we write a wrapper without exporting it at the package level (which we could call something like _RcppCCTZ_test_exported_C_functions and would simply return TRUE or FALSE)?

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

You probably have thought much much harder about this particular question than I have.

So if we can't, we can't. The tests can of course come from the nanotime side too.

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

nanotime makes that call and tests it with a few offsets, so I think it's OK for this function. That said, I'm not sure what's the best way to handle this issue that will come up again if additional functions are exported. Will think about that more...

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

In Rcpp we went balistic (as we had to ...) and within the unit testing framework build and a run a client package. It's all just scripting ...

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Seems the way to go: will look at how it's done in Rccp.

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Not sure we need to here. Tests over from nanotime seems fine and legit given that we own both ends.

…r to 'std::int_fast64_t'
Copy link
Owner

@eddelbuettel eddelbuettel left a comment

Don't mean to be a pain with ChangeLog and inst/NEWS.Rd. If you prefer that I do it that's fine too. Just let me know...

DESCRIPTION Outdated
@@ -1,8 +1,8 @@
Package: RcppCCTZ
Type: Package
Title: 'Rcpp' Bindings for the 'CCTZ' Library
Version: 0.2.6
Date: 2019-08-03
Version: 0.2.7

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

I tend not to make releases from PRs quite yet. Let's make tthat 0.2.6.1, and adjust the ChangeLog to one of those 'Roll minor version' entries as well?

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Absolutely, that makes sense.

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Btw, the reason a version change is good is that nanotime will depend on it in order for period to work correctly, so it's useful that we can request that specific version. So 0.2.6.1 works perfectly :)

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

I have been using that three-digits-for-releases and four for interim versions that are still differentiable for a really long time. It really works. :) And also with the package_version S3 class (and/or the dpkg package comparison).

NEWS Outdated
@@ -0,0 +1,4 @@
Changes in version 0.2.7

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Sorry -- that should into inst/NEWS.Rd.

This comment has been minimized.

@lsilvest

lsilvest Sep 7, 2019
Author Contributor

Ah, missed the location!

@@ -1,3 +1,11 @@
2019-09-07 Leonardo Silvestri <lsilvestri@ztsdb.org>

* DESCRIPTION (Version, Date): Release 0.2.7

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

'Roll minor version'

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Looks like this still says 0.2.7. I'll clean it up in a minute ...

This comment has been minimized.

@eddelbuettel

eddelbuettel Sep 7, 2019
Owner

Or maybe I am caught in a 'you are reviewing, finish this' loop at GH...

@eddelbuettel eddelbuettel merged commit 23cdb32 into eddelbuettel:master Sep 7, 2019
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@eddelbuettel
Copy link
Owner

@eddelbuettel eddelbuettel commented Sep 7, 2019

I edited the ChangeLog and pushed so make sure you pull if you need to make further changes.

Thanks for the PR -- very nice!

@lsilvest
Copy link
Contributor Author

@lsilvest lsilvest commented Sep 7, 2019

Excellent, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.