开发者

Ruby on Rails date locale

I have a datetime string in non-English locale and开发者_运维知识库 Time.parse doesn't understand it:

Pr, 28 Vas 2011 05:04:00 +0200

Is there any way to convert this into:

Mon, 28 Feb 011 05:04:00 +0200

in Ruby on Rails? Or do I have to write my own converter of week days and months for each locale?


Ruby time parsing is handed off to date parsing, which follows the priority:

_parse_time(str, e) # || _parse_beat(str, e)
_parse_day(str, e)

_parse_eu(str, e)     ||
_parse_us(str, e)     ||
_parse_iso(str, e)    ||
_parse_jis(str, e)    ||
_parse_vms(str, e)    ||
_parse_sla_us(str, e) ||
_parse_iso2(str, e)   ||
_parse_year(str, e)   ||
_parse_mon(str, e)    ||
_parse_mday(str, e)   ||
_parse_ddd(str, e)

i.e. it doesn't look to parse considering a locale you hand it. Parsing dates and times in multiple locales is horrible. I would love to be able to help you, but to parse input from a locale that your server is not running on, I would run for the hills (I can't find any, google is not my friend at 1am).

fastest answer is... split the input, rather than accepting free-text.

You can try setting the input locale for every request thread, but most linux/unix implementations have a locale per-process, which means that you would have a race condition for parsing strings to dates for every requesting thread in your rails application.

Yes, I do realize that this is probably not the answer that you're looking for, but unfortunately, ruby relies on standard C, which is a colossal pair of pants for multi-threaded applications


Are you looking for something like this: http://guides.rubyonrails.org/i18n.html#adding-date-time-formats ?


If you absolutely must parse a time/date string (multi-parameter input isn't an option?) then I would write a regular expression to convert your source locale into the locale the server is running and pass that to Time.parse. I agree with @Petesh, parsing in multi-locale environments is an area that Rails could use some improvement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜