Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 4c53e121

Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt

  • ID 4c53e1218749e4523b3a9a85fc1dcaaa9763ffd9
  • Vorgänger 646cb2aa
  • Nachfolger f7c4665f

Rose-Attr-Helfer: _as_timestamp

…mit verschiedenen Präzisionsstufen und Tests!

Unterschiede anzeigen:

SL/DB/Helper/Attr.pm
33 33
  _as_percent($package, $name, places =>  2) if $type =~ /numeric | real | float/xi;
34 34
  _as_number ($package, $name, places =>  0) if $type =~ /int/xi;
35 35
  _as_date   ($package, $name)               if $type =~ /date | timestamp/xi;
36
  _as_timestamp($package, $name)             if $type =~ /timestamp/xi;
36 37
  _as_bool_yn($package, $name)               if $type =~ /bool/xi;
37 38
}
38 39

  
......
105 106
  return 1;
106 107
}
107 108

  
109
sub _as_timestamp {
110
  my $package     = shift;
111
  my $attribute   = shift;
112
  my %params      = @_;
113

  
114
  my $accessor    = sub {
115
    my ($precision, $self, $string) = @_;
116

  
117
    $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2;
118

  
119
    my $dt = $self->$attribute;
120
    return undef unless $dt;
121

  
122
    $dt = DateTime->now if !ref($dt) && ($dt eq 'now');
123

  
124
    return $::locale->format_date_object($dt, precision => $precision);
125
  };
126

  
127
  no strict 'refs';
128
  *{ $package . '::' . $attribute . '_as_timestamp'    } = sub { $accessor->('minute',      @_) };
129
  *{ $package . '::' . $attribute . '_as_timestamp_s'  } = sub { $accessor->('second',      @_) };
130
  *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) };
131

  
132
  return 1;
133
}
134

  
108 135
sub _as_bool_yn {
109 136
  my ($package, $attribute, %params) = @_;
110 137

  
......
158 185

  
159 186
=head1 AUTHOR
160 187

  
161
Sven Schöling <s.schoeling@linet-services.de>
188
Sven Schöling <s.schoeling@linet-services.de>,
189
Moritz Bunkus <m.bunkus@linet-services.de>
162 190

  
163 191
=cut
t/db_helper/attr_timestamp.t
1
package AttrTimestampTestDummy;
2

  
3
use base qw(SL::DB::Object);
4

  
5
__PACKAGE__->meta->setup(
6
  table   => 'dummy',
7
  columns => [ dummy => { type => 'timestamp' }, ]
8
);
9

  
10
use SL::DB::Helper::Attr;
11

  
12
package main;
13

  
14
use strict;
15

  
16
use Test::More;
17

  
18
use lib 't';
19
use utf8;
20

  
21
use Support::TestSetup;
22

  
23
sub new    { return AttrTimestampTestDummy->new(@_) }
24
sub new_dt { DateTime->new(year => 2014, month => 5, day => 31, hour => 23, minute => 9, second => 8, nanosecond => 12000000) }
25

  
26
Support::TestSetup::login();
27

  
28
$::myconfig{dateformat}   = 'dd.mm.yy';
29
$::myconfig{numberformat} = '1.000,00';
30

  
31
is(new->dummy,                 undef, 'uninitialized: raw');
32
is(new->dummy_as_timestamp,    undef, 'uninitialized: as_timestamp');
33
is(new->dummy_as_timestamp_s,  undef, 'uninitialized: as_timestamp_s');
34
is(new->dummy_as_timestamp_ms, undef, 'uninitialized: as_timestamp_ms');
35

  
36
is(new(dummy => new_dt())->dummy,                 new_dt(),                  'initialized with DateTime, raw');
37
is(new(dummy => new_dt())->dummy_as_timestamp,    '31.05.2014 23:09',        'initialized with DateTime: as_timestamp');
38
is(new(dummy => new_dt())->dummy_as_timestamp_s,  '31.05.2014 23:09:08',     'initialized with DateTime: as_timestamp_s');
39
is(new(dummy => new_dt())->dummy_as_timestamp_ms, '31.05.2014 23:09:08,012', 'initialized with DateTime: as_timestamp_ms');
40

  
41
is(new(dummy_as_timestamp => '31.05.2014')->dummy,            new_dt()->truncate(to => 'day'),    'initialized with string: as_timestamp, precision day');
42
is(new(dummy_as_timestamp => '31.05.2014 23')->dummy,         new_dt()->truncate(to => 'hour'),   'initialized with string: as_timestamp, precision hour');
43
is(new(dummy_as_timestamp => '31.05.2014 23:9')->dummy,       new_dt()->truncate(to => 'minute'), 'initialized with string: as_timestamp, precision minute');
44
is(new(dummy_as_timestamp => '31.05.2014 23:9:8')->dummy,     new_dt()->truncate(to => 'second'), 'initialized with string: as_timestamp, precision second');
45
is(new(dummy_as_timestamp => '31.05.2014 23:9:8,012')->dummy, new_dt(),                           'initialized with string: as_timestamp, precision millisecond');
46

  
47
is(new(dummy_as_timestamp_s => '31.05.2014')->dummy,            new_dt()->truncate(to => 'day'),    'initialized with string: as_timestamp_s, precision day');
48
is(new(dummy_as_timestamp_s => '31.05.2014 23')->dummy,         new_dt()->truncate(to => 'hour'),   'initialized with string: as_timestamp_s, precision hour');
49
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');
50
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');
51
is(new(dummy_as_timestamp_s => '31.05.2014 23:9:8,012')->dummy, new_dt(),                           'initialized with string: as_timestamp_s, precision millisecond');
52

  
53
is(new(dummy_as_timestamp_ms => '31.05.2014')->dummy,            new_dt()->truncate(to => 'day'),    'initialized with string: as_timestamp_ms, precision day');
54
is(new(dummy_as_timestamp_ms => '31.05.2014 23')->dummy,         new_dt()->truncate(to => 'hour'),   'initialized with string: as_timestamp_ms, precision hour');
55
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');
56
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');
57
is(new(dummy_as_timestamp_ms => '31.05.2014 23:9:8,012')->dummy, new_dt(),                           'initialized with string: as_timestamp_ms, precision millisecond');
58

  
59
my $item = new();
60
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');
61
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');
62
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');
63
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');
64
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');
65

  
66
done_testing();

Auch abrufbar als: Unified diff