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("polars0")


# make PTime in all time units
pl$PTime(runif(5) * 3600 * 24 * 1E0, tu = "s")
#> PTime [ double ]: number of epochs [ s ] since midnight
#> [1] "22:57:03 val: 82623" "08:28:29 val: 30509" "22:27:40 val: 80860"
#> [4] "17:26:00 val: 62760" "21:47:26 val: 78446"
pl$PTime(runif(5) * 3600 * 24 * 1E3, tu = "ms")
#> PTime [ double ]: number of epochs [ ms ] since midnight
#> [1] "06:19:35:197ms val: 22775197" "19:02:34:086ms val: 68554086"
#> [3] "01:02:01:827ms val: 3721827"  "03:39:12:636ms val: 13152636"
#> [5] "01:53:53:003ms val: 6833003"
pl$PTime(runif(5) * 3600 * 24 * 1E6, tu = "us")
#> PTime [ double ]: number of epochs [ us ] since midnight
#> [1] "09:50:44:600_886us val: 35444600886" "12:27:28:414_716us val: 44848414716"
#> [3] "22:01:52:727_116us val: 79312727116" "23:00:29:262_699us val: 82829262699"
#> [5] "22:42:07:468_031us val: 81727468031"
pl$PTime(runif(5) * 3600 * 24 * 1E9, tu = "ns")
#> PTime [ double ]: number of epochs [ ns ] since midnight
#> [1] "17:23:06:602_953_076ns val: 62586602953076"
#> [2] "16:46:22:094_652_950ns val: 60382094652950"
#> [3] "15:26:17:509_488_165ns val: 55577509488165"
#> [4] "18:42:50:980_884_879ns val: 67370980884879"
#> [5] "19:24:10:115_235_149ns val: 69850115235149"
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]
#> [
#>  00:02:12
#>  08:39:51
#>  04:50:29
#>  11:18:48
#>  18:53:22
#> ]
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"