| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Netstack::Utils::Date; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 4 |  |  |  |  |  |  | # 加载扩展插件 | 
| 5 |  |  |  |  |  |  | #------------------------------------------------------------------------------ | 
| 6 | 1 |  |  | 1 |  | 66132 | use 5.016; | 
|  | 1 |  |  |  |  | 12 |  | 
| 7 | 1 |  |  | 1 |  | 591 | use Moose; | 
|  | 1 |  |  |  |  | 509168 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 8 | 1 |  |  | 1 |  | 8775 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 8952 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub getLocalDate { | 
| 11 | 2 |  |  | 2 | 0 | 20 | my ( $self, @param ) = @_; | 
| 12 |  |  |  |  |  |  | # 初始化变量 | 
| 13 | 2 |  |  |  |  | 4 | my ( $format, $time ); | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 2 | 100 | 66 |  |  | 14 | if ( defined $param[0] and $param[0] =~ /^\d+$/ ) { | 
| 16 | 1 |  |  |  |  | 3 | ( $time, $format ) = @param; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  | else { | 
| 19 | 1 |  |  |  |  | 3 | ( $format, $time ) = @param; | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  | # 缺省时间格式 | 
| 22 | 2 | 50 |  |  |  | 19 | if ( not defined $format ) { | 
| 23 | 2 |  |  |  |  | 6 | $format = 'yyyy-mm-dd hh:mi:ss'; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  | # 缺省为本地时间 | 
| 26 | 2 | 100 |  |  |  | 5 | if ( not defined $time ) { | 
| 27 | 1 |  |  |  |  | 2 | $time = time(); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | # 时间对象切片 | 
| 30 | 2 |  |  |  |  | 93 | my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime($time); | 
| 31 |  |  |  |  |  |  | # 定义本地时间数据字典 | 
| 32 | 2 |  |  |  |  | 21 | my %timeMap = ( | 
| 33 |  |  |  |  |  |  | yyyy => $year + 1900, | 
| 34 |  |  |  |  |  |  | mm   => $mon + 1, | 
| 35 |  |  |  |  |  |  | dd   => $mday, | 
| 36 |  |  |  |  |  |  | hh   => $hour, | 
| 37 |  |  |  |  |  |  | mi   => $min, | 
| 38 |  |  |  |  |  |  | ss   => $sec, | 
| 39 |  |  |  |  |  |  | ); | 
| 40 | 2 |  |  |  |  | 18 | my %formatMap = ( | 
| 41 |  |  |  |  |  |  | yyyy => '%04d', | 
| 42 |  |  |  |  |  |  | mm   => '%02d', | 
| 43 |  |  |  |  |  |  | dd   => '%02d', | 
| 44 |  |  |  |  |  |  | hh   => '%02d', | 
| 45 |  |  |  |  |  |  | mi   => '%02d', | 
| 46 |  |  |  |  |  |  | ss   => '%02d', | 
| 47 |  |  |  |  |  |  | ); | 
| 48 | 2 |  |  |  |  | 12 | my $regex = '(' . join( '|', keys %timeMap ) . ')'; | 
| 49 | 2 |  |  |  |  | 93 | my @times = map { $timeMap{$_} } ( $format =~ /$regex/g ); | 
|  | 12 |  |  |  |  | 25 |  | 
| 50 | 2 | 50 |  |  |  | 10 | if ( scalar(@times) == 0 ) { | 
| 51 | 0 |  |  |  |  | 0 | confess "ERROR: format string [$format]  has none valid characters\n"; | 
| 52 |  |  |  |  |  |  | } | 
| 53 | 2 |  |  |  |  | 65 | $format =~ s/$regex/$formatMap{$1}/g; | 
| 54 | 2 |  |  |  |  | 16 | my $localTime = sprintf( "$format", @times ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # 返回计算结果 | 
| 57 | 2 |  |  |  |  | 64 | return $localTime; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 61 |  |  |  |  |  |  | 1; |