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

发布于 2024-08-29 08:46:56 字数 50 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情域 2024-09-05 08:46:56

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


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

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


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

丑丑阿 2024-09-05 08:46:56

此处找到了答案:

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

Found the answer here:

model:
   datetime_field: 2010-04-16 16:32:03 -5
妞丶爷亲个 2024-09-05 08:46:56

当我 yaml DateTime 时,我通常将其打包为字符串:

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

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

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

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.

哀由 2024-09-05 08:46:56

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

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

For me works it wrapped in " "

created_at: "2010-02-11 11:02:57"
罗罗贝儿 2024-09-05 08:46:56

有时区:
创建时间:2013-09-25 16:00:00 +0800

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文