Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the DateTime CPAN distribution.

Report information
The Basics
Id: 25341
Status: resolved
Priority: 0/
Queue: DateTime

People
Owner: Nobody in particular
Requestors: krishpl [...] shamu.corp.sgi.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.36
Fixed in: (no value)



Subject: string comparison with scalars
Sure would be nice if I could use my $startdt = DateTime->now; if ($startdt lt "200702") { warn "Your clock is *way* off"; } Right now, if ("$startdt" lt "200702") { etc } works ok, but it seems that automatically stringifying the DateTime object when doing a string compare ([gl][te], eq, ne) against a scalar would nearly always be the right thing to do, and is pretty readable in straight code. Thanks for an awesome suite of modules, by the way.
Whoops, that should be: my $startdt = DateTime->now; if ($startdt lt "2007-02") { warn "Your clock is *way* off"; } Right now, if ("$startdt" lt "2007-02") { etc }
On Thu Mar 08 14:06:37 2007, KRISHPL wrote: Show quoted text
> Sure would be nice if I could use > > my $startdt = DateTime->now; > > if ($startdt lt "200702") { > warn "Your clock is *way* off"; > }
This seems like an obvious feature, but it's pretty complicated. First, DateTime.pm does not have a date parser built-in, so we can't parse the string. That means that the string would be limited to ISO8601 or some other format that DateTime.pm can output. Second, while this works pretty simply for dates without times, once you add times you start wanting to consider time zones, which makes the string more complicated. Honestly, I don't think this feature is worth the trouble, because it would be fairly limited, and it would fail to DWIM what mean people might M.
From: krishnoid [...] wapacut.com
On Thu Mar 15 10:46:15 2007, DROLSKY wrote: Show quoted text
> On Thu Mar 08 14:06:37 2007, KRISHPL wrote:
> > Sure would be nice if I could use > > > > my $startdt = DateTime->now; > > > > if ($startdt lt "200702") { > > warn "Your clock is *way* off"; > > }
> > First, DateTime.pm does not have a date parser built-in, so we can't > parse the string. That means that the string would be limited to ISO8601 > or some other format that DateTime.pm can output. Second, while this
I meant that when a DateTime object was an operator of a string comparison ([lg][te] eq ne cmp), it would stringify the DateTime object, not that the string operator would try to parse the string as a date. Anyone who has tried to print a DateTime object knows that it stringifies by default to ISO8601 or to whatever the output formatter has been set to, so that seems pretty unsurprising (in the technical sense). Show quoted text
> works pretty simply for dates without times, once you add times you > start wanting to consider time zones, which makes the string more > complicated.
Yup I believe that. I think that would be a bad idea too. Although, another suggestion would be that DateTime be able to create a DateTime object from its own default (iso8601) string output format. This would provide a sort of self-containment (or mathematical 'closure' on the set of its inputs and outputs), and wouldn't give the impression "This module is willing to stringify to a common format, but not willing to accept that same format". A handy side-effect of that is vastly simplified interchange between DateTime<--stringify/parse-->iso8601<--select/insert-->databases With perhaps a single s/ /T/ in one direction or the other. But this is an issue for another bug. Show quoted text
> Honestly, I don't think this feature is worth the trouble, because it > would be fairly limited, and it would fail to DWIM what mean people
might M. Sure, no problem, I can work around it, but my idea of DWIM was that if a DateTime stringifies in printf or doublequotes (which is really, really handy, thanks -- in particular, I can use it directly in SQL server statements), that it should stringify when presented to a string comparison operator. I'd think a quick addendum to the docs would cover that. ------ The module also overloads stringification to use the iso8601() method, which covers explicit string interpolation and string comparisons as well as other forms of stringification. ------ Thanks for an excellent module. It's made some of the work I'm currently doing simpler, and allows for clearer code with thorough error checking.
From: krishnoid [...] wapacut.com
Didn't mean to reopen this with this comment (RT automatically did that) -- but hey, while it's open, maybe you would perhaps reconsider ? :-)
On Thu Apr 26 14:29:49 2007, krishnoid@wapacut.com wrote: Show quoted text
> On Thu Mar 15 10:46:15 2007, DROLSKY wrote:
> > On Thu Mar 08 14:06:37 2007, KRISHPL wrote:
> > > Sure would be nice if I could use > > > > > > my $startdt = DateTime->now; > > > > > > if ($startdt lt "200702") { > > > warn "Your clock is *way* off"; > > > }
> > > > First, DateTime.pm does not have a date parser built-in, so we can't > > parse the string. That means that the string would be limited to ISO8601 > > or some other format that DateTime.pm can output. Second, while this
> > I meant that when a DateTime object was an operator of a string comparison > ([lg][te] eq ne cmp), it would stringify the DateTime object, not that the > string operator would try to parse the string as a date. Anyone who has > tried to print a DateTime object knows that it stringifies by default to > ISO8601 or to whatever the output formatter has been set to, so that seems > pretty unsurprising (in the technical sense).
I was buying this argument, but then I realized that because of time zones, this could really be weird. You can't determine if a datetime is equal to another datetime by looking at the default stringification output, since it doesn't include the time zone. I'm going to close this ticket. If you'd like to discuss this further, let's do so on the datetime@perl.org list.