Coercing ISO 8601 standard timestamps to class Date
The timestamps are already in ISO 8601 standard format which is unambiguous. Therefore, no format specification is required when converting to class Date:
as.Date(recfive$timestamp)
#> [1] "2018-01-27" "2018-01-27" "2018-01-27" "2018-01-27" "2018-01-27"
Coercing ISO 8601 standard timestamps to class POSIXct (datetime)
When coercing to a datetime object (class POSIXct
), the timezone has to be specified. Although, the timezone is already indicated in the input data by the trailing letter Z
(as Zulu = UTC) this is ignored on input, unfortunately.
In my current locale, the local timezone is used which creates a POSIXct object where the timestamps denote different instances in time than specified (during Winter, CET is one hour ahead of UTC).
as.POSIXct(recfive$timestamp)
#> [1] "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET" "2018-01-27 CET"
So, the timezone has to be specified explicitely:
as.POSIXct(recfive$timestamp, tz = "UTC")
#> [1] "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC" "2018-01-27 UTC"
Note that as.POSIXct()
has returned a truncated datetime denoting the start of the day.
To get the full datetimes as specified, as.POSIXct()
can be called with a format specification:
as.POSIXct(recfive$timestamp, format = "%FT%TZ", tz = "UTC")
#> [1] "2018-01-27 09:01:49 UTC" "2018-01-27 00:04:08 UTC" "2018-01-27 09:04:22 UTC"
#> [4] "2018-01-27 09:04:28 UTC" "2018-01-27 00:07:38 UTC"
Alternatively, there is a number of packages which have specialized in simplified or fast coersion of datetime strings to class POSIXct which all return the same result:
lubridate::ymd_hms(recfive$timestamp)
anytime::anytime(recfive$timestamp, asUTC = TRUE)
anytime::utctime(recfive$timestamp, tz = "UTC")
fasttime::fastPOSIXct(recfive$timestamp, tz = "UTC")
Data
recfive <- data.table::fread(
"timestamp
2018-01-27T09:01:49Z
2018-01-27T00:04:08Z
2018-01-27T09:04:22Z
2018-01-27T09:04:28Z
2018-01-27T00:07:38Z")