支持将时区从Ruby中的任何其他时区转换为IST [英] Support to convert time zone to IST from any other time zone in Ruby

查看:109
本文介绍了支持将时区从Ruby中的任何其他时区转换为IST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下版本的Ruby and Rails
Ruby:2.0.0p481 &
轨道:4.1.1

I,m using following versions of Ruby and Rails Ruby : 2.0.0p481 & Rails : 4.1.1

DateTime 中找不到任何方法 class 将时间转换为IST。

Could not find any method in DateTime class to convert time to IST.

在IRB中尝试过 DateTime.in_time_zone

    2.0.0-p481 :003 > date = "Thu Jan 07 2016 16:20:00 GMT+0530 (India Standard Time)"
    2.0.0-p481 :003 > date = date.in_time_zone('Eastern Time (US & Canada)')
    2.0.0-p481 :003 > date
      => Thu, 07 Jan 2016 05:50:00 EST -05:00

想知道我该怎么办将其他齿区域转换为IST。
请让我知道是否有什么方法可以实现这一目标。

Wanted to know how can I convert other tine zones to IST. Please let me know if there is any way from which I can achive this.

推荐答案

ActiveSupport :: TimeZone 提供所有时区的名称。您可以通过执行以下操作列出所有时区

ActiveSupport::TimeZone provide the names of all timezones. You can list out all timezones by doing

ActiveSupport::TimeZone.all.map(&:name)

仅用于美国时区

ActiveSupport::TimeZone.us_zones.map(&:name)

因此,请更改可用时区的时间。像这样

So change your time with the available timezones. Like this

irb> date = DateTime.now
=> Thu, 31 Dec 2015 22:15:59 +0530

# convert to EST
irb> date_est = date.in_time_zone("Eastern Time (US & Canada)")
=> Thu, 31 Dec 2015 11:45:59 EST -05:00

# convert to IST
irb> date_ist = date_est.in_time_zone("Chennai")
=> Thu, 31 Dec 2015 22:15:59 IST +05:30

快乐的编码...

这篇关于支持将时区从Ruby中的任何其他时区转换为IST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆