[Rd] Wishlist: Function 'difftime' to honor 'tzone' attribute (PR#14182)

From: <suharto_anggono_at_yahoo.com>
Date: Tue, 12 Jan 2010 06:35:12 +0100 (CET)

Full_Name: Suharto Anggono
Version: 2.8.1
OS: Windows
Submission from: (NULL) (

PR#14076 inspired me to write this.

> t1 <- as.POSIXct("1970-01-01 00:00:00", tz="GMT")
> t2 <- as.POSIXlt("1970-01-01 00:00:00", tz="GMT")
> t1 - t2

Time difference of 7 hours

Above, t1 and t2 represent the same time in the same specified timezone. Therefore, it is reasonable if their difference is zero.

The result is like above because the function '-.POSIXt' calls the function 'difftime', and function 'difftime' does not honor 'tzone' attribute, as can be seen from this part of the function definition.

function (time1, time2, tz = "", units = c("auto", "secs", "mins",

    "hours", "days", "weeks"))

    time1 <- as.POSIXct(time1, tz = tz)
    time2 <- as.POSIXct(time2, tz = tz)

Function 'difftime' calls 'as.POSIXct' with the value of 'tz' argument. If 'tz' argument is not supplied, "" (current timezone) is used.

I wish function 'difftime' could honor 'tzone' attribute. The above part of the function definition can be changed to be like this.

function (time1, time2, tz, units = c("auto", "secs", "mins",

    "hours", "days", "weeks"))

    if (missing(tz)) {

        time1 <- as.POSIXct(time1)
        time2 <- as.POSIXct(time2)

    else {
        time1 <- as.POSIXct(time1, tz = tz)
        time2 <- as.POSIXct(time2, tz = tz)

I see that, if 'tz' argument is not supplied, function 'as.POSIXct.POSIXlt' uses the value of 'tzone' attribute if present.

> version

platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
major          2
minor          8.1
year           2008
month          12
day            22
svn rev        47281
language       R

version.string R version 2.8.1 (2008-12-22)
> sessionInfo()

R version 2.8.1 (2008-12-22)

LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MON
ETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base
> Sys.timezone()

[1] "ICT"

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 12 Jan 2010 - 05:40:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Jan 2010 - 05:40:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive