[Rd] POSIXlt getting sec element at 60 when converting from POSIXct

Sklyar, Oleg (London) osklyar at maninvestments.com
Tue Oct 14 18:12:25 CEST 2008


In some cases rounding problems lead to creation of logically incorrect
times in POSIXlt when converting from POSIXct.

## everything works fine for the following values:
x = c(1223972160.00000, 1223982660.00000, 1223994660.0000)
## adding 0s for the epoch
x = x + as.POSIXct("1970-01-01 00:00") -
as.numeric(as.POSIXct("1970-01-01 00:00"))
x
## [1] "2008-10-14 09:16:00 BST" "2008-10-14 12:11:00 BST"
## [3] "2008-10-14 15:31:00 BST"

Now, numbers that look the same but originate from some calculations
lead to the following weirdness in POSIXlt. Sorry you will not be able
to reproduce as it is impossible to reproduce the same rounding:

> as.character(as.numeric(t1))
[1] "1223972160.00000" "1223982660.00000" "1223994660.00000"
> class(t1)
[1] "POSIXt"  "POSIXct"
## here we get a second less due to rounding errors somewhere, not too
bad
> t1
[1] "2008-10-14 09:15:59 BST" "2008-10-14 12:10:59 BST"
[3] "2008-10-14 15:30:59 BST"
> t2 = as.POSIXlt(t1)
> t2  
[1] "2008-10-14 09:15:59 BST" "2008-10-14 12:10:59 BST"
[3] "2008-10-14 15:30:59 BST"

## however here we do get a real problem
> t2$sec
[1] 60 60 60
## and here as well
> format(t2, "%Y-%m-%d %H:%M:%OS3")
[1] "2008-10-14 09:15:60.000" "2008-10-14 12:10:60.000"
[3] "2008-10-14 15:30:60.000"
> format(t1, "%Y-%m-%d %H:%M:%OS3")
[1] "2008-10-14 09:15:60.000" "2008-10-14 12:10:60.000"
[3] "2008-10-14 15:30:60.000"

Everything is back to normal with applying round (we are anyway limited
to 15 digits):
> t3 = round(as.numeric(t1),5) + .origin
> t3
[1] "2008-10-14 09:16:00 BST" "2008-10-14 12:11:00 BST"
[3] "2008-10-14 15:31:00 BST"

> sessionInfo()
R version 2.9.0 Under development (unstable) (2008-09-30 r46585) 
x86_64-unknown-linux-gnu 

locale:
C

attached base packages:
[1] splines   stats     graphics  utils     datasets  grDevices methods

[8] base     

other attached packages:
[1] AHLFinance_0.1.54  AHLDBConn_0.2.3    ROracle_0.5-9      DBI_0.2-4

[5] RODBC_1.2-3        AHLNagLib_0.1.3    AHLCalendar_0.2.59
AHLBase_0.1.25    

loaded via a namespace (and not attached):
[1] tools_2.9.0

Dr Oleg Sklyar
Research Technologist
AHL / Man Investments Ltd
+44 (0)20 7144 3107
osklyar at maninvestments.co

**********************************************************************
The contents of this email are for the named addressees ...{{dropped:19}}



More information about the R-devel mailing list