1 package DBIx::Class::DateTime::Epoch;
8 use base qw( DBIx::Class );
12 __PACKAGE__->load_components( qw( InflateColumn::DateTime ) );
16 my( $class, @cols ) = @_;
19 while (my $col = shift @cols) {
20 my $info = ref $cols[0] ? shift @cols : {};
22 if( my $type = delete $info->{ epoch } ) {
23 $info->{ inflate_datetime } = 'epoch';
25 if( $type =~ m{^[cm]time$} ) {
26 __PACKAGE__->load_components( 'TimeStamp' );
27 $info->{ set_on_create } = 1;
28 $info->{ set_on_update } = 1 if $type eq 'mtime';
32 push @columns, $col => $info;
36 $class->next::method( @columns );
39 sub _inflate_to_datetime {
40 my( $self, $value, $info, @rest ) = @_;
41 return $self->next::method( $value, $info, @rest )
42 unless $info->{ data_type } =~ m{int}i || (exists $info->{ inflate_datetime } && $info->{ inflate_datetime } eq 'epoch');
44 return DateTime->from_epoch( epoch => $value );
47 sub _deflate_from_datetime {
48 my( $self, $value, $info, @rest ) = @_;
49 return $self->next::method( $value, $info, @rest )
50 unless $info->{ data_type } =~ m{int}i || (exists $info->{ inflate_datetime } && $info->{ inflate_datetime } eq 'epoch');
61 DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects
65 package MySchema::Foo;
67 use base qw( DBIx::Class );
69 __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
70 __PACKAGE__->add_columns(
72 data_type => 'varchar',
75 bar => { # epoch stored as an int
76 data_type => 'bigint',
77 inflate_datetime => 1,
79 baz => { # epoch stored as a string
80 data_type => 'varchar',
82 inflate_datetime => 'epoch',
84 # working in conjunction with DBIx::Class::TimeStamp
86 data_type => 'bigint',
87 inflate_datetime => 1,
90 modification_time => {
91 data_type => 'bigint',
92 inflate_datetime => 1,
100 This module automatically inflates/deflates DateTime objects from/to epoch
101 values for the specified columns. This module is essentially an extension to
102 L<DBIx::Class::InflateColumn::DateTime> so all of the settings, including
103 C<locale> and C<timezone>, are also valid.
105 A column will be recognized as an epoch time given one of the following scenarios:
109 =item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
111 =item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
115 L<DBIx::Class::TimeStamp> can also be used in conjunction with this module to support
116 epoch-based columns that are automatically set on creation of a row and updated subsequent
121 =head2 add_columns( )
123 Provides backwards compatibility with the older DateTime::Epoch API.
125 =head2 _inflate_to_datetime( )
127 Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
129 =head2 _deflate_from_datetime( )
131 Overrides column deflation to call C<epoch()> on the column value.
137 =item * L<DBIx::Class>
139 =item * L<DBIx::Class::TimeStamp>
147 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
149 Adam Paynter E<lt>adapay@cpan.orgE<gt>
151 =head1 COPYRIGHT AND LICENSE
153 Copyright 2006-2011 by Brian Cassidy
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself.