rguides

Sys.time

Sys.time()

Description

Sys.time() returns the current system time as a POSIXct object. The returned value represents the number of seconds elapsed since the Unix epoch: 1970-01-01 00:00:00 UTC.

The function takes no arguments.

Value

A POSIXct object (with class c("POSIXct", "POSIXt")) containing a double value representing seconds since the epoch. The underlying storage is a numeric (double) vector.

Details

On Unix-like systems, the resolution is microsecond precision (limited by hardware). On Windows, the resolution is approximately 10 milliseconds.

Fractional seconds are stored internally but are hidden by default when printed. Use options(digits.secs = 6) to display them. The maximum useful value is 6 because POSIXct stores time with microsecond precision (10^-6 seconds).

The timezone is a display property of POSIXct objects, not a property of Sys.time() itself. Use Sys.setenv(TZ = ...) to change the timezone for the R session, or Sys.timezone() to query the system timezone.

Two consecutive calls to Sys.time() almost never return the same value.

Gotchas

Daylight Saving Time: Arithmetic on POSIXct objects treats days as exactly 86400 seconds. This means adding or subtracting days may not produce the expected wall-clock time during DST transitions. For example, adding 1 day near a DST spring-forward may produce a time that is off by an hour. Use the lubridate package for timezone-aware duration arithmetic.

Examples

# Basic usage
Sys.time()
# [1] "2026-03-23 20:23:45 UTC"

# Check the class
class(Sys.time())
# [1] "POSIXct" "POSIXt"

# See the underlying numeric value (seconds since epoch)
as.numeric(Sys.time())
# [1] 1742765023.456

# Show fractional seconds (output varies each time you run this)
options(digits.secs = 6)
Sys.time()
# [1] "2026-03-23 20:23:45.123456 UTC"  # actual fractional part varies

# POSIXct arithmetic: add 3 hours to the current time
now <- Sys.time()
now + 3600 * 3
# [1] "2026-03-23 23:23:45 UTC"

# Measure elapsed time (Sys.sleep pauses for the given seconds)
t1 <- Sys.time()
Sys.sleep(1)
t2 <- Sys.time()
t2 - t1
# Time difference of ~1 seconds

# Compare two calls (almost never equal)
t1 <- Sys.time()
t2 <- Sys.time()
identical(t1, t2)
# [1] FALSE

# Format the output
format(Sys.time(), "%Y-%m-%d %H:%M:%S")
# [1] "2026-03-23 20:23:45"

# Convert to a different timezone (set TZ first for reproducibility)
Sys.setenv(TZ = "UTC")
format(Sys.time(), "%H:%M:%S", tz = "America/New_York")
# [1] "16:23:45"

See Also

  • Sys.Date() — return the current date as a Date object (no time component)
  • date() — return the current date and time as a character string
  • as.POSIXct() — convert strings or numeric values to POSIXct objects