我如何让 Ruby 解析时间,就好像它在不同的时区一样? [英] How do I get Ruby to parse time as if it were in a different time zone?

查看:42
本文介绍了我如何让 Ruby 解析时间,就好像它在不同的时区一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析这样的内容:

I'm parsing something like this:

11/23/10 23:29:57

没有与之关联的时区,但我知道它在 UTC 时区(而我不是).我怎样才能让 Ruby 像在 UTC 时区一样解析它?

which has no time zone associated with it, but I know it's in the UTC time zone (while I'm not). How can I get Ruby to parse this as if it were in the UTC timezone?

推荐答案

您可以在解析字符串之前将 UTC 时区名称附加到字符串中:

You could just append the UTC timezone name to the string before parsing it:

require 'time'
s = "11/23/10 23:29:57"
Time.parse(s) # => Tue Nov 23 23:29:57 -0800 2010
s += " UTC"
Time.parse(s) # => Tue Nov 23 23:29:57 UTC 2010

这篇关于我如何让 Ruby 解析时间,就好像它在不同的时区一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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