1 package DBIx::Class::DateTime::Epoch;
8 use base qw( DBIx::Class );
12 __PACKAGE__->load_components( qw( DynamicDefault InflateColumn::DateTime ) );
16 my( $class, $col, $info ) = @_;
18 if( my $type = delete $info->{ epoch } ) {
19 $info->{ inflate_datetime } = 'epoch';
21 if( $type =~ m{^[cm]time$} ) {
22 __PACKAGE__->load_components( 'TimeStamp' );
23 $info->{ dynamic_default_on_create } = 'get_timestamp';
24 $info->{ dynamic_default_on_update } = 'get_timestamp' if $type eq 'mtime';
28 $class->next::method( $col, $info );
31 sub _inflate_to_datetime {
32 my( $self, $value, $info, @rest ) = @_;
33 $self->next::method( $value, $info, @rest )
34 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
36 return DateTime->from_epoch( epoch => $value );
39 sub _deflate_from_datetime {
40 my( $self, $value, $info, @rest ) = @_;
41 $self->next::method( $value, $info, @rest )
42 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
53 DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects
57 package MySchema::Foo;
59 use base qw( DBIx::Class );
61 __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
62 __PACKAGE__->add_columns(
64 data_type => 'varchar',
67 bar => { # epoch stored as an int
68 data_type => 'bigint',
69 inflate_datetime => 1,
71 baz => { # epoch stored as a string
72 data_type => 'varchar',
74 inflate_datetime => 'epoch',
76 # working in conjunction with DBIx::Class::TimeStamp
78 data_type => 'bigint',
79 inflate_datetime => 1,
82 modification_time => {
83 data_type => 'bigint',
84 inflate_datetime => 1,
92 This module automatically inflates/deflates DateTime objects from/to epoch
93 values for the specified columns. This module is essentially an extension to
94 L<DBIx::Class::InflateColumn::DateTime> so all of the settings, including
95 C<locale> and C<timezone>, are also valid.
97 A column will be recognized as an epoch time given one of the following scenarios:
101 =item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
103 =item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
107 L<DBIx::Class::TimeStamp> can also be used in conjunction with this module to support
108 epoch-based columns that are automatically set on creation of a row and updated subsequent
113 =head2 _inflate_to_datetime( )
115 Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
117 =head2 _deflate_from_datetime( )
119 Overrides column deflation to call C<epoch()> on the column value.
127 =item * DBIx::Class::TimeStamp
135 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
137 Adam Paynter E<lt>adapay@cpan.orgE<gt>
139 =head1 COPYRIGHT AND LICENSE
141 Copyright 2006-2009 by Brian Cassidy
143 This library is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.