Quantcast

Negative years with strptime?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Negative years with strptime?

Winston Chang
Is there a way to make as.Date() and strptime() process strings with
negative years? It appears that Date objects can contain negative years and
you can convert them to strings, but you can't convert them back to Date
objects.


x <- as.Date(c("0001-01-24", "0500-01-24"))
as.character(x)
# "0001-01-24" "0500-02-02"
as.Date(as.character(x))
# "0001-01-24" "0500-01-24"

# Minus 391 days gives negative year
as.character(x - 391)
# "-001-12-30" "0498-12-29"

# Can't convert this string back to Date
as.Date(as.character(x - 391))
# Error during wrapup: character string is not in a standard unambiguous
format


# as.Date.character uses strptime, so we can try using strptime directly
strptime(as.character(x), "%Y-%m-%d")
# "0001-01-24" "0500-01-24"

strptime(as.character(x - 391), "%Y-%m-%d")
# NA           "0498-12-29"


Thanks,
-Winston

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Negative years with strptime?

Rui Barradas
Hello,

Is there a bug with negative dates? Just see:

seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
  [1] "0000-01-01" "000/-01-01" "000.-01-01" "000--01-01" "000,-01-01"
  [6] "000+-01-01" "000*-01-01" "000)-01-01" "000(-01-01" "000'-01-01"
[11] "00/0-01-01" "00//-01-01" "00/.-01-01" "00/--01-01" "00/,-01-01"
[16] "00/+-01-01" "00/*-01-01" "00/)-01-01" "00/(-01-01" "00/'-01-01"
[21] "00.0-01-01" "00./-01-01"

See the year number: "after" the zero, i.e., downward from zero, the
printed character is '/' which happens to be the ascii character before
'0', and before it's '.', etc. This sequence gives the nine ascii
characters before zero as last digit of the year, then the 10th is '0'
as (now) expected, then goes to the second digit, etc.

It seems to stop at the first 9, or else we would have some sort of real
problem.

Anyway, this doesn't look right.

Rui Barradas
Em 10-07-2012 22:17, Winston Chang escreveu:

> Is there a way to make as.Date() and strptime() process strings with
> negative years? It appears that Date objects can contain negative years and
> you can convert them to strings, but you can't convert them back to Date
> objects.
>
>
> x <- as.Date(c("0001-01-24", "0500-01-24"))
> as.character(x)
> # "0001-01-24" "0500-02-02"
> as.Date(as.character(x))
> # "0001-01-24" "0500-01-24"
>
> # Minus 391 days gives negative year
> as.character(x - 391)
> # "-001-12-30" "0498-12-29"
>
> # Can't convert this string back to Date
> as.Date(as.character(x - 391))
> # Error during wrapup: character string is not in a standard unambiguous
> format
>
>
> # as.Date.character uses strptime, so we can try using strptime directly
> strptime(as.character(x), "%Y-%m-%d")
> # "0001-01-24" "0500-01-24"
>
> strptime(as.character(x - 391), "%Y-%m-%d")
> # NA           "0498-12-29"
>
>
> Thanks,
> -Winston
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Negative years with strptime?

Jeffrey Ryan-2
?as.Date

Why would pre 0000 years be handled correctly?  The help file
explicitly states that they likely will not.


Note:

     The default formats follow the rules of the ISO 8601 international
     standard which expresses a day as ‘"2001-02-03"’.

     If the date string does not specify the date completely, the
     returned answer may be system-specific.  The most common behaviour
     is to assume that a missing year, month or day is the current one.
     If it specifies a date incorrectly, reliable implementations will
     give an error and the date is reported as ‘NA’.  Unfortunately
     some common implementations (such as ‘glibc’) are unreliable and
     guess at the intended meaning.

     Years before 1CE (aka 1AD) will probably not be handled correctly.



On Tue, Jul 10, 2012 at 4:59 PM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Is there a bug with negative dates? Just see:
>
> seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
>  [1] "0000-01-01" "000/-01-01" "000.-01-01" "000--01-01" "000,-01-01"
>  [6] "000+-01-01" "000*-01-01" "000)-01-01" "000(-01-01" "000'-01-01"
> [11] "00/0-01-01" "00//-01-01" "00/.-01-01" "00/--01-01" "00/,-01-01"
> [16] "00/+-01-01" "00/*-01-01" "00/)-01-01" "00/(-01-01" "00/'-01-01"
> [21] "00.0-01-01" "00./-01-01"
>
> See the year number: "after" the zero, i.e., downward from zero, the printed
> character is '/' which happens to be the ascii character before '0', and
> before it's '.', etc. This sequence gives the nine ascii characters before
> zero as last digit of the year, then the 10th is '0' as (now) expected, then
> goes to the second digit, etc.
>
> It seems to stop at the first 9, or else we would have some sort of real
> problem.
>
> Anyway, this doesn't look right.
>
> Rui Barradas
> Em 10-07-2012 22:17, Winston Chang escreveu:
>
>> Is there a way to make as.Date() and strptime() process strings with
>> negative years? It appears that Date objects can contain negative years
>> and
>> you can convert them to strings, but you can't convert them back to Date
>> objects.
>>
>>
>> x <- as.Date(c("0001-01-24", "0500-01-24"))
>> as.character(x)
>> # "0001-01-24" "0500-02-02"
>> as.Date(as.character(x))
>> # "0001-01-24" "0500-01-24"
>>
>> # Minus 391 days gives negative year
>> as.character(x - 391)
>> # "-001-12-30" "0498-12-29"
>>
>> # Can't convert this string back to Date
>> as.Date(as.character(x - 391))
>> # Error during wrapup: character string is not in a standard unambiguous
>> format
>>
>>
>> # as.Date.character uses strptime, so we can try using strptime directly
>> strptime(as.character(x), "%Y-%m-%d")
>> # "0001-01-24" "0500-01-24"
>>
>> strptime(as.character(x - 391), "%Y-%m-%d")
>> # NA           "0498-12-29"
>>
>>
>> Thanks,
>> -Winston
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



--
Jeffrey Ryan
[hidden email]

www.lemnica.com
www.esotericR.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Negative years with strptime?

Winston Chang
In reply to this post by Rui Barradas
It looks different on my system (Mac, R 2.15.1)

seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
 [1] "0000-01-01" "-001-01-01" "-002-01-01" "-003-01-01" "-004-01-01"
 [6] "-005-01-01" "-006-01-01" "-007-01-01" "-008-01-01" "-009-01-01"
[11] "-010-01-01" "-011-01-01" "-012-01-01" "-013-01-01" "-014-01-01"
[16] "-015-01-01" "-016-01-01" "-017-01-01" "-018-01-01" "-019-01-01"
[21] "-020-01-01" "-021-01-01"

So in addition to the issues with converting a negative-year string to a
Date, it looks like converting a negative date to a string with
as.character.Date() is probably also not consistent.

It certainly would be useful to have a way of handling dates with negative
years.

-Winston


On Tue, Jul 10, 2012 at 4:59 PM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Is there a bug with negative dates? Just see:
>
> seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
>  [1] "0000-01-01" "000/-01-01" "000.-01-01" "000--01-01" "000,-01-01"
>  [6] "000+-01-01" "000*-01-01" "000)-01-01" "000(-01-01" "000'-01-01"
> [11] "00/0-01-01" "00//-01-01" "00/.-01-01" "00/--01-01" "00/,-01-01"
> [16] "00/+-01-01" "00/*-01-01" "00/)-01-01" "00/(-01-01" "00/'-01-01"
> [21] "00.0-01-01" "00./-01-01"
>
> See the year number: "after" the zero, i.e., downward from zero, the
> printed character is '/' which happens to be the ascii character before
> '0', and before it's '.', etc. This sequence gives the nine ascii
> characters before zero as last digit of the year, then the 10th is '0' as
> (now) expected, then goes to the second digit, etc.
>
> It seems to stop at the first 9, or else we would have some sort of real
> problem.
>
> Anyway, this doesn't look right.
>
> Rui Barradas
> Em 10-07-2012 22:17, Winston Chang escreveu:
>
>> Is there a way to make as.Date() and strptime() process strings with
>> negative years? It appears that Date objects can contain negative years
>> and
>> you can convert them to strings, but you can't convert them back to Date
>> objects.
>>
>>
>> x <- as.Date(c("0001-01-24", "0500-01-24"))
>> as.character(x)
>> # "0001-01-24" "0500-02-02"
>> as.Date(as.character(x))
>> # "0001-01-24" "0500-01-24"
>>
>> # Minus 391 days gives negative year
>> as.character(x - 391)
>> # "-001-12-30" "0498-12-29"
>>
>> # Can't convert this string back to Date
>> as.Date(as.character(x - 391))
>> # Error during wrapup: character string is not in a standard unambiguous
>> format
>>
>>
>> # as.Date.character uses strptime, so we can try using strptime directly
>> strptime(as.character(x), "%Y-%m-%d")
>> # "0001-01-24" "0500-01-24"
>>
>> strptime(as.character(x - 391), "%Y-%m-%d")
>> # NA           "0498-12-29"
>>
>>
>> Thanks,
>> -Winston
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-devel<https://stat.ethz.ch/mailman/listinfo/r-devel>
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Negative years with strptime?

Rui Barradas
On my earlier post I forgot to mention the sessionInfo()

R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252
LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C

[5] LC_TIME=Portuguese_Portugal.1252

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

Rui Barradas

Em 11-07-2012 02:05, Winston Chang escreveu:

> It looks different on my system (Mac, R 2.15.1)
>
> seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
>   [1] "0000-01-01" "-001-01-01" "-002-01-01" "-003-01-01" "-004-01-01"
>   [6] "-005-01-01" "-006-01-01" "-007-01-01" "-008-01-01" "-009-01-01"
> [11] "-010-01-01" "-011-01-01" "-012-01-01" "-013-01-01" "-014-01-01"
> [16] "-015-01-01" "-016-01-01" "-017-01-01" "-018-01-01" "-019-01-01"
> [21] "-020-01-01" "-021-01-01"
>
> So in addition to the issues with converting a negative-year string to a
> Date, it looks like converting a negative date to a string with
> as.character.Date() is probably also not consistent.
>
> It certainly would be useful to have a way of handling dates with
> negative years.
>
> -Winston
>
>
> On Tue, Jul 10, 2012 at 4:59 PM, Rui Barradas <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Hello,
>
>     Is there a bug with negative dates? Just see:
>
>     seq(as.Date("0000-01-01"), length = 22, by = "-1 year")
>       [1] "0000-01-01" "000/-01-01" "000.-01-01" "000--01-01" "000,-01-01"
>       [6] "000+-01-01" "000*-01-01" "000)-01-01" "000(-01-01" "000'-01-01"
>     [11] "00/0-01-01" "00//-01-01" "00/.-01-01" "00/--01-01" "00/,-01-01"
>     [16] "00/+-01-01" "00/*-01-01" "00/)-01-01" "00/(-01-01" "00/'-01-01"
>     [21] "00.0-01-01" "00./-01-01"
>
>     See the year number: "after" the zero, i.e., downward from zero, the
>     printed character is '/' which happens to be the ascii character
>     before '0', and before it's '.', etc. This sequence gives the nine
>     ascii characters before zero as last digit of the year, then the
>     10th is '0' as (now) expected, then goes to the second digit, etc.
>
>     It seems to stop at the first 9, or else we would have some sort of
>     real problem.
>
>     Anyway, this doesn't look right.
>
>     Rui Barradas
>     Em 10-07-2012 22:17, Winston Chang escreveu:
>
>         Is there a way to make as.Date() and strptime() process strings with
>         negative years? It appears that Date objects can contain
>         negative years and
>         you can convert them to strings, but you can't convert them back
>         to Date
>         objects.
>
>
>         x <- as.Date(c("0001-01-24", "0500-01-24"))
>         as.character(x)
>         # "0001-01-24" "0500-02-02"
>         as.Date(as.character(x))
>         # "0001-01-24" "0500-01-24"
>
>         # Minus 391 days gives negative year
>         as.character(x - 391)
>         # "-001-12-30" "0498-12-29"
>
>         # Can't convert this string back to Date
>         as.Date(as.character(x - 391))
>         # Error during wrapup: character string is not in a standard
>         unambiguous
>         format
>
>
>         # as.Date.character uses strptime, so we can try using strptime
>         directly
>         strptime(as.character(x), "%Y-%m-%d")
>         # "0001-01-24" "0500-01-24"
>
>         strptime(as.character(x - 391), "%Y-%m-%d")
>         # NA           "0498-12-29"
>
>
>         Thanks,
>         -Winston
>
>                  [[alternative HTML version deleted]]
>
>         ________________________________________________
>         [hidden email] <mailto:[hidden email]> mailing list
>         https://stat.ethz.ch/mailman/__listinfo/r-devel
>         <https://stat.ethz.ch/mailman/listinfo/r-devel>
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Loading...