Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/DBIx/Class/InflateColumn/DateTime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ If you want to set a specific timezone and locale for that field, use:
starts_when => { data_type => 'datetime', timezone => "America/Chicago", locale => "de_DE" }
);

If you want to set a specific formatter to use when print value of the inflated
DateTime object to user:

__PACKAGE__->add_columns(
starts_when => { data_type => 'datetime', formatter => DateTime::Format::Pg->new() }
);

If you want the inflated DateTime object to have the detected formatter based on
DBIx::Class::Storage::DBI::* you are using, set it to 'auto':

__PACKAGE__->add_columns(
starts_when => { data_type => 'datetime', formatter => 'auto' }
);

If you want all inflated DateTime objects to have the detected formatter set
DBIC_AUTOSET_DATETIME_FORMATTER to 1.

Please note: this affect only the formatter how DateTime will be formatter to a user.
This does nothing how a value is formatted for a database.

If you want to inflate no matter what data_type your column is,
use inflate_datetime or inflate_date:

Expand Down Expand Up @@ -226,6 +246,12 @@ sub _post_inflate_datetime {

$dt->set_time_zone($info->{timezone}) if defined $info->{timezone};
$dt->set_locale($info->{locale}) if defined $info->{locale};
my $f = defined $info->{formatter} && $info->{formatter}
|| $ENV{DBIC_AUTOSET_DATETIME_FORMATTER} && 'auto';
if( $f ) {
$f = $f eq 'auto' ? $self->_datetime_parser : $f;
$dt->set_formatter( $f );
}

return $dt;
}
Expand Down
6 changes: 6 additions & 0 deletions t/inflate/datetime_pg.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ my $dt = DateTime->new( year => 2000, time_zone => "America/Chicago" );

warnings_are {
my $event = $schema->resultset("EventTZPg")->find(1);

$event->update({created_on => '2009-01-15 17:00:00+00'});
$event->discard_changes;
isa_ok($event->created_on, "DateTime") or diag $event->created_on;
is($event->created_on->time_zone->name, "America/Chicago", "Timezone changed");

# Time zone difference -> -6hours
is($event->created_on->iso8601, "2009-01-15T11:00:00", "Time with TZ correct");

# Test behavior when DateTime has or has not fomatter
is($event->created_on, "2009-01-15T11:00:00", 'Default DateTime does not display timezones');
is($event->starts_at, "2006-04-25 22:24:33-0500", 'Inflated DateTime has initialized formatter');

# test 'timestamp without time zone'
my $dt = DateTime->from_epoch(epoch => time);
$dt->set_nanosecond(int 500_000_000);
Expand Down
9 changes: 7 additions & 2 deletions t/lib/DBICTest/Schema/EventTZPg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ __PACKAGE__->table('event');

__PACKAGE__->add_columns(
id => { data_type => 'integer', is_auto_increment => 1 },
starts_at => { data_type => 'datetime', timezone => "America/Chicago", locale => 'de_DE' },
created_on => { data_type => 'timestamp with time zone', timezone => "America/Chicago" },
starts_at => {
data_type => 'datetime',
timezone => "America/Chicago",
locale => 'de_DE',
formatter => 'auto', # This does not affect formatter used to store values into database
},
created_on => { data_type => 'timestamp with time zone', timezone => "America/Chicago" },
ts_without_tz => { data_type => 'timestamp without time zone' },
);

Expand Down