...
 
Commits (7)
2017-10-24 Enrico Schumann <es@enricoschumann.net>
* DESCRIPTION (Version): Version: 0.5-0
* R/functions.R (ts_table): store timestamp as
numeric (which reverts the change introduced in
version 0.4-7; see ChangeLog 2017-05-22)
* R/functions.R (as.zoo.ts_table): fix method
* inst/unitTests/ut_write_read.R
(test.ts_table): fix unit tests
* R/functions.R (ttime): 'strip.attr' is TRUE
by default (i.e. a timezone attribute is
dropped)
2017-09-14 Enrico Schumann <es@enricoschumann.net>
* R/functions.R (file_info): return more
......
Package: tsdb
Type: Package
Title: Terribly-Simple Data Base for Time Series
Version: 0.4-8
Date: 2017-05-26
Version: 0.5-0
Date: 2017-10-24
Maintainer: Enrico Schumann <es@enricoschumann.net>
Authors@R: person("Enrico", "Schumann",
role = c("aut", "cre"),
......
......@@ -5,7 +5,7 @@
## ---------------- time
ttime <- function(x, from = "datetime", to = "numeric",
tz = "", strip.attr = FALSE,
tz = "", strip.attr = TRUE,
format = "%Y-%m-%d") {
if (from == "datetime" && to == "numeric") {
......@@ -345,14 +345,15 @@ ts_table <- function(data, timestamp, columns) {
## TODO if character, match regexp and then coerce
## to Date/POSIXct?
if (inherits(timestamp, "POSIXlt")) {
timestamp <- c(as.POSIXct(timestamp))
timestamp <- ttime(as.POSIXct(timestamp))
t.type <- "POSIXct"
}
if (inherits(timestamp, "POSIXct")) {
timestamp <- c(timestamp)
timestamp <- ttime(timestamp)
t.type <- "POSIXct"
}
if (inherits(timestamp, "Date")) {
timestamp <- ttime(timestamp)
t.type <- "Date"
}
ans <- as.matrix(data)
......@@ -366,7 +367,7 @@ ts_table <- function(data, timestamp, columns) {
if (is.unsorted(timestamp)) {
ii <- order(timestamp)
timestamp <- timestamp[ii]
ans <- ans[ii, ,drop = FALSE]
ans <- ans[ii, , drop = FALSE]
}
attr(ans, "timestamp") <- timestamp
attr(ans, "t.type") <- t.type
......@@ -389,7 +390,8 @@ as.ts_table.zoo <- function(x, columns, ...) {
}
as.zoo.ts_table <- function(x, ...) {
ans <- zoo(unname(as.matrix(x)), .timestamp(x))
ans <- zoo(unname(as.matrix(x)),
ttime(.timestamp(x), "numeric", .t.type(x)))
colnames(ans) <- .columns(x)
ans
}
......@@ -418,14 +420,16 @@ as.matrix.ts_table <- function(x, ...) {
ans <- c(x)
dim(ans) <- d
colnames(ans) <- col
rownames(ans) <- as.character(ttime(timestamp, from = "numeric", to = .t.type(x)))
rownames(ans) <- as.character(ttime(timestamp,
from = "numeric",
to = .t.type(x)))
ans
}
print.ts_table <- function(x, ...) {
tmp <- .timestamp(x)
from_to <- if (length(tmp))
range(tmp)
ttime(range(tmp), "numeric", .t.type(x))
else
c(NA, NA)
if (nrow(x))
......
......@@ -10,8 +10,7 @@ test.ts_table <- function() {
checkEquals(y,
structure(11:15,
.Dim = c(5L, 1L),
timestamp = structure(c(16796, 16797, 16798, 16799, 16800),
class = "Date"),
timestamp = c(16796, 16797, 16798, 16799, 16800),
t.type = "Date",
columns = "close",
class = "ts_table"))
......@@ -29,8 +28,7 @@ test.ts_table <- function() {
checkEquals(y,
structure(11:15,
.Dim = c(5L, 1L),
timestamp = structure(c(16796, 16797, 16798, 16799, 16800),
class = "Date"),
timestamp = c(16796, 16797, 16798, 16799, 16800),
t.type = "Date",
columns = "close",
class = "ts_table"))
......@@ -41,10 +39,9 @@ test.ts_table <- function() {
"close")
checkEquals(y,
structure(11:15, .Dim = c(5L, 1L),
timestamp = structure(c(1451642400, 1451642401,
timestamp = c(1451642400, 1451642401,
1451642402, 1451642403,
1451642404),
class = c("POSIXct", "POSIXt")),
t.type = "POSIXct",
columns = "close",
class = "ts_table"))
......@@ -173,6 +170,8 @@ test.write_ts_table <- function() {
"16806,15",
"16807,16"))
## writing an empty ts_table does no harm
x <- ts_table(numeric(0),
timestamp = Sys.Date()[0],
columns = "x")
......@@ -261,3 +260,23 @@ test.write_ts_table <- function() {
checkEquals(ans, 0)
checkTrue(file.exists(file.path(dir, "EMPTY_FILE")))
}
test.zoo <- function() {
require("RUnit")
require("tsdb")
require("zoo")
y <- ts_table(11:15, as.Date("2016-1-1")-5:1, "close")
checkEqualsNumeric(zoo::as.zoo(y),
zoo::zoo(as.matrix(y), as.Date("2016-1-1")-5:1))
y <- zoo(11:15, as.Date("2016-1-1")-5:1)
checkEquals(as.ts_table(y, columns = "close"),
structure(11:15,
.Dim = c(5L, 1L),
timestamp = c(16796, 16797,
16798, 16799, 16800),
t.type = "Date", columns = "close",
class = "ts_table"))
}