s <- c(" 1999-08-19 10:00:31 ",
" 1999-08-19 10:00",
"19.8.1999 10:00",
"8/19/99 10:00:31",
"8/19/1999 10:00:31",
"19.8.1999 10:00:31")
guess_datetime(s)
## the actual rules
rules <- as.data.frame(matrix(datetimeutils:::.dt_patterns,
byrow = TRUE, ncol = 2),
stringsAsFactors = FALSE)
names(rules) <- c("pattern", "assumed_format")
rules
## ----------------------------------
## a function for finding old files by looking at the
## dates in filenames (e.g. in a backup directory)
old_files <- function(min.age = 365, ## in days
path = ".",
recursive = FALSE,
full.names = FALSE) {
files <- dir(path, recursive = recursive, full.names = full.names)
dates <- guess_datetime(files, date.only = TRUE, within = TRUE)
age <- as.numeric(Sys.Date() - dates)
old <- age >= min.age
files[ !is.na(old) & old ]
}
## ----------------------------------
## specifying additional formats
s <- c("19-08-99",
"29-2-00")
guess_datetime(s, date.only = TRUE)
## NA NA
guess_datetime(s, date.only = TRUE,
try.patterns = c("[0-9]+-[0-9]+-[0-9]+", "%d-%m-%y"))
## "1999-08-19" "2000-02-29"
Run the code above in your browser using DataLab