Skip to content

Allow specifying a custom class for the object returned from parsing #6

@autarch

Description

@autarch

Migrated from rt.cpan.org #77130 (status was 'open')

Requestors:

Attachments:

From joakim.tormoen@evry.com on 2012-05-11 11:45:23:

Hello Sir,

First let me start by saying thank you for creating and maintaining the DateTime package.

In one of my projects i have extended the DateTime::Duration package, so i needed the DateTime package to return a object of this class instead.

So i looked into the DateTime and discovered the possibility to set the constant duration_class. This enables DateTime to return a custom duration object.

So i extended DateTime and changed the constant. But then the problem with DateTime::Format::Strptime occures, i also rely on this package.
This means that it also now needs to return a object of my custom extended DateTime class.

So i applied logic based on the same principles that is in the DateTime core class around returning a custom Duration object to the DateTime::Format::Strptime package.
I choose the same solution principles in order to be consistent through the hole package namespace.

I thought that most likely some other people have also had this desire, and that it could be that others want to do the same as me.
So i therefor humbly ask you to take a look at my small diff, and to consider if that is something you could include in the package.

Attached is a small and very simple test file, and the modified Strptime.pm

(A full diff is below)

If you think this is a useful feature but want it implemented in another way, please contact me with your comments, and i will see what i can do.

Best regards
Joakim

[root@localhost Format]# diff Strptime.pm /tmp/DateTime-Format-Strptime-1.5000/lib/DateTime/Format/Strptime.pm
22,23d21
< use constant core_datetime_class => 'DateTime';
<
389c387
<         $epoch_dt = $self->core_datetime_class->from_epoch(
---
>         $epoch_dt = DateTime->from_epoch(
513c511
<             $self->core_datetime_class->from_day_of_year(
---
>             DateTime->from_day_of_year(
559c557
<             $self->core_datetime_class->new(
---
>             DateTime->new(
665c663
<         $self->core_datetime_class->new(
---
>         DateTime->new(
1020,1021c1018,1019
<     $regex      =~ s|%{(\w+)}|($self->core_datetime_class->can($1)) ? "(.+)" : ".+"|eg;
<     $field_list =~ s|(%{(\w+)})|($self->core_datetime_class->can($2)) ? "#$2#" : $1 |eg;
---
>     $regex      =~ s|%{(\w+)}|(DateTime->can($1)) ? "(.+)" : ".+"|eg;
>     $field_list =~ s|(%{(\w+)})|(DateTime->can($2)) ? "#$2#" : $1 |eg;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions