Get time from xts index

I have an xts, x object. I want to run a for loop before a certain time based on the timestamp in the index.

> index(x) [1] "2011-10-12 16:44:00 SAST" 

Suppose I want to run my loop while the time in the index is less than 16:40:00. How to disable the time component given the index format above?

+6
source share
2 answers

This should lead you to the ball park where you want to be. Using format , you extract only hours, minutes and seconds. The help page for ?strptime gives more detailed information about the characters used to extract the information.

 #if you don't have your string in an appropriate format yet (x <- strptime("2011-10-12 16:44:00 SAST", format = "%Y-%m-%d %H:%M:%S")) [1] "2011-10-12 16:44:00" class(x) [1] "POSIXlt" "POSIXt" (new.x <- format(x, format = "%H:%M:%S")) [1] "16:44:00" 
+4
source

The problem is choosing the right time zone. On my system, the tz "SAST" specification is not valid, but maybe at your level would be:

 x[ index(x) < as.POSIXct( "2011-10-12 16:44:00", tz= SAST") ] 

(UTC +2 appears.) And I return what I said that my system does not recognize it.

  as.POSIXct( "2011-10-12 16:44:00", tz= "Africa/Johannesburg") # [1] "2011-10-12 16:44:00 SAST" 

Therefore use:

 x[ index(x) <= as.POSIXct( "2011-10-12 16:44:00", tz= "Africa/Johannesburg")] 
+2
source

Source: https://habr.com/ru/post/899192/


All Articles