简体   繁体   English

期望Time.zone.parse引发带有无效日期的错误,但返回nil

[英]Expecting Time.zone.parse to raise error with invalid date but return nil

Why doesn't Time.zone.parse('invalid') raise an error while Time.parse('invalid') does ? 为什么不Time.zone.parse('invalid')而产生错误Time.parse('invalid')呢?


Example using Rails 4.2.7.1 使用Rails 4.2.7.1的示例

>> Time.zone.parse('invalid')
nil

>> Time.parse('invalid')
ArgumentError: no time information in "invalid"
    from D:/softwares/ruby/Ruby22/lib/ruby/2.2.0/time.rb:252:in `make_time'
    from D:/softwares/ruby/Ruby22/lib/ruby/2.2.0/time.rb:364:in `parse'

Both methods call Date._parse which return {} if no date could be extracted. 两种方法都调用Date._parse ,如果无法提取日期,则返回{}

Time.zone.parse stops there and returns if the result is empty. Time.zone.parse在那里停止,如果结果为空,则返回。 Time.parse makes a call to make_time which throws an error if no date could be generated. Time.parse调用make_time ,如果无法生成日期,则抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM