From 4c53e1218749e4523b3a9a85fc1dcaaa9763ffd9 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 3 Jun 2014 15:00:54 +0200 Subject: [PATCH] Rose-Attr-Helfer: _as_timestamp MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit …mit verschiedenen Präzisionsstufen und Tests! --- SL/DB/Helper/Attr.pm | 30 +++++++++++++++- t/db_helper/attr_timestamp.t | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 t/db_helper/attr_timestamp.t diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 8c99903d3..4d26d8920 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -33,6 +33,7 @@ sub _make_by_type { _as_percent($package, $name, places => 2) if $type =~ /numeric | real | float/xi; _as_number ($package, $name, places => 0) if $type =~ /int/xi; _as_date ($package, $name) if $type =~ /date | timestamp/xi; + _as_timestamp($package, $name) if $type =~ /timestamp/xi; _as_bool_yn($package, $name) if $type =~ /bool/xi; } @@ -105,6 +106,32 @@ sub _as_date { return 1; } +sub _as_timestamp { + my $package = shift; + my $attribute = shift; + my %params = @_; + + my $accessor = sub { + my ($precision, $self, $string) = @_; + + $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2; + + my $dt = $self->$attribute; + return undef unless $dt; + + $dt = DateTime->now if !ref($dt) && ($dt eq 'now'); + + return $::locale->format_date_object($dt, precision => $precision); + }; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_timestamp' } = sub { $accessor->('minute', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_s' } = sub { $accessor->('second', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) }; + + return 1; +} + sub _as_bool_yn { my ($package, $attribute, %params) = @_; @@ -158,6 +185,7 @@ None yet. =head1 AUTHOR -Sven Schöling +Sven Schöling , +Moritz Bunkus =cut diff --git a/t/db_helper/attr_timestamp.t b/t/db_helper/attr_timestamp.t new file mode 100644 index 000000000..86a1678f7 --- /dev/null +++ b/t/db_helper/attr_timestamp.t @@ -0,0 +1,66 @@ +package AttrTimestampTestDummy; + +use base qw(SL::DB::Object); + +__PACKAGE__->meta->setup( + table => 'dummy', + columns => [ dummy => { type => 'timestamp' }, ] +); + +use SL::DB::Helper::Attr; + +package main; + +use strict; + +use Test::More; + +use lib 't'; +use utf8; + +use Support::TestSetup; + +sub new { return AttrTimestampTestDummy->new(@_) } +sub new_dt { DateTime->new(year => 2014, month => 5, day => 31, hour => 23, minute => 9, second => 8, nanosecond => 12000000) } + +Support::TestSetup::login(); + +$::myconfig{dateformat} = 'dd.mm.yy'; +$::myconfig{numberformat} = '1.000,00'; + +is(new->dummy, undef, 'uninitialized: raw'); +is(new->dummy_as_timestamp, undef, 'uninitialized: as_timestamp'); +is(new->dummy_as_timestamp_s, undef, 'uninitialized: as_timestamp_s'); +is(new->dummy_as_timestamp_ms, undef, 'uninitialized: as_timestamp_ms'); + +is(new(dummy => new_dt())->dummy, new_dt(), 'initialized with DateTime, raw'); +is(new(dummy => new_dt())->dummy_as_timestamp, '31.05.2014 23:09', 'initialized with DateTime: as_timestamp'); +is(new(dummy => new_dt())->dummy_as_timestamp_s, '31.05.2014 23:09:08', 'initialized with DateTime: as_timestamp_s'); +is(new(dummy => new_dt())->dummy_as_timestamp_ms, '31.05.2014 23:09:08,012', 'initialized with DateTime: as_timestamp_ms'); + +is(new(dummy_as_timestamp => '31.05.2014')->dummy, new_dt()->truncate(to => 'day'), 'initialized with string: as_timestamp, precision day'); +is(new(dummy_as_timestamp => '31.05.2014 23')->dummy, new_dt()->truncate(to => 'hour'), 'initialized with string: as_timestamp, precision hour'); +is(new(dummy_as_timestamp => '31.05.2014 23:9')->dummy, new_dt()->truncate(to => 'minute'), 'initialized with string: as_timestamp, precision minute'); +is(new(dummy_as_timestamp => '31.05.2014 23:9:8')->dummy, new_dt()->truncate(to => 'second'), 'initialized with string: as_timestamp, precision second'); +is(new(dummy_as_timestamp => '31.05.2014 23:9:8,012')->dummy, new_dt(), 'initialized with string: as_timestamp, precision millisecond'); + +is(new(dummy_as_timestamp_s => '31.05.2014')->dummy, new_dt()->truncate(to => 'day'), 'initialized with string: as_timestamp_s, precision day'); +is(new(dummy_as_timestamp_s => '31.05.2014 23')->dummy, new_dt()->truncate(to => 'hour'), 'initialized with string: as_timestamp_s, precision hour'); +is(new(dummy_as_timestamp_s => '31.05.2014 23:9')->dummy, new_dt()->truncate(to => 'minute'), 'initialized with string: as_timestamp_s, precision minute'); +is(new(dummy_as_timestamp_s => '31.05.2014 23:9:8')->dummy, new_dt()->truncate(to => 'second'), 'initialized with string: as_timestamp_s, precision second'); +is(new(dummy_as_timestamp_s => '31.05.2014 23:9:8,012')->dummy, new_dt(), 'initialized with string: as_timestamp_s, precision millisecond'); + +is(new(dummy_as_timestamp_ms => '31.05.2014')->dummy, new_dt()->truncate(to => 'day'), 'initialized with string: as_timestamp_ms, precision day'); +is(new(dummy_as_timestamp_ms => '31.05.2014 23')->dummy, new_dt()->truncate(to => 'hour'), 'initialized with string: as_timestamp_ms, precision hour'); +is(new(dummy_as_timestamp_ms => '31.05.2014 23:9')->dummy, new_dt()->truncate(to => 'minute'), 'initialized with string: as_timestamp_ms, precision minute'); +is(new(dummy_as_timestamp_ms => '31.05.2014 23:9:8')->dummy, new_dt()->truncate(to => 'second'), 'initialized with string: as_timestamp_ms, precision second'); +is(new(dummy_as_timestamp_ms => '31.05.2014 23:9:8,012')->dummy, new_dt(), 'initialized with string: as_timestamp_ms, precision millisecond'); + +my $item = new(); +is($item->dummy_as_timestamp_ms('31.05.2014'), '31.05.2014 00:00:00,000', 'return value of accessor as_timestamp_ms, precision day'); +is($item->dummy_as_timestamp_ms('31.05.2014 23'), '31.05.2014 23:00:00,000', 'return value of accessor as_timestamp_ms, precision hour'); +is($item->dummy_as_timestamp_ms('31.05.2014 23:9'), '31.05.2014 23:09:00,000', 'return value of accessor as_timestamp_ms, precision minute'); +is($item->dummy_as_timestamp_ms('31.05.2014 23:9:8'), '31.05.2014 23:09:08,000', 'return value of accessor as_timestamp_ms, precision second'); +is($item->dummy_as_timestamp_ms('31.05.2014 23:9:8,012'), '31.05.2014 23:09:08,012', 'return value of accessor as_timestamp_ms, precision millisecond'); + +done_testing(); -- 2.20.1