1 package DBIx::Class::DateTime::Epoch;
8 use base qw( DBIx::Class );
12 __PACKAGE__->load_components( qw/InflateColumn::DateTime/ );
14 sub _inflate_to_datetime {
15 my( $self, $value, $info, @rest ) = @_;
16 $self->next::method( $value, $info, @rest )
17 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
19 return DateTime->from_epoch( epoch => $value );
22 sub _deflate_from_datetime {
23 my( $self, $value, $info, @rest ) = @_;
24 $self->next::method( $value, $info, @rest )
25 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
36 DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects
40 package MySchema::Foo;
42 use base qw( DBIx::Class );
44 __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
45 __PACKAGE__->add_columns(
47 data_type => 'varchar',
50 bar => { # epoch stored as an int
51 data_type => 'bigint',
52 inflate_datetime => 1,
54 baz => { # epoch stored as a string
55 data_type => 'varchar',
57 inflate_datetime => 'epoch',
59 # working in conjunction with DBIx::Class::TimeStamp
61 data_type => 'bigint',
62 inflate_datetime => 1,
65 modification_time => {
66 data_type => 'bigint',
67 inflate_datetime => 1,
75 This module automatically inflates/deflates DateTime objects from/to epoch
76 values for the specified columns. This module is essentially an extension to
77 L<DBIx::Class::InflateColumn::DateTime> so all of the settings, including
78 C<locale> and C<timezone>, are also valid.
80 A column will be recognized as an epoch time given one of the following scenarios:
84 =item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
86 =item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
90 L<DBIx::Class::TimeStamp> can also be used in conjunction with this module to support
91 epoch-based columns that are automatically set on creation of a row and updated subsequent
96 =head2 _inflate_to_datetime( )
98 Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
100 =head2 _deflate_from_datetime( )
102 Overrides column deflation to call C<epoch()> on the column value.
110 =item * DBIx::Class::TimeStamp
118 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
120 Adam Paynter E<lt>adapay@cpan.orgE<gt>
122 =head1 COPYRIGHT AND LICENSE
124 Copyright 2006-2009 by Brian Cassidy
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.