【发布时间】:2015-12-25 13:51:53
【问题描述】:
我的表中有时间列。并且在/config/application.rb 我有以下声明。 config.time_zone = 'Mumbai'.
我正在通过以下查询节省数据库中的时间
punch_rec.check_out = Time.now
punch_rec.save
当我在终端中输入 Time.now 时,它会显示以下时间。
2.2.2 :032 > Time.now
=> 2015-09-28 15:59:06 +0530
当我从数据库中检索数据时,它在UTC time_zone 中显示的时间。因此,在我看来,时间显示错误。
这些是我正在执行的步骤。
2.2.2 :038 > p = PunchInOut.find_by_id("28")
PunchInOut Load (0.3ms) SELECT `punch_in_outs`.* FROM `punch_in_outs` WHERE `punch_in_outs`.`id` = 28 LIMIT 1
=> #<PunchInOut id: 28, employee_id: 3, check_in: nil, check_out: nil, date: "2015-09-28", created_at: "2015-09-28 18:37:24", updated_at: "2015-09-28 10:25:22", shift_id: nil, shift_name: nil>
2.2.2 :039 > p.check_in
=> nil
2.2.2 :040 > Time.now
=> 2015-09-29 00:19:17 +0530
2.2.2 :041 > p.check_in= Time.now
=> 2015-09-29 00:19:33 +0530
2.2.2 :042 > p.save
(0.3ms) BEGIN
SQL (0.7ms) UPDATE `punch_in_outs` SET `check_in` = '2015-09-28 18:49:33', `updated_at` = '2015-09-28 18:49:38' WHERE `punch_in_outs`.`id` = 28
(37.7ms) COMMIT
=> true
2.2.2 :043 > p = PunchInOut.find_by_id("28")
PunchInOut Load (0.6ms) SELECT `punch_in_outs`.* FROM `punch_in_outs` WHERE `punch_in_outs`.`id` = 28 LIMIT 1
=> #<PunchInOut id: 28, employee_id: 3, check_in: "2000-01-01 18:49:33", check_out: nil, date: "2015-09-28", created_at: "2015-09-28 18:37:24", updated_at: "2015-09-28 18:49:38", shift_id: nil, shift_name: nil>
2.2.2 :044 > p.check_in
=> 2000-01-01 18:49:33 UTC
2.2.2 :045 >
我该如何解决这个问题。
【问题讨论】:
-
Rails 默认以 UTC 格式将所有时间保存到数据库中,然后它们在适当的时区中显示,并在运行中进行转换。所以,我希望视图显示适当转换的时间。您能否将以下信息添加到您的问题中:a)您认为应该在本地什么时候将
att.check_out设置为 b)存储在数据库中的内容,而不进行转换(查看您的 db 终端而不是使用 Rails)c ) 当您执行<%= att.check_out %>时显示的内容 -
@MaxWilliams 我更新了我的问题。
-
@MaxWilliams,如果 rails 每次都以 UTC 时间保存。那么这个语句有什么用(
config.time_zone = 'Mumbai')?它会做什么? -
这告诉 Rails 将 UTC 时间从哪个时区转换为显示目的。
标签: ruby-on-rails time timezone strftime