gpt4 book ai didi

ruby - 在 RubyMotion 中将字符串解析为时间

转载 作者:数据小太阳 更新时间:2023-10-29 07:20:22 25 4
gpt4 key购买 nike

我尝试将时间的文本表示解析为 ruby​​ Time 对象。

通常在 ruby​​ 中我会这样做:

require 'time'
Time.parse('2010-12-15T13:16:45Z')
# => 2010-12-15 13:16:45 UTC

在 RubyMotion 中我无法要求库并且 Time.parse 不可用:

(main)> require 'time'
=> #<RuntimeError: #require is not supported in RubyMotion>
(main)>
(main)> Time.parse
=> #<NoMethodError: undefined method `parse' for Time:Class>

有没有办法在不需要复制和重写整个代码的情况下使用ruby提供的时间库来兼容RubyMotion?

最佳答案

它不是自动的,但你可以使用 NSDateFormatter

date_formatter = NSDateFormatter.alloc.init
date_formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
date = date_formatter.dateFromString "2010-12-15T13:16:45Z"
puts date.description

关于ruby - 在 RubyMotion 中将字符串解析为时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com