Skip to contents

[Maturing]

extract_seconds() extracts the seconds from a time object.

Usage

extract_seconds(x)

# S3 method for class 'Duration'
extract_seconds(x)

# S3 method for class 'difftime'
extract_seconds(x)

# S3 method for class 'POSIXt'
extract_seconds(x)

# S3 method for class 'Interval'
extract_seconds(x)

Arguments

x

A numeric vector.

Value

A numeric vector.

Details

difftime Objects

difftime objects are first converted to hms, which represent time in seconds. That way there is no conflict with the units attribute.

Period Objects

Period objects are a special type of object developed by the lubridate team that represents "human units", ignoring possible timeline irregularities. This means that 1 day as a Period can have different time spans when considering timeline irregularities.

Since the time span of a Period object can fluctuate, extract_seconds() does not accept this type of object. You can transform it to a Duration object and still use the function, but be aware that this can produce errors.

POSIXt Objects

POSIXt objects are converted to hms, stripping away the date component and retaining only the time.

Interval Objects

For Interval objects the function extracts only the duration of the time span.

See also

Other utility functions: change_date(), fix_hms(), flat_posixt_date(), get_last_week(), round_time()

Examples

extract_seconds(lubridate::ddays(1))
#> [1] 86400
#> [1] 86400 # Expected

extract_seconds(lubridate::as.difftime(1, units = "hours"))
#> [1] 3600
#> [1] 3600 # Expected

extract_seconds(hms::as_hms("01:00:00"))
#> [1] 3600
#> [1] 3600 # Expected

extract_seconds(lubridate::as_datetime("2020-01-01 00:00:00"))
#> [1] 0
#> [1] 0 # Expected

extract_seconds(
  lubridate::interval(
    start = lubridate::as_datetime("2020-01-01 00:00:00"),
    end = lubridate::as_datetime("2021-01-01 00:00:00")
  )
)
#> [1] 31622400
#> [1] 31622400 # Expected