• Stars
    star
    153
  • Rank 230,145 (Top 5 %)
  • Language
    R
  • License
    GNU General Publi...
  • Created over 7 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Anything to POSIXct or Date Converter

anytime: Anything to 'POSIXct' or 'Date' Converter

CI License CRAN Dependencies CRAN use Downloads (monthly) Downloads (total) Code Coverage Last Commit Documentation JOSS

Motivation

R excels at computing with dates, and times. Using typed representation for your data is highly recommended not only because of the functionality offered but also because of the added safety stemming from proper representation.

But there is a small nuisance cost in interactive work as well as in programming. Users must have told as.POSIXct() about a million times that the origin is (of course) the epoch. Do we really have to say it a million more times? Similarly, when parsing dates that are some form of YYYYMMDD format, do we really have to manually convert from integer or numeric or factor or ordered to character? Having one of several common separators and/or date / time month forms (YYYY-MM-DD, YYYY/MM/DD, YYYYMMDD, YYYY-mon-DD and so on, with or without times), do we really need a format string? Or could a smart converter function do this?

anytime() aims to be that general purpose converter returning a proper POSIXct (or Date) object no matter the input (provided it was somewhat parseable), relying on Boost date_time for the (efficient, performant) conversion. anydate() is an additional wrapper returning a Date object instead.

Documentation

Package documentation, help pages, a vignette, and more is available here.

Examples

We show some simple examples on Date types.

(Note that in the first few examples, and for numeric conversion in this range we now only use anydate as anytime is consistent in computing seconds since epoch. If you want the behaviour of version older than 0.3.0, set oldHeuristic=TRUE, see help(anytime) for more.)

From Integer or Numeric or Factor or Ordered

library(anytime)                      ## also caches TZ in local env
options(digits.secs=6)                ## for fractional seconds below

## integer
anydate(20160101L + 0:2)              ## older version used anytime for this too
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

## numeric
anydate(20160101 + 0:2)
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

## factor
anydate(as.factor(20160101 + 0:2))
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

## ordered
anydate(as.ordered(20160101 + 0:2))
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

Character: Simple

## Dates: Character
anydate(as.character(20160101 + 0:2))
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

## Dates: alternate formats
anydate(c("20160101", "2016/01/02", "2016-01-03"))
[1] "2016-01-01 CST" "2016-01-02 CST" "2016-01-03 CST"

Character: ISO

## Datetime: ISO with/without fractional seconds
anytime(c("2016-01-01 10:11:12", "2016-01-01 10:11:12.345678"))
[1] "2016-01-01 10:11:12.000000 CST" "2016-01-01 10:11:12.345678 CST"

## Datetime: ISO alternate (?) with 'T' separator
anytime(c("20160101T101112", "20160101T101112.345678"))
[1] "2016-01-01 10:11:12.000000 CST" "2016-01-01 10:11:12.345678 CST"

Character: Textual month formats

## ISO style
anytime(c("2016-Sep-01 10:11:12", "Sep/01/2016 10:11:12", "Sep-01-2016 10:11:12"))
[1] "2016-09-01 10:11:12 CDT" "2016-09-01 10:11:12 CDT" "2016-09-01 10:11:12 CDT"

## Datetime: Mixed format (cf https://stackoverflow.com/questions/39259184)
anytime(c("Thu Sep 01 10:11:12 2016", "Thu Sep 01 10:11:12.345678 2016"))
[1] "2016-09-01 10:11:12.000000 CDT" "2016-09-01 10:11:12.345678 CDT"

Character: Dealing with DST

This shows an important aspect. When not working localtime (by overriding to UTC) the changing difference UTC is correctly covered (which the underlying Boost Date_Time library does not by itself).

## Datetime: pre/post DST
anytime(c("2016-01-31 12:13:14", "2016-08-31 12:13:14"))
[1] "2016-01-31 12:13:14 CST" "2016-08-31 12:13:14 CDT"
anytime(c("2016-01-31 12:13:14", "2016-08-31 12:13:14"), tz="UTC")  # important: catches change
[1] "2016-01-31 18:13:14 UTC" "2016-08-31 17:13:14 UTC"

Technical Details

The heavy lifting is done by a combination of Boost lexical_cast to go from anything to string representation which is then parsed by Boost Date_Time. We use the BH package to access Boost, and rely on Rcpp for a seamless C++ interface to and from R.

Further, as the Boost Date_Time library cannot resolve timezones on the Windows platform (where timezone information is typically provided by R itself for its use), we offer a fallback of calling into R (via facilities from Rcpp); see the help for the useR argument for more details.

Status

The package should work as expected.

Example Uses

Several different CRAN packages import this package. Among them are the following research-focused packages:

  • adheRenceRX by Beal assesses medication adherence;
  • AGread by Hibbing et al which reads and transforms ActiGraph physical activity measures;
  • cqcr by Odell accesses 'Care Quality Commission' data from the health and adult social care regulator for England;
  • datadogr by Yutani queries metrics from Datadog;
  • E4tools by Kleiman which reads data from Empatica wearable physiology monitors;
  • nprcgenekeepr by Raboin et al provides genetic tools for colony management ;
  • RDS by Handcock et al which is part of the "RDS Ananlyst" suite for analysing respondent-driven sampling data;
  • rtsdata by RTSVizTeam manages time series data dtorage;
  • threesixtygiving by Odell accesses download charitable grants from the '360Giving' Platform;
  • tsbox by Sax for format-agnostic time series data representation and conversions;
  • tsibble by Wang et al for temporal data in an explicit data- and model-oriented format.

Changes

See the NEWS.Rd file on CRAN or GitHub. In particular, version 0.3.0 corrects an overly optimistic heuristic for integer or numeric arguments and now behaves more like R itself. Specifically, epoch offsets are interpreted as seconds for datetime objects, and days for date objects. The prior behaviour can be restored with an option which also be be set globally, see the help page for details.

Installation

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

install.packages("anytime")

Continued Testing

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

tinytest::test_package("anytime")

at any later point.

Contributing

Any problems, bug reports, or features requests for the package can be submitted and handled most conveniently as Github issues in the repository.

Before submitting pull requests, it is frequently preferable to first discuss need and scope in such an issue ticket. See the file Contributing.md (in the Rcpp repo) for a brief discussion.

Author

Dirk Eddelbuettel

License

GPL (>= 2)

More Repositories

1

littler

A scripting and command-line front-end for GNU R
R
293
star
2

tint

Tint is not Tufte
R
259
star
3

gsir-te

Getting Started in R -- Tinyverse Edition
R
226
star
4

mkl4deb

Adding the Intel MKL to a Debian / Ubuntu system via one simple script
Shell
204
star
5

binb

Binb is not Beamer
TeX
188
star
6

r2u

CRAN as Ubuntu Binaries
R
161
star
7

drat

Drat R Archive Template
HTML
148
star
8

pinp

Pinp Is Not PNAS -- Two-Column PDF Template
TeX
146
star
9

rinside

Seamless embedding of R in C++ programs
C++
130
star
10

rcppsimdjson

Rcpp Bindings for the 'simdjson' Header Library
C++
112
star
11

rquantlib

R interface to the QuantLib library
C++
111
star
12

linl

Linl Is Not Letter -- Markdown-based LaTeX Letter Template
TeX
109
star
13

digest

R package to create compact hash digests of R objects
C
108
star
14

rpushbullet

R interface to the awesome Pushbullet service
R
94
star
15

bh

R package providing Boost Header files
C++
80
star
16

crp

Archived copies of the CRAN Repo Policy
HTML
72
star
17

rcppannoy

Rcpp bindings for Annoy
C++
69
star
18

rprotobuf

R Interface to Protocol Buffers
C++
68
star
19

ctv-finance

CRAN Task View: Empirical Finance
R
55
star
20

samples-rmarkdown-metropolis

RMarkdown with Metropolis/Mtheme for Beamer
Makefile
52
star
21

nanotime

Nanosecond Resolution Time Functionality for R
R
50
star
22

rcppredis

R interface to Redis using the hiredis library
C
49
star
23

rcppexamples

Examples of using Rcpp to interface R and C++
C++
45
star
24

inline

Inline C, C++ or Fortran functions in R
R
40
star
25

tidycpp

Tidy C++ wrapping of the C API of R
C++
38
star
26

ctv-hpc

CRAN Task View: High-Performance Computing with R
R
38
star
27

rcppkalman

Kalman filtering via RcppArmadillo
MATLAB
35
star
28

pkgkitten

Create simple packages which pass R CMD check
R
33
star
29

rcpptoml

Rcpp Bindings to C++ parser for TOML files
C++
31
star
30

rcppgsl

Rcpp integration for GNU GSL vectors and matrices
TeX
30
star
31

rfoaas

R interface to FOAAS service
R
28
star
32

rcppcnpy

Rcpp bindings for NumPy files
C++
26
star
33

rvowpalwabbit

R interface to the Vowpal Wabbit
C++
24
star
34

rcppcctz

Rcpp interface to CCTZ library
C++
22
star
35

r-ci

CI for R at Travis, GitHub Actions, Azure Pipelines, ...
22
star
36

ttdo

Extend tinytest with diffobj
R
21
star
37

rcppmsgpack

MsgPack Headers for R / msgpack.org[R]
C++
19
star
38

rcppfastfloat

Rcpp Bindings for the 'fastfloat' Header-Only Library
C++
18
star
39

rcppbdt

Rcpp bindings for Boost Date_Time
C++
17
star
40

docker-ubuntu-r

Docker images for R on Ubuntu
Shell
17
star
41

rcppstreams

A C++ DSEL for real-time event stream processing
C++
15
star
42

t4

Support repo for T^4 Video Lightning Talks
Shell
15
star
43

rticles-gallery

Preview of Templates in the rticles Package
R
14
star
44

dtts

Time-series functionality based on nanotime and data.table
R
14
star
45

rcppspdlog

Bundling of spdlog for use from R and Rcpp
C++
14
star
46

filter-journal-spam

spamassassin blocked-list to avoid traffic from predatory publishers
Makefile
13
star
47

tiledb-user2021

Repository for useR! 2021 TileDB Tutorial Helper Package
R
13
star
48

rcppde

Rcpp port of Differential Evolution
C++
13
star
49

pgapack

A general-purpose, data-structure-neutral, and parallel genetic algorithm library
C
12
star
50

rcppnloptexample

Rcpp Example for accessing NLopt
C++
12
star
51

ppa-rstudio

apt install rstudio rstudio-server quarto
R
12
star
52

prrd

Parallel Running of Reverse Depends
R
12
star
53

rcppdate

R package providing date C++ library header files
C++
11
star
54

lwplot

(Experimental but working) LightWeight Plot / Leland Wilkinson Plot -- a ggplot2 2.1.0 fork aiming for lighter weight
R
11
star
55

rcppziggurat

Rcpp bindings for different Ziggurat RNG implementations
C++
11
star
56

pkg-fonts-fira

(Unofficial) Debian packaging for Mozilla Fira fonts
Makefile
11
star
57

asioheaders

R package providing Asio C++ library header files
C++
11
star
58

td

R interface to 'twelvedata' API
R
11
star
59

rapiserialize

Serialization from the C API for R
C++
10
star
60

minm

Minm Is Not Meta: One way to get several RMarkdown-using packages
10
star
61

rapidatetime

Datetime functionality from the C API for R
C
10
star
62

samples-uzuerich-2017-06

Some Examples for Rcpp Workshop
9
star
63

rcppxts

Rcpp interface to xts objects
C++
9
star
64

rcppapt

Rcpp Interface to the APT Package Manager
C++
9
star
65

dang

A collection of utility functions for R
R
8
star
66

samplecode

Example code for talks or workshops
HTML
8
star
67

earthmovdist

Earth Mover's Distance for R via the Emd-L1 library
C++
8
star
68

rcpputs

Rcpp bindings for algorithms for unevenly spaced time series
C++
8
star
69

random

R package for true random numbers from random.org
R
8
star
70

dieharder-rgb

Random number generator tester
C
7
star
71

beancounter

Stock Portfolio Performance Tool
Perl
7
star
72

gcbd

R package for GPU/CPU benchmarking on Debian-based systems
R
7
star
73

rcppfastad

Rcpp Bindings to FastAD Automatic Differentiation
C++
7
star
74

rdieharder

R interface to the Dieharder RNG test suite
C
7
star
75

user2022-r-core-panel

useR! 2022 R Core Keynote and Panel
6
star
76

r-travis

Tools for using R with Travis, GitHub Actions and other CI backends
Shell
6
star
77

docker-debian-r

Docker image for R on Debian
Shell
6
star
78

dieharder

C
6
star
79

ldlasb

Lies, Damned Lies, and Selective Benchmarks
C++
5
star
80

rf2

Iteration Two of RinFinance.com
5
star
81

rcpp_comparison_convolution

Rcpp versus C on the Standard Convolution Example
R
5
star
82

rbenchmark

Benchmarking routine for R
R
5
star
83

safy

Show the Global Environment Some Appreciation
R
5
star
84

snap-r-base

Snapcraft for R
R
5
star
85

tinythemes

Lightweight Repackaging of 'Themes' for 'ggplot2'
R
5
star
86

pkg-latex-metropolis

[deprecated as Metropolis is now in TeXLive and hence Debian] (Unofficial) Debian packaging for Metropolis theme for Beamer
TeX
5
star
87

sanitizers

Sample R package with C/C++ code to trigger Address and Undefined Behaviour Sanitizers
C++
5
star
88

stackoverflow

Grabbag repo collecting some answers I posted. If only I had started this repo a decade ago...
HTML
5
star
89

data-examples

Unpacked data sets with Gapminder, Titanic and more
R
4
star
90

rocker-tiledb

Unofficial Development Docker files for R and TileDB
Makefile
4
star
91

rmsfact

Amazing Random Facts About the World's Greatest Hacker
R
4
star
92

arch

Arrow R and C Helpers
C
4
star
93

gaussfacts

The Greatest Mathematician since Antiquity
R
4
star
94

pkg-fonts-jetbrains-mono

(Unofficial) Debian packaging for Jetbrains Mono fonts
Makefile
4
star
95

pkg-fonts-plex

(Unofficial) Debian packaging for IBM Plex fonts
Makefile
4
star
96

drr35

Temporary repository for Debian packages built with R 3.5.0
3
star
97

docker-swc

Docker image for Software Carpentry
3
star
98

crc32c

R Package for crc32c with hardware-acceleration and software fallback
C++
3
star
99

cook-county-tax-model

Modeling Cook County Property Taxes
R
3
star
100

rcppnarrow

Some Lightweight Wrapping of narrow via Rcpp
C++
3
star