Convert UTC to local time in Rails 3

后端 未结 6 928
面向向阳花
面向向阳花 2020-11-30 19:14

I\'m having trouble converting a UTC Time or TimeWithZone to local time in Rails 3.

Say moment is some Time varia

相关标签:
6条回答
  • 2020-11-30 19:30

    Rails has its own names. See them with:

    rake time:zones:us
    

    You can also run rake time:zones:all for all time zones. To see more zone-related rake tasks: rake -D time

    So, to convert to EST, catering for DST automatically:

    Time.now.in_time_zone("Eastern Time (US & Canada)")
    
    0 讨论(0)
  • 2020-11-30 19:31

    Time#localtime will give you the time in the current time zone of the machine running the code:

    > moment = Time.now.utc
      => 2011-03-14 15:15:58 UTC 
    > moment.localtime
      => 2011-03-14 08:15:58 -0700 
    

    Update: If you want to conver to specific time zones rather than your own timezone, you're on the right track. However, instead of worrying about EST vs EDT, just pass in the general Eastern Time zone -- it will know based on the day whether it is EDT or EST:

    > Time.now.utc.in_time_zone("Eastern Time (US & Canada)")
      => Mon, 14 Mar 2011 11:21:05 EDT -04:00 
    > (Time.now.utc + 10.months).in_time_zone("Eastern Time (US & Canada)")
      => Sat, 14 Jan 2012 10:21:18 EST -05:00 
    
    0 讨论(0)
  • 2020-11-30 19:34

    There is actually a nice Gem called local_time by basecamp to do all of that on client side only, I believe:

    https://github.com/basecamp/local_time

    0 讨论(0)
  • 2020-11-30 19:37

    It is easy to configure it using your system local zone, Just in your application.rb add this

    config.time_zone = Time.now.zone
    

    Then, rails should show you timestamps in your localtime or you can use something like this instruction to get the localtime

    Post.created_at.localtime
    
    0 讨论(0)
  • 2020-11-30 19:53

    Don't know why but in my case it doesn't work the way suggested earlier. But it works like this:

    Time.now.change(offset: "-3000")
    

    Of course you need to change offset value to yours.

    0 讨论(0)
  • 2020-11-30 19:55

    If you're actually doing it just because you want to get the user's timezone then all you have to do is change your timezone in you config/applications.rb.

    Like this:

    Rails, by default, will save your time record in UTC even if you specify the current timezone.

    config.time_zone = "Singapore"
    

    So this is all you have to do and you're good to go.

    0 讨论(0)
提交回复
热议问题