hsToPosix: Conversion to POSIXt

Description Usage Arguments Details Examples

View source: R/convert.R

Description

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.

Usage

1
hsToPosix(datetime, keepTZ = is.null(tzone), tzone = NULL, lt = FALSE, ...)

Arguments

datetime

object of class POSIXt or Date or character representing date (and time) information to be converted to class POSIXct.

keepTZ

if TRUE and if the given object is already of POSIX-type, the returned POSIXct object will be in the same time zone as the original object. Otherwise POSIX-objects will be returned in the time zone tzone.

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.

Details

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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"
  
# You may decide to create a POSIXlt object instead:
lt <- hsToPosix(datetime, lt = TRUE)
class(lt) # "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)

# These are all available pieces of information 
# (isdst = is daylight savings time in effect)
sapply(attr(lt, "names"), function(name) try(lt[[name]]))
  
# You may use hsToPosix to convert between lt and ct
identical(hsToPosix(ct, lt = TRUE), lt)
identical(hsToPosix(lt, lt = FALSE), ct)
  
# 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"
  
# 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"
as.POSIXlt("2011-03-27 02:00:00") # "2011-03-27"
as.POSIXlt("2011-03-27 02:59:59") # "2011-03-27"
as.POSIXlt("2011-03-27 03:00:00") # "2011-03-27 03:00:00"  
  
# 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" ...

KWB-R/kwb.datetime documentation built on July 9, 2021, 10:28 p.m.