Ruby:如何在 YAML 中定义日期时间字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2656196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:32:08  来源:igfitidea点击:

Ruby: How do I define a datetime field in YAML?

ruby-on-railsrubyunit-testingyaml

提问by Drew Johnson

I'm creating test data for a Rails app. How do I define the value for a datetime field in YAML?

我正在为 Rails 应用程序创建测试数据。如何在 YAML 中定义日期时间字段的值?

回答by house9

when rails generates fixture files it uses the following format in the yaml files

当 rails 生成夹具文件时,它在 yaml 文件中使用以下格式


one:
 something_at: 2010-02-11 11:02:57
 something_on: 2010-02-11

回答by Drew Johnson

Found the answer here:

这里找到答案:

model:
   datetime_field: 2010-04-16 16:32:03 -5

回答by Sniggerfardimungus

When I yaml DateTime, I generally pack it as a string:

我在yaml DateTime的时候,一般都是把它打包成一个字符串:

foo = DateTime.now
foo_yaml = YAML.dump(foo.to_s)
foo = DateTime.parse(YAML.load(foo_yaml))
puts foo

I'd be interested to know if there's a better way.

我很想知道是否有更好的方法。

回答by redrom

For me works it wrapped in " "

对我来说,它包裹在“”中

created_at: "2010-02-11 11:02:57"

回答by Simon Liu

With Time zone: created_at: 2013-09-25 16:00:00 +0800

带时区: created_at: 2013-09-25 16:00:00 +0800