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] "02:23:39 val: 8619"  "01:30:17 val: 5417"  "23:43:50 val: 85430"
#> [4] "08:57:10 val: 32230" "08:42:52 val: 31372"
pl$PTime(runif(5) * 3600 * 24 * 1E3, tu = "ms")
#> PTime [ double ]: number of epochs [ ms ] since midnight
#> [1] "06:00:53:422ms val: 21653422" "15:57:57:827ms val: 57477827"
#> [3] "11:32:29:104ms val: 41549104" "23:20:35:410ms val: 84035410"
#> [5] "00:30:09:447ms val: 1809447"
pl$PTime(runif(5) * 3600 * 24 * 1E6, tu = "us")
#> PTime [ double ]: number of epochs [ us ] since midnight
#> [1] "08:34:25:589_714us val: 30865589714" "17:19:43:269_938us val: 62383269938"
#> [3] "08:03:05:289_754us val: 28985289754" "08:18:19:597_368us val: 29899597368"
#> [5] "20:40:46:294_469us val: 74446294469"
pl$PTime(runif(5) * 3600 * 24 * 1E9, tu = "ns")
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "01:26:28:181_595_504ns val: 5188181595504" 
#> [2] "14:20:05:990_756_303ns val: 51605990756303"
#> [3] "11:21:56:038_680_821ns val: 40916038680821"
#> [4] "03:16:11:872_539_818ns val: 11771872539818"
#> [5] "22:33:16:592_938_900ns val: 81196592938900"
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]
#> [
#>  14:55:09
#>  21:41:20
#>  09:48:33
#>  19:45:35
#>  22:13:01
#> ]
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"