ActiveRecord的不同日期/时间格式 [英] ActiveRecord with different Date/Time format
问题描述
我们有一个应用程序,用户将他/她的preferred日期/时间格式。用户预计将在整个应用程序配置的格式输入日期时间
。现在的问题是少数几种格式的日期时间
解析错误,而创建/更新
ActiveRecord的
。
We have an application where user sets his/her preferred date/time format. User is expected to enter datetime
in configured format across the application. Now the problem is for few formats the datetime
is parsed wrongly while create/update
ActiveRecord
.
例如用户已设置的日期/时间格式 HH:MM DD / MM / YYYY
。现在,如果用户输入 17:00 04/05/2012
它解析为下午5点2012年4月5日
的地方应下午5点2012年5月4日
For example user has set date/time format in hh:mm dd/MM/yyyy
. Now if user enters 17:00 04/05/2012
it parses it as 5 PM 5 Apr, 2012
where it should be 5 PM 4 May, 2012
1.8.7 :004 > a = Article.create!(:name => 'a1', :published_at => '17:00 04/05/2012')
=> #<Article id: 2, name: "a1", published_at: "2012-04-05 17:00:00", created_at: "2012-04-16 13:54:36", updated_at: "2012-04-16 13:54:36">
据我所知,红宝石/ Rails已经predefined设置格式针对的日期/时间字符串的判断,如果按顺序匹配任何格式,它分析给定的字符串与该格式。
I understand that ruby/rails has predefined set of formats against which the date/time string is evaluated and if matches any format in sequence it parses given string with that format.
另一个例子
1.8.7 :006 > a = Article.create!(:name => 'a1', :published_at => '17:00 15/12/2012')
=> #<Article id: 4, name: "a1", published_at: nil, created_at: "2012-04-16 13:55:43", updated_at: "2012-04-16 13:55:43">
下面用户输入 15 2012年12月,
这是解析,以零
Here user entered 15 December, 2012
which was parsed to nil
有人已经在这里问类似的问题 和的红宝石论坛,但该解决方案并不适用于我的情况,我无法配置在整个应用程序的单一日期时间格式为每个用户都会有自己的preferred日期时间格式。
Someone has already asked similar question here and on ruby-forum but the solution is not applicable in my case as I cannot configure a single datetime format across the application as each user will have his own preferred datetime format.
现在有多个日期时间
格式,我就准备了 PARAMS前操纵它的工作
传递给的ActiveRecord
。因此,步骤想
Now to work with multiple datetime
format, I am planning to manipulate it before the params
is passed to the ActiveRecord
. So the steps would like
- 读
日期时间
从PARAMS
地图 - 在与用户的preferred日期时间格式解析
- 格式
日期时间
来使用的ActiveRecord
的默认格式
- 替换当前的
日期时间
在PARAMS
与重新格式化日期时间
- Read
datetime
fromparams
map - Parse it with user's preferred datetime format
- Format
datetime
to the default format used byActiveRecord
- Replace current
datetime
inparams
with re-formatted datetime
下面是控制器的一个片段
Here is a snippet of the controller
class ArticlesController < ApplicationController
before_filter :format_date_time, :only => [:create, :update]
...
...
private
def format_date_time
datetime = params[:datetime]
params[:datetime] = DateTime.strptime(datetime, pref_date_time_format).strftime('%c')
end
end
会有人请确认它是否是正确的做法还是不?我将AP preciable如果有人提出更好的方法来做到这一点。
Would anyone please verify if it is right approach or not? I would be appreciable if someone suggests better ways to accomplish this.
谢谢, 阿米特·帕特尔
Thanks, Amit Patel
推荐答案
我将与我提到的方法去。如果任何人有更好的方法请大家共享。
I would go with the approach I mentioned. If anyone has better way please keep sharing.
谢谢,阿米特·帕特尔
这篇关于ActiveRecord的不同日期/时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!