我有一个代表各种用户接收电子邮件的应用程序,这些用户的收件箱(使用Mail
gem 通过 IMAP 获取)具有不同的时区。例如:
Loading development environment (Rails 3.2.12)
2.0.0-p247 :001 > first = Email.all[0].datetime
=> Mon, 04 Feb 2013 07:05:28 +0000
2.0.0-p247 :002 > second = Email.all[1].datetime
=> Wed, 21 Aug 2013 11:54:49 +0200
我的应用程序的用户位于不同的时区,所以他们每个人都有一个数据库字段来说明他们的时区。这可以是例如"+0000"
或"-0500"
。我需要我的代码来截取原始时区并将时间转换为用户的时区。
例如:
class User
include Mongoid::Document
field :timezone, :type => String, :default => "+0200"
has_many :emails, :inverse_of => :user
end
class Email
include Mongoid::Document
field :datetime, :type => DateTime
belongs_to :user, :inverse_of => :emails
end
Loading development environment (Rails 3.2.12)
2.0.0-p247 :001 > email = User.first.emails.first
=> #<Email ... >
2.0.0-p247 :002 > datetime = email.datetime
=> Mon, 04 Feb 2013 07:05:28 +0000
2.0.0-p247 :003 > email.update_attribute(datetime: datetime.convert_zone(datetime.zone, user.timezone) # => convert_zone is not an actual method, so what should I put in here?
=> Mon, 04 Feb 2013 09:05:28 +0200