gpt4 book ai didi

ruby - 强制 Ruby Time 类使用 ISO8601

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:43 25 4
gpt4 key购买 nike

当调用 to_s 时,如何强制 Ruby 的 Time 类使用 ISO8601?

最佳答案

正如@DumpHole 提到的,您始终可以像这样覆盖 Time.to_s:

require 'time'    

class Time
def to_s
iso8601
end
end

但是,您可能不想为所有 Time 实例覆盖 to_s。相反,您可以创建自定义包装器类并使用它:

require 'time'

class Iso8601Time < Time
def to_s
iso8601
end
end

Iso8601Time.now.to_s #=> "2015-04-17T17:35:13-07:00"

关于ruby - 强制 Ruby Time 类使用 ISO8601,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711510/

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