Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 598
Drew Johnson Avatar asked Apr 16 '10 21:04

Drew Johnson


People also ask

Does YAML support unicode?

YAML accepts the entire Unicode character set, except for some control characters, and may be encoded in any one of UTF-8, UTF-16 or UTF-32.

Is YAML a markup language?

YAML (YAML Ain't Markup Language) is a data-oriented language structure used as the input format for diverse software applications. YAML is not intended to be a markup language used for document markup. An application user or administrator specifies data in a YAML file, which the application can read.

Is it yml or YAML?

The (rather sparse) YAML FAQ recommends that you use . yaml in preference to . yml , but for historic reasons many Windows programmers are still scared of using extensions with more than three characters and so opt to use . yml instead.

What is the YAML file format?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.


4 Answers

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

like image 163
house9 Avatar answered Oct 07 '22 15:10

house9


Found the answer here:

model:
   datetime_field: 2010-04-16 16:32:03 -5
like image 24
Drew Johnson Avatar answered Oct 07 '22 13:10

Drew Johnson


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.

like image 27
Sniggerfardimungus Avatar answered Oct 07 '22 14:10

Sniggerfardimungus


For me works it wrapped in " "

created_at: "2010-02-11 11:02:57"
like image 20
redrom Avatar answered Oct 07 '22 14:10

redrom