--- /dev/null
+package DBIx::Class::DateTime::Epoch;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use base qw( DBIx::Class );\r
+\r
+use DateTime;\r
+\r
+__PACKAGE__->mk_classdata( ctime_columns => [ ] );\r
+__PACKAGE__->mk_classdata( mtime_columns => [ ] );\r
+\r
+sub register_column {\r
+ my( $class, $col, $info ) = @_;\r
+ $class->next::method( $col, $info );\r
+ \r
+ if( my $type = $info->{ datetime } ) {\r
+ $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime';\r
+ $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime';\r
+ \r
+ $class->inflate_column(\r
+ $col => {\r
+ inflate => sub { DateTime->from_epoch( epoch => shift ) },\r
+ deflate => sub { shift->epoch }\r
+ }\r
+ );\r
+ }\r
+}\r
+\r
+sub insert {\r
+ my $self = shift;\r
+ my $time = time;\r
+ \r
+ for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) {\r
+ next if defined $self->get_column( $column );\r
+ $self->store_column( $column => $time );\r
+ }\r
+\r
+ $self->next::method( @_ );\r
+}\r
+\r
+sub update {\r
+ my $self = shift;\r
+ my $time = time;\r
+ \r
+ for my $column ( @{ $self->mtime_columns } ) {\r
+ $self->set_column( $column => $time );\r
+ }\r
+ \r
+ $self->next::method( @_ );\r
+}\r
+\r
+1;
\ No newline at end of file