File Coverage

lib/App/TimeTracker/Utils.pm
Criterion Covered Total %
statement 35 39 89.7
branch 2 6 33.3
condition 1 3 33.3
subroutine 11 12 91.6
pod 0 4 0.0
total 49 64 76.5


line stmt bran cond sub pod time code
1             package App::TimeTracker::Utils;
2              
3             # ABSTRACT: Utility Methods/Functions for App::TimeTracker
4             our $VERSION = '3.010'; # VERSION
5              
6 13     13   93 use strict;
  13         31  
  13         392  
7 13     13   72 use warnings;
  13         26  
  13         311  
8 13     13   258 use 5.010;
  13         48  
9              
10 13     13   80 use Scalar::Util qw(blessed);
  13         27  
  13         752  
11 13     13   7564 use Term::ANSIColor;
  13         106075  
  13         917  
12 13     13   107 use Exporter;
  13         27  
  13         472  
13 13     13   72 use parent qw(Exporter);
  13         26  
  13         94  
14              
15             our @EXPORT = qw();
16             our @EXPORT_OK = qw(pretty_date now error_message warning_message);
17             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
18              
19             sub error_message {
20 1     1 0 4 _message( 'bold red', @_ );
21             }
22              
23             sub warning_message {
24 0     0 0 0 _message( 'bold yellow', @_ );
25             }
26              
27             sub _message {
28 1     1   4 my ( $color, $message, @params ) = @_;
29              
30 1         4 my $string = sprintf( $message, @params );
31              
32 1         6 print color $color;
33 1         107 print $string;
34 1         7 say color 'reset';
35             }
36              
37             sub pretty_date {
38 3     3 0 10 my ($date) = @_;
39              
40 3 50 33     31 unless ( blessed $date
41             && $date->isa('DateTime') )
42             {
43 0         0 return $date;
44             }
45             else {
46 3         11 my $now = now();
47 3         10 my $yesterday = now()->subtract( days => 1 );
48 3 50       3552 if ( $date->dmy eq $now->dmy ) {
    0          
49 3         85 return $date->hms(':');
50             }
51             elsif ( $date->dmy eq $yesterday->dmy ) {
52 0         0 return 'yesterday ' . $date->hms(':');
53             }
54             else {
55 0         0 return $date->dmy('.') . ' ' . $date->hms(':');
56             }
57             }
58             }
59              
60             sub now {
61 20     20 0 153 my $dt = DateTime->now();
62 20         7272 $dt->set_time_zone('local');
63 20         18652 return $dt;
64             }
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             App::TimeTracker::Utils - Utility Methods/Functions for App::TimeTracker
76              
77             =head1 VERSION
78              
79             version 3.010
80              
81             =head1 AUTHOR
82              
83             Thomas Klausner <domm@plix.at>
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is copyright (c) 2011 - 2021 by Thomas Klausner.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.
91              
92             =cut