From 378fe5c197bf3556a34af6e5194858da8f7af1b3 Mon Sep 17 00:00:00 2001
From: Peder <pbac@dtu.dk>
Date: Fri, 2 Jul 2021 16:28:16 +0200
Subject: [PATCH] Fixed time zone subset issue

---
 R/data.list.R |  2 +-
 R/in_range.R  | 19 ++++++++++++-------
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/R/data.list.R b/R/data.list.R
index df10779..98ba83b 100644
--- a/R/data.list.R
+++ b/R/data.list.R
@@ -120,7 +120,7 @@ subset.data.list <- function(x, subset = NA, nms = NA, kseq = NA, lagforecasts =
                 if(length(grep("k[[:digit:]]+$", names(X))) > 0){
                     # If it holds forecasts, check that they are all there
                     if( !all(pst("k",kseq) %in% names(X)) ){
-                        warning(pst("The variable ",nms[i]," contain ",pst(names(X),collapse=",")," hence doesn't contain all k in kseq = ",pst(kseq,collapse=",")))
+                        warning(pst("The variable ",nms[i]," contains ",pst(names(X),collapse=",")," hence doesn't contain all k in kseq = ",pst(kseq,collapse=",")))
                     }
                 }
             }
diff --git a/R/in_range.R b/R/in_range.R
index cfad0a7..4b9e72c 100644
--- a/R/in_range.R
+++ b/R/in_range.R
@@ -44,25 +44,30 @@
 #' # since it's covering to "2010-12-25 23:00:00" to "2010-12-26 00:00:00"
 #' D$t[in_range("2010-12-26", D$t, "2010-12-27")]
 #'
+#' # When characters are given, then they are translated to the time zone of the time vector
+#' D <- subset(Dbuilding, c("2010-12-15", "2010-12-16"))
+#' D$t <- ct(D$t, tz="CET")
+#' D$t[in_range("2010-12-15 02:00", D$t, "2010-12-15 05:00")]
 #'
 #' @export
 
-in_range <- function(tstart, time, tend=NA, timezone=NA) {
+in_range <- function(tstart, time, tend=NA) {
     if (is.na(tend)){
         tend <- time[length(time)]
     }
     if(is.numeric(time)){
         return(tstart < time & time <= tend)
     }else{
+        # Use the time zone of the time when converting from character
+        tz <- attr(time, "tzone")
+        #
         if (class(tstart)[1] == "character"){
-            tstart <- ct(tstart)
+            tstart <- ct(tstart, tz=tz)
         }
         if (class(tend)[1] == "character"){
-            tend <- ct(tend)
+            tend <- ct(tend, tz=tz)
         }
-        if (is.na(timezone)){
-            timezone <- attr(time, "tzone")
-        }
-        return(ct(tstart, tz = timezone) < time & time <= ct(tend, tz = timezone))
+        #timezone <- attr(time, "tzone")
+        return(tstart < time & time <= tend)
     }
 }
-- 
GitLab