Coder Social home page Coder Social logo

Comments (4)

eddelbuettel avatar eddelbuettel commented on May 28, 2024

Connecting the two packages also connects their dependencies which is something less desirable for the majority of users.

Maybe something like this belongs in a smaller utility package (just how in dang I have some xts / data.table mappers) ?

Apart from that, why not use what nanotime has? Aren't you just redoing this:

R> library(nanotime)
R> nt <- as.nanotime(Sys.time())
R> nt
[1] 2020-10-09T12:08:00.333868+00:00
R> as.Date(nt)
[1] "2020-10-09"
R> 

from anytime.

statquant avatar statquant commented on May 28, 2024

Indeed I did not remember as.Date.nanotime, I was fearing a doh moment but it seems this way is quite faster.

suppressMessages(expr = {                                                                                                                                                                                        
    library(nanotime)                                                                                                                                                                                            
    library(bit64)                                                                                                                                                                                               
    library(microbenchmark)                                                                                                                                                                                      
})                                                                                                                                                                                                               
                                                                                                                                                                                                                 
N <- 1e7                                                                                                                                                                                                         
x <- nanotime(Sys.time() + 864000 * runif(n = N, -1, 1))                                                                                                                                                         
                                                                                                                                                                                                                 
microbenchmark::microbenchmark(                                                                                                                                                                                  
    y <- as.Date(x),                                                                                                                                                                                             
    y1 <- as.Date(as.integer64(x)/1e9/86400, origin = "1970-01-01"),                                                                                                                                             
    times = 10L                                                                                                                                                                                                  
) -> res                                                                                                                                                                                                         
res                                                                                                                                                                                                              
# Unit: milliseconds                                                                                                                                                                                             
#                                                               expr      min       lq     mean   median       uq      max neval                                                                                 
#                                                    y <- as.Date(x) 416.4999 484.2233 494.6725 496.2450 517.1470 572.8053    10                                                                                 
#  y1 <- as.Date(as.integer64(x)/1e+09/86400, origin = "1970-01-01") 187.1953 197.8244 224.7171 203.0242 262.8207 271.0411    10                                                                                 
                                                                                                                                                                                                                 
identical(as.integer(y), as.integer(y1))                                                                                                                                                                         
# [1] TRUE                                                                                                                                                                                                       
                                                                                                                                                                                                                 
sessionInfo()                                                                                                                                                                                                    
# R version 4.0.2 (2020-06-22)                                                                                                                                                                                   
# Platform: x86_64-redhat-linux-gnu (64-bit)                                                                                                                                                                     
# Running under: Fedora 32 (Workstation Edition)                                                                                                                                                                 
#                                                                                                                                                                                                                
# Matrix products: default                                                                                                                                                                                       
# BLAS/LAPACK: /usr/lib64/libopenblas-r0.3.10.so                                                                                                                                                                 
 [line_length_linter] [I] Lines should not be more than 80 characters.                                                                                                                                           
# locale:                                                                                                                                                                                                        
#  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C    
#  [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C                                                                                                      
#                                                                                                                                                                                                                
# attached base packages:                                                                                                                                                                                        
# [1] stats     graphics  grDevices utils     methods   base                                                                                                                                                     
#                                                                                                                                                                                                                
# other attached packages:                                                                                                                                                                                       
# [1] microbenchmark_1.4-7 bit64_4.0.5          bit_4.0.4            nanotime_0.3.2       nvimcom_0.9-102      colorout_1.2-2                                                                                    
#                                                                                                                                                                                                                
# loaded via a namespace (and not attached):                                                                                                                                                                     
# [1] zoo_1.8-8         compiler_4.0.2    tools_4.0.2       RcppCCTZ_0.2.9    Rcpp_1.0.5        grid_4.0.2        lattice_0.20-41   CoprManager_0.3.5                                                            

You seem to be doing smart things in

##' @rdname nanotime
as.POSIXct.nanotime <- function(x, tz="", ...) {
    ## if (verbose) warning("Lossy conversion dropping precision")
    if ((tz == "" || is.null(tz)) && length(attr(x, "tzone")) > 0) {
        tz <- attr(x, "tzone")
    }
    pt <- as.POSIXct(as.double(S3Part(x, strictS3=TRUE)/1e9), tz=tz, origin="1970-01-01")
    pt
}

But that's not used (as far as I understand in as.Date (you might have forgotten to pass ...))

##' @rdname nanotime
as.Date.nanotime <- function(x, ...) {
    as.Date(as.POSIXct(x))
}

from anytime.

eddelbuettel avatar eddelbuettel commented on May 28, 2024

If the user passes nothing ... is an empty default. We have to have it in the signature...

If you don't mind, I am closing this. That your (to be frank a little more hackish int math) function is very well for you and you can and should use it if that is important to you. This was for starters at the wrong repo and there still is nothing deficient in anytime. Always eager to hear real issues (as e.g. in your earlier "bulk test" bug report) but this does not seem to be one.

from anytime.

statquant avatar statquant commented on May 28, 2024

Ok thanks, it's wrong package indeed.
I take from this there is no point changing as.Date in nanotime because you think the above "feels" hackish ? Or did I miss something and it is actually wrong ?
Regards

from anytime.

Related Issues (20)

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.