Re: adding a time to a DT object

[email protected] (Zefram)
Newsgroups perl.datetime
Message-ID <[email protected]>
Perrin Harkins wrote:
>I tried making a Strptime object with pattern '%I %p' but that seems
>to just return undef when I try to parse these times.

%I only does the hour.  You'd need "%I:%M %p" to parse the string you
gave.  If the input is from a human, though, DT::F::Natural might be a
better choice: you don't have to tell it what the format is.

>                                                       It looks like
>maybe I want a DateTime::Incomplete,

You could use one to represent the time-of-day on its own, and that class
then provides a method to merge it with the date to make a complete
DateTime.  You don't win much from this.  I think it's only useful if
the parts you're merging don't always have the same boundary.  Say, if
you sometimes want to include the day-of-month in your time-of-day string.

>                                     but it's not clear how to make
>one from a string.

Yes.  DT::F::Strptime is logically an appropriate tool for this,
but it doesn't actually provide output in DT::Incomplete form.
Likewise DT::F::Natural.

>                    And it also looks like maybe I want a
>DateTime::Duration since I want to add it to my DT object,

No.  That would be appropriate if you were starting from a base time
and adding on a duration (such as 10 hours and 30 minutes).  That's not
what you're doing.  You're constructing the representation of a point
in time by taking parts of that representation from different places.
It's a piecewise construction, not an addition of complete entities.

>I ended up parsing the string myself and calling set methods on the
>DT, but that seems pretty lame with all of these classes available.

It's not a bad way, especially if the string format is fixed.  With
DT::F::Strptime doing its bit, you still need to use the set methods to
copy the time-of-day from one DT object to another (since DT::Incomplete
isn't doing that for you).

-zefram
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.