Converts an object representing a date (and if applicable a time) into an object of class POSIXct. Supported input classes are character, Date and POSIXt.
hsToPosix(datetime, keepTZ = is.null(tzone), tzone = NULL, lt = FALSE, ...)
datetime | object of class POSIXt or Date or character representing date (and time) information to be converted to class POSIXct. |
---|---|
keepTZ | if |
tzone | time zone. Will be set to “UTC” if missing. UTC it the preferred time zone as it seems that only UTC prevents the POSIXt-classes from applying daylight-savings time. |
lt | if TRUE a POSIXlt object is returned instead of a POSIXct object. |
... | further arguments to be passed to as.POSIXct/as.POSIXlt, e.g. format, help for as.POSIXct/as.POSIXlt. |
If datetime is already of class POSIXlt or POSIXct the time zone is preserved unless keepTZ is FALSE. If datetime is a character string it is expected to be in ISO format: “yyyy-mm-dd [HH:MM:SS]” where the time-part in brackets is optional.
# Start with a string representing a timestamp datetime <- "2011-01-02 12:34:56" # By default hsToPosix creates a POSIXct object: ct <- hsToPosix(datetime) class(ct) # "POSIXct" "POSIXt"#> [1] "POSIXct" "POSIXt"# You may decide to create a POSIXlt object instead: lt <- hsToPosix(datetime, lt = TRUE) class(lt) # "POSIXlt" "POSIXt"#> [1] "POSIXlt" "POSIXt"# With a POSIXlt object you can access the different parts of the timestamp sprintf("%d hours, %d minutes, %d seconds", lt$hour, lt$min, lt$sec)#> [1] "12 hours, 34 minutes, 56 seconds"# These are all available pieces of information # (isdst = is daylight savings time in effect) sapply(attr(lt, "names"), function(name) try(lt[[name]]))#> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds #> Error in FUN(X[[i]], ...) : subscript out of bounds#> sec #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> min #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> hour #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> mday #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> mon #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> year #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> wday #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> yday #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n" #> isdst #> "Error in FUN(X[[i]], ...) : subscript out of bounds\n"#> [1] TRUE#> [1] TRUE# The following time does not exist in CET/CEST but in UTC # as it is the time when daylight-savings time switched. hsToPosix("2011-03-27 02:00:00") # "2011-03-27 02:00:00 UTC"#> [1] "2011-03-27 02:00:00 UTC"# Compare with as.POSIXct: between 02:00:00 and 02:59:59 the # time information gets lost and is only recognized again # from 03:00:00 on. Similar results with as.POSIXlt. as.POSIXlt("2011-03-27 01:59:59") # "2011-03-27 01:59:59"#> [1] "2011-03-27 01:59:59 UTC"#> [1] "2011-03-27 02:00:00 UTC"#> [1] "2011-03-27 02:59:59 UTC"#> [1] "2011-03-27 03:00:00 UTC"# When loading data from an Access table it will be of class # POSIXct: #dat <- hsGetTable(xmdb(), "tbl_Hyd") #class(dat$Zeitst) # "POSIXct" "POSIXt" # In order to prevent R from considering daylight savings time # we should convert to UTC time zone. But then we have to keep # in mind that the indication "UTC" is not correct as the time # stamps in fact represent the time zone "UTC+1"! #head(dat$Zeitst) # "2011-08-23 00:00:00 CEST" "2011-08-23 00:01:00 CEST" ... #head(hsToPosix(dat$Zeitst)) # "2011-08-23 00:00:00 UTC" "2011-08-23 00:01:00 UTC" ...