rpy2.robjects changes time.strptime locale?

Issue #268 closed
CE created an issue

As shown below:

In [1]: from time import strptime

In [2]: strptime('Fri Feb 06 2015', '%a %b %d %Y')
Out[2]: time.struct_time(tm_year=2015, tm_mon=2, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=37, tm_isdst=-1)

In [3]: import rpy2.robjects as robjects

In [4]: strptime('Fri Feb 06 2015', '%a %b %d %Y')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-880728a7a193> in <module>()
----> 1 strptime('Fri Feb 06 2015', '%a %b %d %Y')

/usr/lib/python2.7/_strptime.pyc in _strptime_time(data_string, format)
    465 
    466 def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
--> 467     return _strptime(data_string, format)[0]

/usr/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
    323     if not found:
    324         raise ValueError("time data %r does not match format %r" %
--> 325                          (data_string, format))
    326     if len(data_string) != found.end():
    327         raise ValueError("unconverted data remains: %s" %

ValueError: time data 'Fri Feb 06 2015' does not match format '%a %b %d %Y'

I figured only %a was broken, however:

In [5]: strptime('Feb 06 2015', '%b %d %Y')
Out[5]: time.struct_time(tm_year=2015, tm_mon=2, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=37, tm_isdst=-1)

In [6]: strptime('Mar 19 2015', '%b %d %Y')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-999315f0de57> in <module>()
----> 1 strptime('Mar 19 2015', '%b %d %Y')

/usr/lib/python2.7/_strptime.pyc in _strptime_time(data_string, format)
    465 
    466 def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
--> 467     return _strptime(data_string, format)[0]

/usr/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
    323     if not found:
    324         raise ValueError("time data %r does not match format %r" %
--> 325                          (data_string, format))
    326     if len(data_string) != found.end():
    327         raise ValueError("unconverted data remains: %s" %

ValueError: time data 'Mar 19 2015' does not match format '%b %d %Y'

Which definitely indicated a locale issue, as proven with Dutch time:

In [7]: strptime('Mrt 19 2015', '%b %d %Y')
Out[7]: time.struct_time(tm_year=2015, tm_mon=3, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=78, tm_isdst=-1)

Which is weird, considering:

In [1]: from locale import getlocale, resetlocale

In [2]: getlocale()
Out[2]: (None, None)

In [3]: import rpy2.robjects as robjects

In [4]: getlocale()
Out[4]: ('en_US', 'UTF-8')

And the fact that it is fixed with:

In [5]: locale.resetlocale()

In [5]: strptime('Fri Feb 06 2015', '%a %b %d %Y')
Out[5]: time.struct_time(tm_year=2015, tm_mon=2, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=37, tm_isdst=-1)

It is still on the same locale though:

In [6]: getlocale()
Out[6]: ('en_US', 'UTF-8')

Comments (8)

  1. Laurent Gautier

    This is very likely happening during the initialization of the embedded R (and this is done by R itself, not rpy2).

    Start R and enter:

    Sys.getlocale()
    

    The initialization is done in setup_Rmainloop(), located in the R source at src/main/main.c:

        if(!setlocale(LC_TIME, ""))
            snprintf(deferred_warnings[ndeferred_warnings++], 250,
                     "Setting LC_TIME failed, using \"C\"\n");
    
  2. Log in to comment