Revision 4c53e121
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
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 |
Auch abrufbar als: Unified diff
Rose-Attr-Helfer: _as_timestamp
…mit verschiedenen Präzisionsstufen und Tests!