Q:

Converting a number into time (0,5 of an hour = 00:30:00)

I am trying to convert a number into time format.
For example: 
 I calculate how long has to be charged an electric car at the charging station of 11 kWh.  

Energy demand - 2,8 kWh  
Charging time = 2,8 kWh/11 kWh = 0,257 h  
0,257 h = 15 min 25 sec. = 00:15:25

How can I convert 0,257 h into 00:15:25 in R?

A:

Based on the example, we will assume that the input is less than 24 (but if that is not the case these could be modified to handle that depending on the definition of what such an input should produce).
1) chron::times Use chron times like this.  times measures times in fractions of a day so divide the hours (.257) by 24 to give the fraction of a day that it represents.
library(chron)

times(.257 / 24)
## [1] 00:15:25

This gives a chron "times" class object.  If x is such an object use format(x) to convert it to a character string, if desired.
2) POSIXct This uses no packages although it is longer.  It returns the time as a character string.  POSIXct measures time in seconds and so multiply the hours (.257) by 3600 as there are 3600 seconds in an hour.
format(as.POSIXct("1970-01-01") + 3600 * .257, "%H:%M:%S")
## [1] "00:15:25"

2a) This variation would also work. It is longer but it involves no conversion factors.  It returns a character string.
format(as.POSIXct("1970-01-01") + as.difftime(.257, units = "hours"), "%H:%M:%S")
## [1] "00:15:25"

Updates: Added (2).  Also added (2a) and improved (2).

A:

The answer by @GGrothendieck seems to be the way to go here.  But if you had to do this in base R, you could just compute the hour, minute, and second components and build the time string manually:
x <- 2.257                               # number of hours
total <- round(x*60*60, digits=0)        # the total number of seconds
hours <- trunc(total / (60*60))
minutes <- trunc((x - hours) * 60)
seconds <- total %% 60

ts <- paste0(formatC(hours, width=2, flag="0"), ":",
             formatC(minutes, width=2, flag="0"), ":",
             formatC(seconds, width=2, flag="0"))
ts

[1] "02:15:25"

Demo

A:

The tidyverse solution would use the hms package:

hms::hms(0.257 * 60^2)  
#> 00:15:25.2

Gives you an object of classes hms and difftime. If you want a string:

format(hms::hms(0.257 * 60^2))
#> [1] "00:15:25.2"

