Check out the source code for lib/yaml/rubytypes.rb in the ruby source
tree.  Ruby transforms a YAML timestamp[1] into an instance of Time.
You can add support for DateTime by defining a few methods. This naive
approach[2] might work, but there may be caveats.  Look at the yaml
methods for Time.  You will need to do a little more work if you need
to support micro seconds, since that info is not encoded in the string
representation of a DateTime.

[1]: http://yaml.org/type/timestamp.html
[2]: http://gist.github.com/13926

On Sep 29, 6:20 pm, Glenn Little <[EMAIL PROTECTED]> wrote:
> It appears that the roundtrip of a DateTime object to yaml and
> back via YAML.dump and YAML.load results in a Time object, instead
> of a DateTime object:
>
>   >> dt = DateTime.now
>   >> temp = YAML.dump(dt)
>   >> restored = YAML.load(temp)
>   >> restored.class
>   => Time
>
> Is there any more elegant way to deal with this other than
> hard-coding a conversion back to DateTime:
>
>   >> temp = YAML.dump(dt)
>   >> restored = YAML.load(temp).to_datetime
>   >> restored.class
>   => DateTime
>
> ?
>
> Thanks!
>
>         -glenn
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to