Coder Social home page Coder Social logo

rcppcctz's Introduction

RcppCCTZ: Rcpp bindings for CCTZ

CI License CRAN Dependencies Downloads Last Commit

What is CCTZ?

CCTZ (C++ Time Zone) is an excellent (yet small) C++11 library for translating between absolute times and civil times using the rules defined by a time zone. See its repository (as well as code) for very detailed documentation. CCTZ is being developed by Google but not an officially endorsed product.

What is RcppCCTZ?

This package wraps CCTZ for use by R via Rcpp.

Examples

Difference between Timezones

R> # simple call: difference now
R> tzDiff("America/New_York", "Europe/London", Sys.time())
[1] 5
R> # tabulate difference for every week of the year
R> table(sapply(0:52, function(d) tzDiff("America/New_York", "Europe/London",
+                                        as.POSIXct(as.Date("2016-01-01") + d*7))))

 4  5
 3 50

Shifting Timezone

R> # Given current time in NY what is the time London, UK
R> # (running the code locally in Chicago hence CST text format)    
R> toTz(Sys.time(), "America/New_York", "Europe/London") 	
[1] "2016-12-10 17:15:04.20370 CST"
R> # this redoes the 'Armstrong on the moon in NYC and Sydney' example
R> # note that the default print method will print the return object in _your local time_
R> toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"), "America/New_York", "Australia/Sydney", TRUE)
1969-07-20 22:56:00 -0400
1969-07-21 12:56:00 +1000
[1] "1969-07-20 21:56:00 CDT"
R> # whereas explicitly formating for Sydney time does the right thing
R> format(toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"),
+             "America/New_York", "Australia/Sydney", verbose=TRUE),
+        tz="Australia/Sydney")
1969-07-20 22:56:00 -0400
1969-07-21 12:56:00 +1000
[1] "1969-07-21 12:56:00"

Parsing and Formatting

R> now <- Sys.time()
R> formatDatetime(now)            # current (UTC) time, in full precision RFC3339
[1] "2016-12-10T18:23:03.327956+00:00"
R> formatDatetime(now, tgttzstr="America/New_York")  # same but in NY
[1] "2016-12-10T13:23:03.327956-05:00"
R> formatDatetime(now + 0:4)	   # vectorised
[1] "2016-12-10T18:23:03.327956+00:00" "2016-12-10T18:23:04.327956+00:00"
[3] "2016-12-10T18:23:05.327956+00:00" "2016-12-10T18:23:06.327956+00:00"
[5] "2016-12-10T18:23:07.327956+00:00"
R>

R> ds <- getOption("digits.secs")
R> options(digits.secs=6) # max value
R> parseDatetime("2016-12-07 10:11:12",        "%Y-%m-%d %H:%M:%S");   # full seconds
[1] "2016-12-07 04:11:12 CST"
R> parseDatetime("2016-12-07 10:11:12.123456", "%Y-%m-%d %H:%M:%E*S"); # fractional seconds
[1] "2016-12-07 04:11:12.123456 CST"
R> parseDatetime("2016-12-07T10:11:12.123456-00:00")  ## default RFC3339 format
[1] "2016-12-07 04:11:12.123456 CST"
R> now <- trunc(Sys.time())
R> parseDatetime(formatDatetime(now + 0:4))	   			# vectorised
[1] "2016-12-10 12:24:25 CST" "2016-12-10 12:24:26 CST" "2016-12-10 12:24:27 CST"
[4] "2016-12-10 12:24:28 CST" "2016-12-10 12:24:29 CST"
R> options(digits.secs=ds)

Requirements

The CCTZ library depends on timezone files typically found in /usr/share/zoneinfo which requires a Unix-alike OS such as Linux or OS X. Old school Unix variants may work.

Status

On CRAN, builds and tests cleanly, and the example functions are accessible from R.

Installation

The package is now on CRAN and can be installed via a standard

install.packages("RcppCCTZ")

Continued Testing

As we rely on the tinytest package, the already-installed package can also be verified via

tinytest::test_package("RcppCCTZ")

at any later point.

Author

Dirk Eddelbuettel

License

GPL (>= 2)

rcppcctz's People

Contributors

anderic1 avatar dcdillon avatar eddelbuettel avatar kiwiwarmnfuzzy avatar lsilvest avatar michaelquinn32 avatar qulogic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rcppcctz's Issues

FYI: CCTZ v2.0 is out, with some added features and spelling changes

Hi. I just wanted to let you know that we released a 2.0 version of https://github.com/google/cctz that adds a whole new civil-time library (for computing with human-scale time such as year, month, day, hour, minute, second). There are also some interface and spelling changes of the API points to make them match more standard C++ API (e.g., cctz::TimeZone -> cctz::time_zone). https://github.com/google/cctz/wiki/Migrating-from-V1-to-V2-interface has more info.

Anyway, I just wanted to let you know. Let us know if you run into any problems. Thanks.

Nanosecond precision loss in 'parseDouble'

parseDouble in some cases loses the last digits of the nanoseconds.

Example:

RcppCCTZ::parseDouble("2120-01-01T00:00:59.987654321+00:00")

##           [,1]      [,2]
## [1,] 4733510459 987653809

instead of the expected result:

##           [,1]      [,2]
## [1,] 4733510459 987654321

The problem comes from the coercion to double in parseDouble (utilities.cpp, lines 275-276):

double secs = std::trunc(nanoseconds / 1.0e9);
auto nanos = nanoseconds - static_cast<int64_t>(secs * 1e9);

Maybe the following works?

auto secs = nanoseconds / 1000000000;
auto nanos = nanoseconds - secs * 1000000000;

bug.report information:

Package: RcppCCTZ
 Version: 0.2.0
 Maintainer: Dirk Eddelbuettel <[email protected]>
 Built: R 3.3.1; x86_64-pc-linux-gnu; 2017-01-27 16:54:31 UTC; unix

R Version:
 platform = x86_64-pc-linux-gnu
 arch = x86_64
 os = linux-gnu
 system = x86_64, linux-gnu
 status = 
 major = 3
 minor = 3.1
 year = 2016
 month = 06
 day = 21
 svn rev = 70800
 language = R
 version.string = R version 3.3.1 (2016-06-21)
 nickname = Bug in Your Hair

Locale:
 LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

Search Path:
 .GlobalEnv, package:nanotime, ESSR, package:stats, package:graphics,
 package:grDevices, package:utils, package:datasets, package:methods,
 Autoloads, package:base

ERROR: compilation failed for package 'RcppCCTZ'

Hi,
I'm trying to install RcppCCTZ 0.2.3 on RHEL 6.4 with R 3.4.0

And getting the following error messages.

install.packages('RcppCCTZ')
Installing package into ‘/home//Rlibs’
(as ‘lib’ is unspecified)
trying URL 'http://cran.mtu.edu/src/contrib/RcppCCTZ_0.2.3.tar.gz'
Content type 'application/x-gzip' length 62869 bytes (61 KB)
==================================================
downloaded 61 KB

  • installing source package ‘RcppCCTZ’ ...
    ** package ‘RcppCCTZ’ successfully unpacked and MD5 sums checked
    ** libs
    g++ -m64 -std=gnu++0x -I/usr/include/R -DNDEBUG -I"/home/jari/Rlibs/bio3d/Rcpp/include" -I/usr/local/include -I../inst/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -c RcppExports.cpp -o RcppExports.o
    g++ -m64 -std=gnu++0x -I/usr/include/R -DNDEBUG -I"/home/jari/Rlibs/bio3d/Rcpp/include" -I/usr/local/include -I../inst/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -c civil_time_detail.cc -o civil_time_detail.o
    In file included from civil_time_detail.cc:1:
    ../inst/include/civil_time_detail.h:37: error: expected nested-name-specifier before ‘year_t’
    ../inst/include/civil_time_detail.h:37: error: ‘year_t’ has not been declared
    ../inst/include/civil_time_detail.h:37: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:37: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:41: error: expected nested-name-specifier before ‘diff_t’
    ../inst/include/civil_time_detail.h:41: error: ‘diff_t’ has not been declared
    ../inst/include/civil_time_detail.h:41: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:41: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:46: error: expected nested-name-specifier before ‘month_t’
    ../inst/include/civil_time_detail.h:46: error: ‘month_t’ has not been declared
    ../inst/include/civil_time_detail.h:46: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:46: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:47: error: expected nested-name-specifier before ‘day_t’
    ../inst/include/civil_time_detail.h:47: error: ‘day_t’ has not been declared
    ../inst/include/civil_time_detail.h:47: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:47: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:48: error: expected nested-name-specifier before ‘hour_t’
    ../inst/include/civil_time_detail.h:48: error: ‘hour_t’ has not been declared
    ../inst/include/civil_time_detail.h:48: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:48: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:49: error: expected nested-name-specifier before ‘minute_t’
    ../inst/include/civil_time_detail.h:49: error: ‘minute_t’ has not been declared
    ../inst/include/civil_time_detail.h:49: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:49: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:50: error: expected nested-name-specifier before ‘second_t’
    ../inst/include/civil_time_detail.h:50: error: ‘second_t’ has not been declared
    ../inst/include/civil_time_detail.h:50: error: expected ‘;’ before ‘=’ token
    ../inst/include/civil_time_detail.h:50: error: expected unqualified-id before ‘=’ token
    ../inst/include/civil_time_detail.h:54: error: expected ‘)’ before ‘year’
    ../inst/include/civil_time_detail.h:78: error: ‘cctz::detail::impl::is_leap_year’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:78: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:78: error: expected ‘,’ or ‘;’ before ‘noexcept’
    ../inst/include/civil_time_detail.h:81: error: ‘cctz::detail::impl::year_index’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:81: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:81: error: ‘month_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:81: error: initializer expression list treated as compound expression
    ../inst/include/civil_time_detail.h:81: error: expected ‘,’ or ‘;’ before ‘noexcept’
    ../inst/include/civil_time_detail.h:84: error: ‘cctz::detail::impl::days_per_century’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:84: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:84: error: ‘month_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:84: error: initializer expression list treated as compound expression
    ../inst/include/civil_time_detail.h:84: error: expected ‘,’ or ‘;’ before ‘noexcept’
    ../inst/include/civil_time_detail.h:88: error: ‘cctz::detail::impl::days_per_4years’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:88: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:88: error: ‘month_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:88: error: initializer expression list treated as compound expression
    ../inst/include/civil_time_detail.h:88: error: expected ‘,’ or ‘;’ before ‘noexcept’
    ../inst/include/civil_time_detail.h:92: error: ‘cctz::detail::impl::days_per_year’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:92: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:92: error: ‘month_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:92: error: initializer expression list treated as compound expression
    ../inst/include/civil_time_detail.h:92: error: expected ‘,’ or ‘;’ before ‘noexcept’
    ../inst/include/civil_time_detail.h:95: error: ‘cctz::detail::impl::days_per_month’ declared as an ‘inline’ variable
    ../inst/include/civil_time_detail.h:95: error: ‘year_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:95: error: ‘month_t’ was not declared in this scope
    ../inst/include/civil_time_detail.h:95: error: initializer expression list treated as compound expression
    ../inst/include/civil_time_detail.h:95: error: expected ‘,’ or ‘;’ before ‘noexcept’
    civil_time_detail.cc:72: error: expected ‘}’ at end of input
    civil_time_detail.cc:72: error: expected ‘}’ at end of input
    civil_time_detail.cc:72: error: expected ‘}’ at end of input
    make: *** [civil_time_detail.o] Error 1
    ERROR: compilation failed for package ‘RcppCCTZ’
  • removing ‘/home/jari/Rlibs/bio3dweb/RcppCCTZ’

The downloaded source packages are in
‘/tmp/RtmpHjN8UY/downloaded_packages’
Warning message:
In install.packages("RcppCCTZ") :
installation of package ‘RcppCCTZ’ had non-zero exit status

My sessionInfo()

R version 3.4.0 (2017-04-21)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server release 6.4 (Santiago)

Matrix products: default
BLAS: /usr/lib64/R/lib/libRblas.so
LAPACK: /usr/lib64/R/lib/libRlapack.so

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_3.4.0 tools_3.4.0

Header <R_ext/Rdynload.h> missing for RcppCCTZ_API.h, needed by R_GetCCallable.

In RcppCCTZ_API.h, there's a reference to R_GetCCallable, which is defined in <R_ext/Rdynload.h> header. However, the header isn't in the #include lines of the header file or being included transitively by any of the existing header includes.

The header would be more conformant to best practices if the <R_ext/Rdynload.h> is included.

RcppCCTZ does not build on FreeBSD

On a recent FreeBSD HEAD and R-3.3.2, built with gcc-4.9.4, the following built error occurs with RcppCCTZ_0.2.1:

[..snip..]
g++49 -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/library/Rcpp/include" -DLIBICONV_PLUG -I/usr/local/include -isystem /usr/local/include  -I../inst/include -fpic  -O2 -pipe -DLIBICONV_PLUG -fstack-protector -Wl,-rpath=/usr/local/lib/gcc49 -isystem /usr/local/include -fno-strict-aliasing  -DLIBICONV_PLUG -Wl,-rpath=/usr/local/lib/gcc49 -isystem /usr/local/include -c time_zone_info.cc -o time_zone_info.o
time_zone_info.cc: In function 'char* cctz::{anonymous}::errmsg(int, char*, size_t)':
time_zone_info.cc:64:40: error: invalid conversion from 'int' to 'char*' [-fpermissive]
   return strerror_r(errnum, buf, buflen);
                                        ^
*** Error code 1

I have no clue, what's wrong here. Any help is really appreciated.

Rainer Hurling

Parse error for dates without separator

This works:

RcppCCTZ::parseDatetime("2019-09-30 2346", "%Y-%m-%d %H%M")
# [1] "2019-09-30 23:46:00 UTC"

And I believe this should work too:

RcppCCTZ::parseDatetime("20190930 2346", "%Y%m%d %H%M")
# Error in RcppCCTZ::parseDatetime("20190930 2346", "%Y%m%d %H%M") : 
#   Parse error on 20190930 2346

comments and clarification on toTz

I am not very clear about the intention of the toTz. All examples use UTC as the baseline. For another baseline tz the results are not meaningful:

> tt <- ISOdate(2017, 05, 01, 0, tz = "America/New_York")
> tt
[1] "2017-05-01 EDT"
> RcppCCTZ::toTz(tt,  "America/New_York", "Europe/Amsterdam", T)
2017-05-01 04:00:00 -0400
2017-05-01 10:00:00 +0200
[1] "2017-05-01 10:00:00 CEST"

Judging from the description of the function, the results should be 06:00:00 CEST (=Europe/Amsterdam).

If the intention is to render the time instant in a different time zone, then an easier option would be to simply change the tzone attribute:

> tt
[1] "2017-05-01 EDT"
> attr(tt, "tzone") <- "Europe/Amsterdam"
> tt
[1] "2017-05-01 06:00:00 CEST"

or do I miss something?

How to actually link to cctz from external package?

Sorry for spaming today but what is the correct way to link to cctz? Contrary to what they claim cctz is not headers only, so how do I correctly link to it?

Simply having LinkingTo: RcppCCTZ is not enough. I am getting:

** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/home/vspinu/.lib/R/lubridate/libs/lubridate.so':
  /home/vspinu/.lib/R/lubridate/libs/lubridate.so: undefined symbol: _ZNK4cctz9time_zone6lookupERKNS_6detail10civil_timeINS1_10second_tagEEE
Error: loading failed

Naturally, cctz object files are not there. So, should I include all relevant xxx.cc cctz files in the package just like you do in rcppcctz? Or there is a more elegant solution?

Unexpected result tzDiff

Hi,

Using tzDiff with one of the timezones set to "Europe/London" gives unexpected results. Here is an example:

library(RcppCCTZ)
dt = as.Date("2010-01-01")
fromtz = "Europe/London"
totz="Europe/Berlin"
tzDiff(fromtz, totz, dt, verbose=TRUE)

The last line returns 0, where a 1 hour difference would be expected. verbose=TRUE seems to indicate the "Europe/London" date gets parsed as a +0100 timezone-offset instead of +0000.

As a comparison, both the following lines return a 1h difference:

parseDatetime(format(dt), "%Y-%m-%d", fromtz) - parseDatetime(format(dt), "%Y-%m-%d", totz)
as.POSIXct(format(dt), tz=fromtz) - as.POSIXct(format(dt), tz=totz)

Conversion to time_t ?

Not clear yet how to convert an auto tp = cctz::convert(...) into a (fractional) double to create a POSIXct for R.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.