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] "00:49:39 val: 2979"  "21:55:47 val: 78947" "00:21:45 val: 1305" 
#> [4] "23:40:48 val: 85248" "05:11:42 val: 18702"
pl$PTime(runif(5) * 3600 * 24 * 1E3, tu = "ms")
#> PTime [ double ]: number of epochs [ ms ] since midnight
#> [1] "15:10:55:354ms val: 54655354" "19:52:47:017ms val: 71567017"
#> [3] "12:52:05:569ms val: 46325569" "19:29:15:740ms val: 70155740"
#> [5] "00:59:51:422ms val: 3591422"
pl$PTime(runif(5) * 3600 * 24 * 1E6, tu = "us")
#> PTime [ double ]: number of epochs [ us ] since midnight
#> [1] "11:42:35:152_539us val: 42155152539" "02:29:06:975_953us val: 8946975953" 
#> [3] "01:46:17:154_506us val: 6377154506"  "00:08:12:106_886us val: 492106886"  
#> [5] "14:29:55:936_922us val: 52195936922"
pl$PTime(runif(5) * 3600 * 24 * 1E9, tu = "ns")
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "00:32:58:550_966_084ns val: 1978550966084" 
#> [2] "11:31:58:893_246_352ns val: 41518893246352"
#> [3] "20:25:26:056_592_166ns val: 73526056592166"
#> [4] "19:18:10:651_061_385ns val: 69490651061385"
#> [5] "20:24:23:965_559_005ns val: 73463965559005"
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]
#> [
#>  17:54:44
#>  09:18:04
#>  19:58:37
#>  10:46:24
#>  04:33:20
#> ]
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"