[R] Milisecond problem in as.POSIXct ?

Joshua Ulrich josh.m.ulrich at gmail.com
Mon Jun 15 03:41:10 CEST 2015


On Wed, Jun 10, 2015 at 8:05 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>
> This is known behavior with how POSIXt objects are printed.  See the discussion on StackOverflow: http://stackoverflow.com/questions/7726034/how-r-formats-posixct-with-fractional-seconds
>
To summarize the relevant portion of the discussion on StackOverflow:

I believe the behavior is due to truncating the fractional seconds
instead of rounding, combined with the floating point representation
of the POSIXct time (though I would need to take a closer look at
do_formatPOSIXlt to verify).  If you look at the underlying double
value of the POSIXct object, you can see why the printed value in the
first example below is 0.2s and the printed value for the second
example is 0.4s.

R> sprintf("%20.10f", as.POSIXct('2011-10-11 07:49:36.3', tz="UTC"))
[1] "1318319376.2999999523"
R> sprintf("%20.10f", as.POSIXct('2011-10-11 07:49:36.4', tz="UTC"))
[1] "1318319376.4000000954"

> On Wed, Jun 10, 2015 at 7:41 PM, ce <zadig_1 at excite.com> wrote:
>>
>> Dear all,
>>
>> my main problem is with miliseconds. I have an array :
>>
>> library(xts)
>> options(digits.secs = 3)
>> > x
>> [1] "2015-06-10 10:22:06.389 EDT" "2015-06-10 10:22:07.473 EDT"
>> [3] "2015-06-10 10:22:08.717 EDT" "2015-06-10 10:22:09.475 EDT"
>>
>> > x[1]
>> [1] "2015-06-10 10:22:06.38 EDT"
>> > x[2]
>> [1] "2015-06-10 10:22:07.473 EDT"
>>
>> why it cuts last digit of miliseconds 389 to 38 ? ( it doesn't cut 473 !! )
>>
>> I try to dump it to post here:
>>
>> > dump("x",file=stdout())
>>
>> x <-
>> structure(c(1433946126.39, 1433946127.474, 1433946128.717, 1433946129.476
>> ), tzone = "", tclass = c("POSIXct", "POSIXt"), class = c("POSIXct",
>> "POSIXt"))
>>
>> new array becomes :
>>
>> > x
>> [1] "2015-06-10 10:22:06.390 EDT" "2015-06-10 10:22:07.473 EDT"
>> [3] "2015-06-10 10:22:08.717 EDT" "2015-06-10 10:22:09.476 EDT"
>>
>> this time first milisecond 389 became 390 ?  and last element 475 became 476 ?
>>
>> I do some more tests :
>>
>> as.POSIXct("2015-06-10 10:22:07.473",format='%Y-%m-%d %H:%M:%OS')
>> [1] "2015-06-10 10:22:07.473 EDT"
>>
>> is correct, but :
>>
>> as.POSIXct("2015-06-10 10:22:06.389",format='%Y-%m-%d %H:%M:%OS')
>> [1] "2015-06-10 10:22:06.388 EDT"
>>
>> why miliseconds turn to 388 instead of 389 ?
>>
>> or
>>
>>  as.POSIXct("2015-06-10 10:22:07.478",format='%Y-%m-%d %H:%M:%OS')
>> [1] "2015-06-10 10:22:07.477 EDT"
>>
>>  why it shows 477 instead of 478
>>
>> > sessionInfo()
>> R version 3.2.0 (2015-04-16)
>> Platform: x86_64-suse-linux-gnu (64-bit)
>> Running under: openSUSE 13.2 (Harlequin) (x86_64)
>>
>> locale:
>>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
>>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>> [1] xts_0.9-7  zoo_1.7-12
>>
>> loaded via a namespace (and not attached):
>> [1] tools_3.2.0     grid_3.2.0      lattice_0.20-31
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

>
> --
> Joshua Ulrich  |  about.me/joshuaulrich
> FOSS Trading  |  www.fosstrading.com


-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-help mailing list