Skip to content

Commit

Permalink
Create DateTime schema helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Oct 18, 2013
1 parent ef185b6 commit ca85705
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for {{$dist->name}}
{{$NEXT}}

- Create clean_rs row shortcut (wreis)
- Create DateTime schema helper (wreis)

2.018004 2013-10-07 15:23:39 America/Chicago
- fix dep marked as test but actually runtime
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ on test => sub {
requires 'Test::Deep' => 0;
requires 'DBD::SQLite' => 0;
requires 'Test::Exception' => 0;
requires 'DateTime::Format::SQLite' => 0;
};
28 changes: 28 additions & 0 deletions lib/DBIx/Class/Helper/Schema/DateTime.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package DBIx::Class::Helper::Schema::DateTime;

# ABSTRACT: DateTime helper

use strict;
use warnings;

# VERSION

sub datetime_parser { return shift->storage->datetime_parser }

sub parse_datetime { return shift->datetime_parser->parse_datetime(@_) }

sub format_datetime { return shift->datetime_parser->format_datetime(@_) }

1;

=head1 SYNOPSIS
package MyApp::Schema;
__PACKAGE__->load_components('Helper::Schema::DateTime');
...
$schema->resultset('Book')->search({
written_on => $schema->format_datetime(DateTime->now)
});
22 changes: 22 additions & 0 deletions t/schema/datetime.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use strict;
use warnings;

use lib 't/lib';
use Test::More;

use TestSchema;
use DateTime;

TestSchema->load_components('Helper::Schema::DateTime');
my $schema = TestSchema->deploy_or_connect();

isa_ok($schema->datetime_parser, 'DateTime::Format::SQLite');
my $dt = DateTime->now;
my $s = $schema->format_datetime($dt);
is(
$schema->format_datetime($schema->parse_datetime($s)),
$schema->format_datetime($dt),
'format_datetime and parse_datetime roundtrip',
);

done_testing;

0 comments on commit ca85705

Please sign in to comment.