[R] Bug in as.POSIXct regarding AM/PM

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Oct 8 15:50:10 CEST 2010


On Fri, 8 Oct 2010, David Winsemius wrote:

>
> On Oct 8, 2010, at 5:54 AM, Ted Zeng (曾振兴) wrote:
>
>> 
>> Dear All,
>> 
>> I encounted in a problem with as.POSIXct() function.
>> 
>>> as.POSIXct("2009/03/26 01:00:00 AM" , format="%Y/%m/%d %I:%M:%S %p")
>> [1] NA
>>> as.POSIXct("2009/03/26 02:00:00 PM" , format="%Y/%m/%d %I:%M:%S %p")
>> [1] NA
>> 
>> I have tried this in the R version 2.11.1,  R version 2.10.1  and R version
>> 2.9.2 . They all does not work. The operation system is win7.

At least one did work, as you showed output.

> Cannot reproduce on R 2.11.1 Mac OSX 10/5/8
> R version 2.11.1 Patched (2010-06-14 r52281)
>> as.POSIXct("2009/03/26 01:00:00 AM" , format="%Y/%m/%d %I:%M:%S %p")
> [1] "2009-03-26 01:00:00 EDT"
>> as.POSIXct("2009/03/26 02:00:00 PM" , format="%Y/%m/%d %I:%M:%S %p")
> [1] "2009-03-26 14:00:00 EDT"
>
> Is this a "daylight savings" time switch in your locale? Those switches 
> generally occur at night (suggested by your 1AM NA and not by the 2PM value) 
> but my experience is limited to the US so I don't know all possibilities.

As far as I am aware all recent DST transitions are on a Sunday in all 
the timezones I have had cause to look at (quite a few over the 
years).

> You have not included the at a minimum information requested in the Posting 
> Guide:
>
> ?sessionInfo
>
> This seems likely due to your setup or possibly some sort of encoding issue 
> that might be easier to reproduce with the results of sessionInfo.

Most likely AM/PM is not supported in the unstated locale.  See 
?strptime, which does say

      ‘%p’ AM/PM indicator in the locale.  Used in conjunction with ‘%I’
           and *not* with ‘%H’.  An empty string in some locales.
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



>
>
> -- 
> David Winsemius, MD
> West Hartford, CT
>
>> sessionInfo()
> R version 2.11.1 Patched (2010-06-14 r52281)
> x86_64-apple-darwin9.8.0
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

-- 
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-help mailing list