From murdoch@dunc@n @ending from gm@il@com  Fri Jun 22 16:04:50 2018
From: murdoch@dunc@n @ending from gm@il@com (Duncan Murdoch)
Date: Fri, 22 Jun 2018 10:04:50 -0400
Subject: [Rd] Bug in as.Date or strptime?
In-Reply-To: <ed49bf06-77ff-3549-567a-5d2352cfbc16@sapo.pt>
References: <ed49bf06-77ff-3549-567a-5d2352cfbc16@sapo.pt>
Message-ID: <df383438-1b9f-42ed-fe59-08985a9ec285@gmail.com>

On 22/06/2018 9:55 AM, Rui Barradas wrote:
> Hello,
> 
> This just came up in SO, sessionInfo() at the end.
> 
> https://stackoverflow.com/questions/50988018/seeking-explanation-for-as-date-function-in-r?noredirect=1#comment88971055_50988018
> 
> 
> 
> # example 1
> # not even the month is right
> as.Date(x =  1, format = '%j', origin= '2015-01-01')
> #[1] "2018-07-21"

Since x is numeric, it is added to the origin date.  But the origin date 
is a character, so it is converted to a date using format.  The %j 
format says "day of year"; since you didn't give a year, that is assumed 
to be the current year, 2018.  %j only uses the 1st 3 digits that it 
finds, so the origin is taken to be day 201 of 2018.  Add 1, you get 
July 21.

> 
> # example 2a
> # nonsense output
> as.Date(x = 1, origin= '2015-01-01')
> #[1] "2015-01-02"

Since no format is given, the origin is found using the default 
conversion, which gives what you'd expect.  Then we add one day.

> 
> # example 2a
> # nonsense output, see example 6 below
> as.Date(x = 1, origin = as.Date('2015-01-01'))
> #[1] "2015-01-02"

Same as above.  Same result, hurray!
> 
> 
> 
> # example 3
> # I know that the method as.Date.numeric doesn't have
> # argument 'format' but does have the dots argument.
> # The format is passed on to strptime so maybe the problem is there.
> as.Date(x =  1, format = '%j', origin= as.Date('2015-01-01'))
> #[1] "2015-01-02"

There's no problem here.  The format is ignored for numeric x and date 
object origin.  This is the same as 2a.

> 
> 
> # example 4
> # Wrong, documented.
> # origin should be automatically coerced to class 'Date'
> # This is documented to behave like example 6 below
> as.Date(x = '1',format = '%j', origin= '2015-01-01')
> #[1] "2018-01-01"

origin is ignored for character x.  That is day 1 of the default year.

> 
> 
> # example 5
> # right, documented. x of class 'character' needs argument 'format'
> as.Date(x = '1', origin= '2015-01-01')
> #Error in charToDate(x) :
> #  string de caracteres n?o ? um formato padr?o n?o amb?guo

There's no default conversion for '1'.

> 
> 
> # example 6
> # the safe way, the only one that outputs the right date
> as.Date(x =  '1', format = '%j', origin= as.Date('2015-01-01'))
> #[1] "2018-01-01"

origin is ignored again, so this is the same as number 4.


Duncan Murdoch

> 
> 
> sessionInfo()
> R version 3.4.4 (2018-03-15)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 16.04.4 LTS
> 
> Matrix products: default
> BLAS: /usr/lib/libblas/libblas.so.3.6.0
> LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
> 
> locale:
>    [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
>    [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
>    [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
>    [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
>    [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods
> [7] base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.4.4 tools_3.4.4    yaml_2.1.19
> 
> 
> Or maybe I am missing something.
> 
> Thanks in advance,
> 
> Rui Barradas
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] Bug in as.Date or strptime?
Duncan Murdoch