File Coverage

blib/lib/App/Manoc/Utils/Datetime.pm
Criterion Covered Total %
statement 17 19 89.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 26 92.3


line stmt bran cond sub pod time code
1             package App::Manoc::Utils::Datetime;
2             #ABSTRACT: routines for managing timestamps and dates in Manoc
3              
4 3     3   31258 use strict;
  3         7  
  3         750  
5 3     3   15 use warnings;
  3         5  
  3         111  
6              
7             our $VERSION = '2.99.2'; ##TRIAL VERSION
8              
9             BEGIN {
10 3     3   13 use Exporter 'import';
  3         6  
  3         93  
11 3     3   54 our @EXPORT_OK = qw/
12             str2seconds print_timestamp
13             /;
14             }
15              
16 3     3   17 use Carp;
  3         4  
  3         172  
17              
18 3     3   1254 use POSIX qw(strftime);
  3         13929  
  3         12  
19 3     3   5169 use DateTime::Format::RFC3339; # used for parse_datetime
  0            
  0            
20              
21              
22             sub str2seconds {
23             my ( $str, $unit ) = @_;
24             croak "empty input string" unless defined $str;
25              
26             my ( $num, $unit2 ) = $str =~ m/^([+-]?\d+)([smhdwMy]?)$/;
27             if ( $unit && $unit2 ) {
28             warn "multiple units specified ($unit, $unit2)";
29             }
30             $unit //= $unit2;
31             $unit ||= 's';
32              
33             my %map = (
34             's' => 1,
35             'm' => 60,
36             'h' => 3600,
37             'd' => 86400,
38             'w' => 604800,
39             'M' => 2592000,
40             'y' => 31536000
41             );
42             exists $map{$unit} or
43             carp "Couldn't parse '$str'. Unknown unit $unit.";
44              
45             defined($num) or
46             carp "Couldn't parse '$str'. Possible invalid syntax.";
47              
48             return $num * $map{$unit};
49             }
50              
51              
52             sub print_timestamp {
53             my $timestamp = shift @_;
54             defined($timestamp) || croak "Missing timestamp";
55             my @timestamp = localtime($timestamp);
56             return strftime( "%d/%m/%Y %H:%M:%S", @timestamp );
57             }
58              
59              
60             sub parse_datetime {
61             my $str = shift;
62              
63             my $f = DateTime::Format::RFC3339->new();
64             my $dt = $f->parse_datetime($str);
65             }
66              
67             1;
68             # Local Variables:
69             # mode: cperl
70             # indent-tabs-mode: nil
71             # cperl-indent-level: 4
72             # cperl-indent-parens-as-block: t
73             # End:
74              
75             __END__
76              
77             =pod
78              
79             =head1 NAME
80              
81             App::Manoc::Utils::Datetime - routines for managing timestamps and dates in Manoc
82              
83             =head1 VERSION
84              
85             version 2.99.2
86              
87             =head1 FUNCTIONS
88              
89             =head2 str2seconds($str)
90              
91             Parse a string made by an integer and a unit of time (e.g. h for
92             hours, M for months) and returns the equivalent number of seconds.
93              
94             =head2 print_timestamp($timestamp)
95              
96             Convert a unixtime stamp into a "%d/%m/%Y %H:%M:%S" string.
97              
98             =head2 parse_datetime($str)
99              
100             Parse a timestamp formatted according RFC 33339. Return a Date::Time object.
101              
102             =head1 AUTHORS
103              
104             =over 4
105              
106             =item *
107              
108             Gabriele Mambrini <gmambro@cpan.org>
109              
110             =item *
111              
112             Enrico Liguori
113              
114             =back
115              
116             =head1 COPYRIGHT AND LICENSE
117              
118             This software is copyright (c) 2017 by Gabriele Mambrini.
119              
120             This is free software; you can redistribute it and/or modify it under
121             the same terms as the Perl 5 programming language system itself.
122              
123             =cut