File Coverage

blib/lib/Armadito/Agent/Tools/Time.pm
Criterion Covered Total %
statement 24 59 40.6
branch 0 18 0.0
condition n/a
subroutine 8 13 61.5
pod 4 4 100.0
total 36 94 38.3


line stmt bran cond sub pod time code
1             package Armadito::Agent::Tools::Time;
2              
3 5     5   2793065 use strict;
  5         7  
  5         128  
4 5     5   12 use warnings;
  5         8  
  5         141  
5 5     5   18 use base 'Exporter';
  5         35  
  5         448  
6 5     5   19 use English qw(-no_match_vars);
  5         7  
  5         39  
7 5     5   4243 use Time::Piece;
  5         32805  
  5         23  
8 5     5   2254 use Date::Calc 'Add_Delta_DHMS';
  5         22673  
  5         325  
9 5     5   29 use Time::Local;
  5         8  
  5         217  
10 5     5   886 use POSIX qw(strftime);
  5         9158  
  5         26  
11              
12             our @EXPORT_OK = qw(
13             computeDuration
14             msFiletimeToUnixTimestamp
15             iso8601ToUnixTimestamp
16             nowToISO8601
17             );
18              
19             # ; Time Start: 2016-11-30 16:04:34
20             # ; Time Finish: 2016-11-30 16:04:37
21              
22             sub computeDuration {
23 0     0 1   my (%params) = @_;
24              
25 0           my $format = '%Y-%m-%d %H:%M:%S';
26 0           my $diff = Time::Piece->strptime( $params{end}, $format ) - Time::Piece->strptime( $params{start}, $format );
27              
28 0           return _secondsToDuration( $diff->seconds );
29             }
30              
31             sub _secondsToDuration {
32 0     0     my $totalsecs = shift;
33 0           my ( $hours, $mins, $secs, $leftover ) = ( 0, 0, 0, 0 );
34              
35 0           $leftover = $totalsecs;
36 0 0         if ( $totalsecs >= 3600 ) {
37 0           $hours = $totalsecs / 3600;
38 0           $leftover = $totalsecs % 3600;
39             }
40              
41 0 0         if ( $leftover >= 60 ) {
42 0           $mins = $leftover / 60;
43 0           $leftover = $leftover % 60;
44             }
45              
46 0           return "PT" . $hours . "H" . $mins . "M" . $leftover . "S";
47             }
48              
49             sub msFiletimeToUnixTimestamp {
50 0     0 1   my ( $vt_filetime, $time_zone ) = @_;
51              
52             # Disregard the 100 nanosecond units (but you could save them for later)
53 0           $vt_filetime = substr( $vt_filetime, 0, 11 );
54              
55 0           my $days = int( $vt_filetime / ( 24 * 60 * 60 ) );
56 0           my $hours = int( ( $vt_filetime % ( 24 * 60 * 60 ) ) / ( 60 * 60 ) );
57 0           my $mins = int( ( $vt_filetime % ( 60 * 60 ) ) / 60 );
58 0           my $secs = $vt_filetime % 60;
59              
60 0           my @date = Add_Delta_DHMS( 1601, 1, 1, 0, 0, 0, $days, $hours, $mins, $secs );
61              
62 0           my ( $year, $mon, $mday, $hour, $min, $sec )
63             = ( $date[0], $date[1], $date[2], $date[3], $date[4], $date[5] );
64              
65 0 0         if ( $time_zone eq "Local" ) {
    0          
66 0           return timelocal( $sec, $min, $hour, $mday, $mon - 1, $year );
67             }
68             elsif ( $time_zone eq "UTC" ) {
69 0           return timegm( $sec, $min, $hour, $mday, $mon - 1, $year );
70             }
71             }
72              
73             sub iso8601ToUnixTimestamp {
74 0     0 1   my ( $vt_iso8601, $time_zone ) = @_;
75              
76 0 0         if ( $vt_iso8601 =~ /(\d{4,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/ms ) {
77 0           my ( $year, $mon, $mday, $hour, $min, $sec ) = ( $1, $2, $3, $4, $5, $6 );
78              
79 0 0         if ( $time_zone eq "Local" ) {
    0          
80 0           return timelocal( $sec, $min, $hour, $mday, $mon - 1, $year );
81             }
82             elsif ( $time_zone eq "UTC" ) {
83 0           return timegm( $sec, $min, $hour, $mday, $mon - 1, $year );
84             }
85             }
86             }
87              
88             sub nowToISO8601 {
89 0     0 1   my ($time_zone) = @_;
90              
91 0 0         if ( $time_zone eq "Local" ) {
    0          
92 0           return strftime "%Y-%m-%d %H:%M:%S", localtime;
93             }
94             elsif ( $time_zone eq "UTC" ) {
95 0           return strftime "%Y-%m-%d %H:%M:%S", gmtime;
96             }
97             }
98              
99             1;
100             __END__
101              
102             =head1 NAME
103              
104             Armadito::Agent::Tools::Time - Tools for time manipulation
105              
106             =head1 DESCRIPTION
107              
108             This module provides some functions for time and date manipulation.
109              
110             =head1 FUNCTIONS
111              
112             =head2 computeDuration(%params)
113              
114             Returns the duration at ISO8601 format.
115              
116             =over
117              
118             =item I<dirpath>
119              
120             Path of the directory to read.
121              
122             =head2 msFiletimeToUnixTimestamp($msfiletime)
123              
124             Converts from Microsoft FileTime format to Unix timestamp.
125              
126             =head2 iso8601ToUnixTimestamp($iso8601datetime)
127              
128             Converts from ISO8601 Date time format to Unix timestamp.
129              
130             =head2 nowToISO8601($timezone)
131              
132             Return ISO8601 formatted string for now for a specified timezone (Local or UTC).
133