Skip to content

Store Time in R

Source code

Description

Store Time in R

Usage

pl$PTime(x, tu = c("s", "ms", "us", "ns"), format = "%H:%M:%S")

Arguments

x an integer or double vector of n epochs since midnight OR a char vector of char times passed to as.POSIXct converted to seconds.
tu timeunit either "s","ms","us","ns"
format a format string passed to as.POSIXct format via …

Details

PTime should probably be replaced with package nanotime or similar.

base R is missing encoding of Time since midnight "s" "ms", "us" and "ns". The latter "ns" is the standard for the polars Time type.

Use PTime to convert R doubles and integers and use as input to polars functions which needs a time.

Loosely inspired by data.table::ITime which is i32 only. PTime must support polars native timeunit is nanoseconds. The R double(float64) can imitate a i64 ns with full precision within the full range of 24 hours.

PTime does not have a time zone and always prints the time as is no matter local machine time zone.

An essential difference between R and polars is R prints POSIXct/lt without a timezone in local time. Polars prints Datetime without a timezone label as is (GMT). For POSIXct/lt taged with a timexone(tzone) and Datetime with a timezone(tz) the behavior is the same conversion is intuitive.

It appears behavior of R timezones is subject to change a bit in R 4.3.0, see polars unit test test-expr_datetime.R/"pl$date_range Date lazy/eager".

Value

a PTime vector either double or integer, with class "PTime" and attribute "tu" being either "s","ms","us" or "ns"

Examples

library("polars")


# make PTime in all time units
pl$PTime(runif(5) * 3600 * 24 * 1E0, tu = "s")
#> PTime [ double ]: number of epochs [ s ] since midnight
#> [1] "07:03:16 val: 25396" "08:24:18 val: 30258" "16:33:52 val: 59632"
#> [4] "10:37:56 val: 38276" "20:38:46 val: 74326"
pl$PTime(runif(5) * 3600 * 24 * 1E3, tu = "ms")
#> PTime [ double ]: number of epochs [ ms ] since midnight
#> [1] "00:59:34:717ms val: 3574717"  "09:30:05:269ms val: 34205269"
#> [3] "03:16:53:965ms val: 11813965" "10:57:20:627ms val: 39440627"
#> [5] "01:36:36:598ms val: 5796598"
pl$PTime(runif(5) * 3600 * 24 * 1E6, tu = "us")
#> PTime [ double ]: number of epochs [ us ] since midnight
#> [1] "19:17:52:977_811us val: 69472977811" "17:25:12:061_998us val: 62712061998"
#> [3] "18:01:43:789_137us val: 64903789137" "19:27:16:454_377us val: 70036454377"
#> [5] "01:07:13:885_060us val: 4033885060"
pl$PTime(runif(5) * 3600 * 24 * 1E9, tu = "ns")
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "11:28:51:200_540_810ns val: 41331200540810"
#> [2] "20:02:49:610_258_191ns val: 72169610258191"
#> [3] "21:25:02:761_472_761ns val: 77102761472761"
#> [4] "07:28:06:303_453_147ns val: 26886303453147"
#> [5] "01:48:25:194_085_836ns val: 6505194085836"
pl$PTime("23:59:59")
#> PTime [ double ]: number of epochs [ s ] since midnight
#> [1] "23:59:59 val: 86399"
as_polars_series(pl$PTime(runif(5) * 3600 * 24 * 1E0, tu = "s"))
#> polars Series: shape: (5,)
#> Series: '' [time]
#> [
#>  10:57:59
#>  14:11:59
#>  02:41:34
#>  11:39:40
#>  18:06:53
#> ]
pl$lit(pl$PTime("23:59:59"))$to_series()
#> polars Series: shape: (1,)
#> Series: '' [time]
#> [
#>  23:59:59
#> ]
pl$lit(pl$PTime("23:59:59"))$to_r()
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "23:59:59:000_000_000ns val: 8.6399e+13"