Commit c6e124a2 authored by Enrico Schumann's avatar Enrico Schumann

[write_ts_table] Fix 'add' for series going back before 1970

See ChangeLog for details.
parent d721d7b4
This diff is collapsed.
Package: tsdb
Type: Package
Title: Terribly-Simple Data Base for Time Series
Version: 0.5-0
Date: 2017-10-24
Version: 0.6-0
Date: 2018-11-21
Maintainer: Enrico Schumann <es@enricoschumann.net>
Authors@R: person("Enrico", "Schumann",
Authors@R: person(given = "Enrico", family = "Schumann",
role = c("aut", "cre"),
email = "es@enricoschumann.net")
email = "es@enricoschumann.net",
comment = c(ORCID = "0000-0001-7601-6576"))
Description: A terribly-simple data base for time series. All
series are saved as csv files. The package offers utilities for
saving files in a standardised format, and for retrieving and
......
## The package uses code from Enrico Schumann's
## R package 'database'.
## Copyright Enrico Schumann 2010-2017
## Copyright Enrico Schumann 2010-2018
## ---------------- time
......@@ -61,8 +61,6 @@ write_ts_table <- function(ts, dir, file,
if (!(inherits(ts, "ts_table")))
stop(sQuote("ts"), " must be a ", sQuote("ts_table"))
save.scipen <- options(scipen = 1e5)
on.exit(options(scipen = save.scipen))
timestamp <- ttime(.timestamp(ts))
columns <- .columns(ts)
......@@ -180,13 +178,20 @@ read_ts_tables <- function(file, dir, t.type = "guess",
stop("check lengths of file and dir")
}
if (t.type == "guess" || missing(columns))
if (t.type == "guess" || missing(columns) || missing(start)) {
samp <- readLines(dfile[[1]], n = 2L)
samp.n <- length(samp)
}
if (missing(start) || t.type == "guess") {
if (samp.n == 2L)
timestamp1 <- as.numeric(
strsplit(samp[[2L]], ",", fixed = TRUE)[[1L]][[1L]])
else
timestamp1 <- numeric(0)
}
if (t.type == "guess") {
if (length(samp) == 2L) {
tmp <- as.numeric(
strsplit(samp[[2]], ",", fixed = TRUE)[[1L]][[1L]])
t.type <- if (tmp < 43200) ## 86400/2
if (samp.n == 2L) {
t.type <- if (timestamp1 < 43200) ## 86400/2
"Date" else "POSIXct"
} else
t.type <- "Date"
......@@ -198,7 +203,11 @@ read_ts_tables <- function(file, dir, t.type = "guess",
}
if (t.type == "Date") {
start <- if (missing(start))
start <- if (missing(start) && length(timestamp1))
ttime(timestamp1, "numeric", "Date")
else if (missing(start))
## a dummy date, only used for
## empty files
as.Date("1970-01-01")
else
as.Date(start)
......@@ -207,16 +216,16 @@ read_ts_tables <- function(file, dir, t.type = "guess",
previous_businessday(Sys.Date())
else
as.Date(end)
timestamp <- seq(start, end , "1 day")
if (drop.weekends)
timestamp <- timestamp[is_businessday(timestamp)]
} else if (t.type == "POSIXct") {
warning("'Oh boy', said Helen, 'that's not really supported.'")
start <- if (missing(start))
as.POSIXct(Sys.Date() - 365)
ttime(timestamp1,
from = "numeric", to = "POSIXct")
else
as.POSIXct(start)
as.POSIXct(start) ## in case it is POSIXlt
if (missing(end))
end <- as.POSIXct(previous_businessday(Sys.Date()))
......@@ -286,6 +295,14 @@ read_ts_tables <- function(file, dir, t.type = "guess",
ans <- data.frame(timestamp, results)
colnames(ans) <- c("timestamp", colnames)
ans
} else if (return.class == "ts_table") {
ans <- as.matrix(ans)
dimnames(ans) <- NULL
attr(ans, "t.type") <- t.type
attr(ans, "timestamp") <- ttime(timestamp)
attr(ans, "columns") <- columns
class(ans) <- "ts_table"
ans
} else {
stop("unknown ", sQuote("return.class"))
}
......
......@@ -2,9 +2,9 @@
test.ts_table <- function() {
require("RUnit")
require("tsdb")
require("zoo")
## require("RUnit")
## require("tsdb")
## require("zoo")
y <- ts_table(11:15, as.Date("2016-1-1")-5:1, "close")
checkEquals(y,
......@@ -50,6 +50,9 @@ test.ts_table <- function() {
test.read_ts_tables <- function() {
## require("RUnit")
## require("tsdb")
## require("zoo")
x <- ts_table(data = 11:15,
timestamp = as.Date("2016-1-1") + 1:5,
columns = "A")
......@@ -259,13 +262,33 @@ test.write_ts_table <- function() {
ans <- write_ts_table(x, dir, "EMPTY_FILE")
checkEquals(ans, 0)
checkTrue(file.exists(file.path(dir, "EMPTY_FILE")))
## add before 1970
dates <- seq(from = as.Date("1969-12-25"),
to = as.Date("1970-01-05"),
by = "1 day")
x <- ts_table(seq_along(dates),
timestamp = dates,
columns = "TEST")
write_ts_table(x, dir, "1970", replace.file = TRUE)
x <- ts_table(99,
as.Date("1970-1-6"),
columns = "TEST")
ans <- write_ts_table(x, dir, "1970", add = TRUE)
checkEqualsNumeric(ans, 1)
ans <- read_ts_tables("1970", dir, drop.weekends = FALSE)
checkEquals(ans$timestamp, c(dates, as.Date("1970-1-6")))
checkEqualsNumeric(ans$data, c(seq_along(dates), 99))
}
test.zoo <- function() {
require("RUnit")
require("tsdb")
require("zoo")
## require("RUnit")
## require("tsdb")
library("zoo", warn.conflicts = FALSE)
y <- ts_table(11:15, as.Date("2016-1-1")-5:1, "close")
checkEqualsNumeric(zoo::as.zoo(y),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment