http://www.perlmonks.org?node_id=870856

pokki has asked for the wisdom of the Perl Monks concerning the following question:

I'm planning on using DateTime::Format::Flexible or DateTime::Format::Natural to parse datetime strings. The problem is that the string to be parsed may or may not contain a time component, e.g. "2010-11-11 Thu" is valid, as is "2010-11-11 Thu 22:00".

How do I differentiate between a DateTime object that was built from a string without a time component, and one which was built from, say, "2010-11-11 Thu 0:00", since both would have the same time component?

(This is a calendaring type application, so "no time component" is to be interpreted as "all day", not just as "user forgot to set date so we'll treat it as an error" or "use a default". It's also built from an existing format which I can't extend to manually indicate "all day" type timestamps.)

Update I may not have made myself clear, but the time in the string is not a duration, it's part of a timestamp, e.g. "Meeting <2010-11-11 Thu 13:00>" means that a meeting is scheduled for November 11 at 13:00 (or 1 PM).

Replies are listed 'Best First'.
Re: DateTime parsing and midnight
by Corion (Patriarch) on Nov 11, 2010 at 13:56 UTC

    I wouldn't lump the two together. One is a point in time, easiest modelled as DateTime instance. The other is a duration with a (fixed) start and endpoint or with a fixed start point and a length given in (say) minutes.

    As the two are different, use different parsers.

      I may not have made myself clear, but the time in the string is not a duration, it's part of a timestamp, e.g. "Meeting <2010-11-11 Thu 13:00>" means that a meeting is scheduled for November 11 at 13:00 (or 1 PM).

        Yes. That is a point in time. But the other string is a duration, as it is meant for the whole day, isn't it?

        In any way, as you seem to want the two to be treated differently, you will have to do so.