热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Rails,如何在正确的时区内获取日期时间?-Rails,howtogethoursofadatetimeinthecorrecttimezone?

Ihaveanoffermodel.andwhenIaccessitfromthecommandline,itisdisplayedinthefollowing

I have an offer model. and when I access it from the command line, it is displayed in the following format:

我有报价模型。当我从命令行访问它时,它以下列格式显示:

1.9.3p392 :058 > o = Offer.last
 => # 

In this output, valid_from and valid_until are formatted as I would expect. however, when I access them directly, I get something different:

在此输出中,valid_from和valid_until的格式符合我的预期。但是,当我直接访问它们时,我会得到一些不同的东西:

1.9.3p392 :059 > o.valid_from
   => Fri, 12 Apr 2013 05:00:00 EDT -04:00 

The issue comes with this function I have inside of app/models/offer_model.rb :

问题来自app / models / offer_model.rb中的这个函数:

  def start_hour
    self.valid_from.strftime('%l').to_i unless self.valid_from.nil?
  end

When I run this command, with the model above, the output is 5. I would expect it to return 9 in this case. How can I get start_hour to return the hour adjusted for the time zone in this case?

当我运行此命令时,使用上面的模型,输出为5.我希望它在这种情况下返回9。在这种情况下,如何让start_hour返回为时区调整的小时数?

1 个解决方案

#1


0  

What Time zone are you using in config/application.rb? Rails adjust printed date to the Time Zone set in this file.

你在config / application.rb中使用什么时区? Rails将打印日期调整为此文件中设置的时区。


推荐阅读
author-avatar
铱雯_213
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有