Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display time inside a clock time after performing mathematical operations

I have created a time sequence from 00:00:00 hrs to 23:00:00 hrs. Adding any number of hours is easy until the sum is less than or equal to 23:00:00 hrs. After the sum crosses 23:00:00 it start displaying the time in number of days which is quite intuitive.But I want the output inside a clock time irrespective of whether I do subtraction or addition

Suppose I want to do addition like below

library(chron)
times("23:00:00")+ times("01:00:00")
Time in days:
[1] 1 

My desired output is below one. Instead of getting days I want

00:00:00 

I also tried subtraction

times("00:00:00")- times("01:00:00")
[1] -0.04166667

Desired output

 "23:00:00"

I also tried it with POSIXct but it gives various error at various instances

 as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC")
    [1] "2017-02-07 UTC" #Not printing time. Only dates 

Subtraction using POSIXct

as.POSIXct("00:00:00", format = "%H:%M:%S", tz = "UTC") -as.POSIXct("01:00:00", format = "%H:%M:%S", tz = "UTC")
        Time difference of -1 hours
        Warning message:
        In 1:0:0 : numerical expression has 2 elements: only the first used

Addition using POSIXct

as.POSIXct("23:00:00", format = "%H:%M:%S", tz = "UTC") + as.POSIXct("01:00:00", format = "%H:%M:%S", tz = "UTC")
Error in `+.POSIXt`(as.POSIXct("23:00:00", format = "%H:%M:%S", tz = "UTC"),  : 
  binary '+' is not defined for "POSIXt" objects

Please help me to crack this problem. Also please help me to print the time along with date in POSIXct shown above Please let me know if anything doesn't make sense to you instead of down vote my question.

like image 313
learner Avatar asked Feb 07 '17 13:02

learner


1 Answers

Note that times of chron package internally represents time as a numeric value (decimal days). What you are trying to do is retain the values after the decimal and remove the integer before it. This can be achieved by taking modulus by 1 (try 2.5 %% 1). So, you can just take modulus by times(1) and that will get rid of the day.

library(chron)
(times("23:00:00")+ times("01:00:00")) %% times(1)
#[1] 00:00:00
> (times("00:00:00") - times("01:00:00")) %% times(1)
#[1] 23:00:00

Using POSIXct

I would not prefer to use POSIXct for this since POSIXct complicates things due to dates and timezones. Internally, POSIXct are numeric values of seconds from some origin (usually 1970-01-01). So, if you want to add a time interval to a POSIXct object, just add that time interval in seconds.

To add 1 hour to "23:00:00", you can do

format(as.POSIXct(
       as.numeric(as.POSIXct("23:00:00", format = "%H:%M:%S", tz = "UTC")) + 3600,
       origin = "1970-01-01", tz = "UTC"), "%H:%M:%S")
#[1] "00:00:00"

Subtraction can work in the same way

format(as.POSIXct(
       as.numeric(as.POSIXct("24:00:00", format = "%H:%M:%S", tz = "UTC")) - 3600,
       origin = "1970-01-01", tz = "UTC"), "%H:%M:%S")
#[1] "23:00:00"
like image 149
d.b Avatar answered Nov 09 '22 09:11

d.b