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] "03:40:28 val: 13228" "03:28:15 val: 12495" "09:05:30 val: 32730"
#> [4] "03:49:16 val: 13756" "08:45:48 val: 31548"
pl$PTime(runif(5) * 3600 * 24 * 1E3, tu = "ms")
#> PTime [ double ]: number of epochs [ ms ] since midnight
#> [1] "20:39:22:622ms val: 74362622" "09:59:41:734ms val: 35981734"
#> [3] "02:27:00:571ms val: 8820571"  "01:58:34:032ms val: 7114032" 
#> [5] "14:14:40:911ms val: 51280911"
pl$PTime(runif(5) * 3600 * 24 * 1E6, tu = "us")
#> PTime [ double ]: number of epochs [ us ] since midnight
#> [1] "03:42:05:682_780us val: 13325682780" "05:15:47:219_055us val: 18947219055"
#> [3] "18:59:51:565_230us val: 68391565230" "16:34:51:142_959us val: 59691142959"
#> [5] "17:03:26:253_994us val: 61406253994"
pl$PTime(runif(5) * 3600 * 24 * 1E9, tu = "ns")
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "06:35:41:798_199_713ns val: 23741798199713"
#> [2] "06:35:38:750_036_805ns val: 23738750036805"
#> [3] "23:22:01:409_321_576ns val: 84121409321576"
#> [4] "09:25:09:322_350_472ns val: 33909322350472"
#> [5] "17:53:08:339_026_272ns val: 64388339026272"
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]
#> [
#>  22:10:31
#>  08:20:25
#>  02:40:10
#>  17:02:20
#>  23:01:45
#> ]
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"