rdrop2
rdrop2 copied to clipboard
Feature Request: Date Parser
Can we get a date parser for the modified field returned by e.g. drop_dir()?
Consider this ...
library(rdrop2)
drop_auth()
files_and_folders <- drop_dir()
files_and_folders$modified %>% head(5)
## [1] "Wed, 03 Jun 2015 09:27:11 +0000" "Thu, 07 Jan 2016 10:05:52 +0000"
## [3] "Wed, 01 Feb 2012 12:21:22 +0000" "Sat, 18 Oct 2014 08:15:01 +0000"
## [5] "Tue, 25 Nov 2014 14:14:00 +0000"
These are strings and very ugly to work with. No wouldn't it be nice to have function accompanying the package that allows to parse these strings into something one can work with?
Like this one? ...
#' parse dropbox dates into POSIXct
#' @param x dates to be parsed
drop_as_posixct <- function(x){
date <- substring(x,6,16)
time <- substring(x, 18, 25)
day <- substring(date, 1,2)
year <- substring(date, 8,12)
month <-
substring(date, 4,6) %>%
match(
c(
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
)
)
month[nchar(month)==1] <-
paste0("0", month[nchar(month)==1])
date_time <-
paste0(year, "-", month, "-", day, " ", time)
as.POSIXct(date_time, tz="UTC")
}
Now we get this ...
drop_as_posixct(files_and_folders$modified) %>% head()
## [1] "2015-06-03 09:27:11 UTC" "2016-01-07 10:05:52 UTC" "2012-02-01 12:21:22 UTC"
## [4] "2014-10-18 08:15:01 UTC" "2014-11-25 14:14:00 UTC" "2014-10-28 08:53:17 UTC"
... and can do stuff like that ...
drop_as_posixct(files_and_folders$modified) > (Sys.time() - 60*60*24*30)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [14] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
## [27] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
## [40] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
If you think this is worth putting in - I would thinks so - I can make a pull request. But I am not sure where to best put this that it gets integrated nicely into the package - i.e. should it be a separate function or should it be an un-exported helper that gets called automatically for stuff like drop_dir() and the user than gets already parsed time stamps in return?
... or structured like that to get it more ordered ...
#' parse dropbox dates into POSIXct
#' @param x dates to be parsed
#' @export
drop_date_as_posixct <- function(x){
time <- substring(x, 18, 25)
day <- drop_date_day(x)
year <- drop_date_year(x)
month <- drop_date_month(x)
date_time <- paste0(year, "-", month, "-", day, " ", time)
as.POSIXct(date_time, tz="UTC")
}
#' get year from dropbox dates
#' @param x dates to be parsed
drop_date_year <- function(x){
substring(x, 13, 16)
}
#' get month from dropbox dates
#' @param x dates to be parsed
drop_date_month <- function(x){
month <-
substring(x, 9,11) %>%
match(
c(
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
)
)
month[nchar(month)==1] <-
paste0("0", month[nchar(month)==1])
month
}
#' get day from dropbox dates
#' @param x dates to be parsed
drop_date_day <- function(x){
substring(x, 6,7)
}