Labridate Interval Length

What is the best way to get the length of time represented by interval in lubridate , in specific units? All I can understand is something like the following dirty thing:

 > ival [1] 2011-01-01 03:00:46 -- 2011-10-21 18:33:44 > difftime(attr(ival, "start") + as.numeric(ival), attr(ival, "start"), 'days') Time difference of 293.6479 days 

(I also added this as a request for the function https://github.com/hadley/lubridate/issues/105 , assuming there is no better way - but maybe someone here knows one thing.)

Update - apparently, the difftime function also does not cope. Here is an example.

 > (d1 <- as.POSIXct("2011-03-12 12:00:00", 'America/Chicago')) [1] "2011-03-12 12:00:00 CST" > (d2 <- d1 + days(1)) # Gives desired result [1] "2011-03-13 12:00:00 CDT" > (i2 <- d2 - d1) [1] 2011-03-12 12:00:00 -- 2011-03-13 12:00:00 > difftime(attr(i2, "start") + as.numeric(i2), attr(i2, "start"), 'days') Time difference of 23 hours 

As I mentioned below, I believe that one of the good ways to deal with this would be to implement the /.interval function, which at first did not contribute to period .

+6
source share
2 answers

The as.duration function is what lubridate provides. The interval class is represented internally as the number of seconds from the beginning, so if you need the number of hours, you can simply divide as.numeric(ival) by 3600 or (3600 * 24) for several days.

If you need processed examples of functions applied to your object, you must provide the output of dput(ival ). I tested objects created on the help(duration) page where ?interval sent me.

  date <- as.POSIXct("2009-03-08 01:59:59") # DST boundary date2 <- as.POSIXct("2000-02-29 12:00:00") span <- date2 - date #creates interval span #[1] 2000-02-29 12:00:00 -- 2009-03-08 01:59:59 str(span) #Classes 'interval', 'numeric' atomic [1:1] 2.85e+08 # ..- attr(*, "start")= POSIXct[1:1], format: "2000-02-29 12:00:00" as.duration(span) #[1] 284651999s (9.02y) as.numeric(span)/(3600*24) #[1] 3294.583 # A check against the messy method: difftime(attr(span, "start") + as.numeric(span), attr(span, "start"), 'days') # Time difference of 3294.583 days 
+10
source

Ken, Day Division (1) will give you what you want. Lubridate does not force periods to duration when you divide intervals by periods. (Although the algorithm for finding the exact number of integer periods in an interval starts with an estimate that uses the interval divided by the analog number of durations, which may be what you notice).

The end result is the number of whole periods that correspond to the interval. A warning message warns the user that this is an estimate because there will be some part of the period that is discarded from the response. It is unreasonable to do math with a fraction of the period, since we cannot change the clock with it unless we convert it in the shortest possible time, but there is no consistent way to do the conversion. For example, the day you mention it will be equal to 23 hours, but on other days it will be equal to 24 hours. You think of the right way β€” periods are an attempt to respect the variations caused by DST, leap years, etc., but they only do this in whole units.

I cannot reproduce the subtraction error mentioned above. It seems to work for me.

  three <- force_tz(ymd_hms("2011-03-12 12:00:00"), "") # note: here in TX, "" *is* CST (four <- three + days(1)) > [1] "2011-03-13 12:00:00 CDT" four - days(1) > [1] "2011-03-12 12:00:00 CST" 
+3
source

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


All Articles