Skip to content

Instantly share code, notes, and snippets.

@seakintruth
Created May 27, 2021 02:45
Show Gist options
  • Save seakintruth/10d386fb66e092dd4ef8de48d8c6bdcf to your computer and use it in GitHub Desktop.
Save seakintruth/10d386fb66e092dd4ef8de48d8c6bdcf to your computer and use it in GitHub Desktop.
Get a pretty display from two timestamps (diff of seconds)
get_pretty_timestamp_diff <- function(
start_timestamp,
end_timestamp,
seconds_decimal=2,
round_simple=TRUE
){
# Set defaults
.days_decimal <- 0
.days <- 0
.hours_decimal <- 0
.hours <- 0
.minutes_decimal <- 0
.minutes <- 0
.seconds_display <- 0
.seconds <- (end_timestamp-start_timestamp)
if (round_simple) {
.years <- as.integer(.seconds / (365.24*24*60*60))
if (.years > 0) {
.years <-round(.seconds / (365.24*24*60*60),1)
.seconds <- 0
} else {
.days <- as.integer((.seconds / (365.24*24*60*60)-.years)*365.24)
if (.days > 0 ) {
.days <-round((.seconds / (365.24*24*60*60)-.years)*365.24,1)
.seconds <- 0
} else {
.days_decimal <-(.seconds / (365.24*24*60*60)-.years)*365.24-.days
.hours <- as.integer(.days_decimal*24)
if (.hours > 0) {
.hours <- round(.days_decimal*24,1)
}else{
.hours_decimal <- .days_decimal*24 - .hours
.minutes <- as.integer(.hours_decimal*60)
.minutes_decimal <- .hours_decimal*60 - .minutes
if (.minutes > 0) {
.minutes <- round(.hours_decimal*60,1)
.hours_decimal <- 0
.seconds_display <- 0
} else {
.seconds_display <-1 # round(.seconds,seconds_decimal)
}
}
}
}
} else {
.years <- as.integer(.seconds / (365.24*24*60*60))
.days <- as.integer((.seconds / (365.24*24*60*60)-.years)*365.24)
.days_decimal <-(.seconds / (365.24*24*60*60)-.years)*365.24-.days
.hours <- as.integer(.days_decimal*24)
.hours_decimal <- .days_decimal*24 - .hours
.minutes <- as.integer(.hours_decimal*60)
.minutes_decimal <- .hours_decimal*60 - .minutes
.seconds_display <- round(.minutes_decimal*60,seconds_decimal)
}
.time_statement_list <- c(
ifelse(as.integer(.years),
ifelse((.years == 1)," year ",paste0(.years," years ")),
NA
),
ifelse(as.integer(.days),
ifelse((.days == 1)," day ",paste0(.days," days ")),
NA
),
ifelse(as.integer(.hours),
ifelse((.hours == 1)," hour ",paste0(.hours," hours ")),
NA
),
ifelse(as.integer(.minutes),
ifelse((.minutes == 1)," minute ",paste0(.minutes," minutes ")),
NA
),
ifelse(as.integer(.seconds_display),
ifelse(
(.seconds_display == 1),
" second ",
paste0(.seconds_display," seconds ")
),
NA
)
)
.time_statement_list <- na.omit(.time_statement_list)
ifelse(
(length(.time_statement_list) <= 1),
.time_statement_list[1],
paste0(
paste0(
.time_statement_list[1:(length(.time_statement_list)-1)],
collapse=""
),
"and ",
.time_statement_list[length(.time_statement_list)]
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment