[Rd] POSIXct Formating Error (PR#9819)

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue Jul 31 10:35:52 CEST 2007


No bug, as intended.  If you want to see fractional seconds then you need 
to use a non-default format.  They are not (just) numbers, and fractional 
seconds are intended only for use intentionally.

There is a round() method for POSIXct objects:

> a
[1] "2007-07-27 16:11:03 GMT Daylight Time"
[2] "2007-07-27 16:11:03 GMT Daylight Time"
> format(a, "%Y-%m-%d %H:%M:%OS5")
[1] "2007-07-27 16:11:03.00000" "2007-07-27 16:11:03.99999"
> round(a)
[1] "2007-07-27 16:11:03 GMT Daylight Time"
[2] "2007-07-27 16:11:04 GMT Daylight Time"

which may do what you want.


On Tue, 31 Jul 2007, josh.quigley at tibra.com.au wrote:

> To Whom It May Concern:
>
>
>
> The following appears to be a bug in the way POSIXct dates are formated.
>
> The example is forced, but occurs naturally when importing Excel type dates
>
> (where fractional part is fraction of a day) and small rounding errors
> result.
>
>
>
> As shown, looking at the POSIXct class, it looks as if both times are
> 16:11:03 (truncation)
>
> Looking at as.numeric.POSIXct, it looks as if the times should be 1 second
> different (rounding)
>
>
>
> Looking at more digits shows the problem, the character format is truncating
> the fractional part while
>
> the numeric format is rounding it.
>
>
>
> The workaround of rounding the POSIXct is non-trivial, as it is not possible
> (or at least I can't)
>
> manipulate the POSIXct number directly. Rather, you need to go to POSIXlt,
> round and then go back.
>
>
>
> ------------------------------------------------------------------ Example
> ----------
>
>
>
>> a <- as.POSIXct(c("2007-07-27 16:11:03.000002", "2007-07-27
> 16:11:03.999995"))
>
>> a
>
>
>
> [1] "2007-07-27 16:11:03 AUS Eastern Standard Time"
>
> [2] "2007-07-27 16:11:03 AUS Eastern Standard Time"
>
>
>
>> as.numeric(a)
>
>
>
> [1] 1185516663 1185516664
>
>
>
>> format(as.numeric(a), digits=20)
>
>
>
> [1] "1185516663.000002" "1185516663.999995"
>
>
>
>
>
> Yours truly,
>
>
>
> Josh Quigley.
>
>
>
> -- please do not edit the information below
> ---------------------------------------------
>
>
>
> Version:
>
> platform = i386-pc-mingw32
>
> arch = i386
>
> os = mingw32
>
> system = i386, mingw32
>
> status =
>
> major = 2
>
> minor = 5.0
>
> year = 2007
>
> month = 04
>
> day = 23
>
> svn rev = 41293
>
> language = R
>
> version.string = R version 2.5.0 (2007-04-23)
>
>
>
> Windows XP (build 2600) Service Pack 2.0
>
>
>
> Locale:
>
> LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETAR
> Y=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252
>
>
>
> Search Path:
>
> .GlobalEnv, package:zoo, package:chron, package:RDCOMClient, package:stats,
> package:graphics, package:grDevices, package:utils, package:datasets,
> package:methods, Autoloads, package:base
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list