File Coverage

blib/lib/Mojo/Hakkefuin/Utils.pm
Criterion Covered Total %
statement 37 37 100.0
branch 10 14 71.4
condition 3 6 50.0
subroutine 6 6 100.0
pod 3 3 100.0
total 59 66 89.3


line stmt bran cond sub pod time code
1             package Mojo::Hakkefuin::Utils;
2 8     8   48 use Mojo::Base -base;
  8         18  
  8         52  
3              
4 8     8   3703 use Mojo::Date;
  8         34697  
  8         67  
5 8     8   4268 use String::Random;
  8         14935  
  8         4766  
6              
7             sub gen_cookie {
8 27     27 1 247 my ($self, $num) = @_;
9 27   50     83 $num //= 3;
10 27         84 state $random = String::Random->new;
11 27         360 $random->randpattern('CnCCcCCnCn' x $num);
12             }
13              
14             sub sql_datetime {
15 35     35 1 109 my ($self, $time_plus) = @_;
16 35   50     98 $time_plus //= 0;
17 35         69 my $epoch = time + $time_plus;
18 35         148 my $to_get_dt = Mojo::Date->new($epoch)->to_datetime;
19 35         1776 $to_get_dt =~ qr/^([0-9\-]+)\w([0-9\:]+)(.*)/;
20 35         210 return $1 . ' ' . $2;
21             }
22              
23             sub time_convert {
24 42     42 1 441 my ($self, $abbr) = @_;
25              
26             # Reset shortening time
27 42   50     92 $abbr //= '1h';
28 42         292 $abbr =~ qr/^([\d.]+)(\w)/;
29              
30             # Set standard of time units
31 42         85 my $minute = 60;
32 42         61 my $hour = 60 * 60;
33 42         63 my $day = 24 * $hour;
34 42         58 my $week = 7 * $day;
35 42         73 my $month = 30 * $day;
36 42         59 my $year = 12 * $month;
37              
38             # Calculate by time units.
39 42         54 my $identifier;
40 42 50       156 $identifier = int $1 * 1 if $2 eq 's';
41 42 100       99 $identifier = $1 * $minute if $2 eq 'm';
42 42 100       118 $identifier = $1 * $hour if $2 eq 'h';
43 42 50       98 $identifier = $1 * $day if $2 eq 'd';
44 42 100       123 $identifier = $1 * $week if $2 eq 'w';
45 42 50       146 $identifier = $1 * $month if $2 eq 'M';
46 42 50       93 $identifier = $1 * $year if $2 eq 'y';
47 42         196 return $identifier;
48             }
49              
50             1;
51              
52             =encoding utf8
53              
54             =head1 NAME
55              
56             Mojo::Hakkefuin::Utils - Utilities
57              
58             =head1 SYNOPSIS
59              
60             use Mojo::Hakkefuin::Utils;
61            
62             my $utils = Mojo::Hakkefuin::Utils->new;
63            
64             # to generate cookie
65             my $cookie = $utils->gen_cookie;
66            
67             # to generate sql time
68             my $sql_time = $utils->sql_datetime;
69            
70             # to generate time by the abbreviation
71             my $time = $utils->time_convert('1d');
72              
73             =head1 DESCRIPTION
74              
75             General utilities which used on Backend and plugin class.
76              
77             =head1 METHODS
78              
79             L inherits all methods from L and implements the
80             following new ones.
81              
82             =head2 gen_cookie
83              
84             my $cookie = $utils->gen_cookie;
85             my $cookie = $utils->gen_cookie(3);
86            
87             This method only generate cookie login.
88              
89             =head2 sql_datetime
90              
91             my $sql_time = $utils->sql_datetime;
92             my $sql_time = $utils->sql_datetime(60 * 60);
93            
94             This method only generate datetime.
95              
96             =head2 time_convert
97              
98             # To get 1 hour in units of seconds.
99             my $time = $utils->time_convert;
100            
101             # time specified by the abbreviation
102             my $time = $utils->time_convert('1d');
103            
104             Abbreviation of time :
105              
106             s = seconds.
107             m = minutes
108             h = hours
109             d = days
110             w = weeks
111             M = months
112             y = years
113            
114             =head1 SEE ALSO
115              
116             =over 1
117              
118             =item * L
119              
120             =item * L
121              
122             =item * L
123              
124             =item * L
125              
126             =item * L
127              
128             =item * L
129              
130             =item * L
131              
132             =back
133              
134             =cut