New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date time conversion for different locales #140

Closed
mviana opened this Issue Feb 14, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@mviana

mviana commented Feb 14, 2017

I had a problem using the getSymbols from google, since I use another locale than en_US.utf8, the strptime in getSymbols.google does not recognize the "%B" tag and therefore the dates come all NA.

For me, the solution was simply to do a temporary setup in the LC_TIME inside the function. It's not beautiful and might require tests (for parallel systems, or other application):

# Save current locale
def.locale <- Sys.getlocale("LC_TIME")
# Changes the locale for google's en_US
Sys.setlocale(category="LC_TIME",locale="en_US.utf8")

fr <- xts(as.matrix(fr[,-1]),
         as.Date(strptime(fr[,1],"%d-%B-%y"),origin='1970-01-01'),
         src='google',updated=Sys.time())
colnames(fr) <- paste(toupper(gsub('\\^','',Symbols.name)),
                     c('Open','High','Low','Close','Volume'),
                     sep='.')
# convert '-' to NAs
suppressWarnings(storage.mode(fr) <- "numeric")
fr <- convert.time.series(fr=fr,return.class=return.class)
Symbols[[i]] <-toupper(gsub('\\^','',Symbols[[i]])) 
if(auto.assign)
 assign(Symbols[[i]],fr,env)
# Set locale back
Sys.setlocale(category="LC_TIME",locale="en_US.utf8")

Cheers

@joshuaulrich

This comment has been minimized.

Show comment
Hide comment
@joshuaulrich

joshuaulrich Feb 14, 2017

Owner

Thanks for the report! I'm not sure how I feel about changing system settings inside a function though. I will investigate alternative solutions.

Owner

joshuaulrich commented Feb 14, 2017

Thanks for the report! I'm not sure how I feel about changing system settings inside a function though. I will investigate alternative solutions.

@joshuaulrich joshuaulrich changed the title from Date time convertion for different locales to Date time conversion for different locales Feb 15, 2017

joshuaulrich added a commit that referenced this issue Apr 12, 2017

Ensure strptime uses C locale
The CSV Google provides contains English month abbreviations.
strptime() uses the current locale LC_TIME when parsing month
abbreviations. If the user is in a locale that has different
abbreviations, different locale, strptime() may return some or all
dates as NA.

Set LC_TIME = "C" for the duration of the getSymbols.google() call.
Using on.exit() ensures the user's original locale will be restored
regardless of how the function exits (e.g. even on error).

Fixes #140.

@joshuaulrich joshuaulrich added this to the Release 0.4-8 milestone Apr 12, 2017

@joshuaulrich joshuaulrich added the bug label Apr 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment