Convert integer to ITime?

I am learning new date and time formats data.table. Since it is ITimestored as an integer under the hood (seconds since midnight):

x <- as.ITime( "10:25:00" )
i <- as.integer( x )
i
[1] 37500

It seems like it should be easy enough to convert an integer (provided that it is less than 24 * 60 * 60) to ITime, but I do not find how:

as.ITime( i )
Error in as.POSIXlt.numeric(x, ...) : 'origin' must be supplied

as( i, "ITime" )
Error in as(i, "ITime") : 
  no method or default for coercing "integer" to "ITime"

Providing the source (which I collect is passed in as.POSIX...) really helps, but even then it only gives the correct value (for my locale), if you also specify tzas "UTC":

as.ITime( i, origin = "1970-01-01 00:00:00" )
[1] "20:25:00"

as.ITime( i, origin = "1970-01-01 00:00:00", tz = "UTC" )
[1] "10:25:00"

# note that the date seems necessary but irrelevant here
as.ITime( i, origin = "2000-01-01 00:00:00", tz = "UTC" )
[1] "10:25:00"

What I'm talking about is similar to what I sometimes use with chron::times:

x <- chron::times( "10:25:00" )

n <- as.numeric( x )
n
[1] 0.4340278

chron::times( n )
[1] 10:25:00

Is a similar method (other than using integers instead of numbers / doubles / floats) available with the class ITime?

. "" IDate, Date .., , origin="1970-01-01" "" ( , "" ).

+4
1

:

library(data.table)
DT = data.table(int = 37500L)
DT[, setattr(int, "class", "ITime")]

#         int
# 1: 10:25:00

int = 37500L
setattr(int, "class", "ITime")

# "10:25:00"
+5

Source: https://habr.com/ru/post/1676113/


All Articles