R间隔,持续时间和期间
示例
间隔是在润滑物中记录时间跨度的最简单方法。间隔是两个特定时刻之间发生的时间跨度。
# create interval by substracting two instants today_start <- ymd_hms("2016-07-22 12-00-00", tz="IST") today_start ## [1] "2016-07-22 12:00:00 IST" today_end <- ymd_hms("2016-07-22 23-59-59", tz="IST") today_end ## [1] "2016-07-22 23:59:59 IST" span <- today_end - today_start span ## Time difference of 11.99972 hours as.interval(span, today_start) ## [1] 2016-07-22 12:00:00 IST--2016-07-22 23:59:59 IST # create interval using interval() function span <- interval(today_start, today_end) [1] 2016-07-22 12:00:00 IST--2016-07-22 23:59:59 IST
持续时间可衡量两个瞬间之间发生的确切时间。
duration(60, "seconds") ## [1] "60s" duration(2, "minutes") ## [1] "120s (~2 minutes)"
注意:大于周的单位由于可变性而不会使用。
持续时间可以通过创建dseconds,dminutes以及持续时间等辅助功能。
运行?quick_durations完整列表。
dseconds(60) ## [1] "60s" dhours(2) ## [1] "7200s (~2 hours)" dyears(1) ## [1] "31536000s (~365 days)"
可以减去持续时间并将其添加到瞬间以获得新的瞬间。
today_start + dhours(5) ## [1] "2016-07-22 17:00:00 IST" today_start + dhours(5) + dminutes(30) + dseconds(15) ## [1] "2016-07-22 17:30:15 IST"
可以从间隔创建持续时间。
as.duration(span) [1] "43199s (~12 hours)"
周期测量两个时刻之间发生的时钟时间变化。
期间可以使用创建period功能以及其他辅助功能,如seconds,hours等要得到的时期辅助功能的完整列表,运行?quick_periods。
period(1, "hour") ## [1] "1H 0M 0S" hours(1) ## [1] "1H 0M 0S" period(6, "months") ## [1] "6m 0d 0H 0M 0S" months(6) ## [1] "6m 0d 0H 0M 0S" years(1) ## [1] "1y 0m 0d 0H 0M 0S"
is.period函数可用于检查对象是否为句点。
is.period(years(1)) ## [1] TRUE is.period(dyears(1)) ## [1] FALSE