File Coverage

blib/lib/WebService/Cmis/Property/DateTime.pm
Criterion Covered Total %
statement 12 70 17.1
branch 0 26 0.0
condition 0 25 0.0
subroutine 4 8 50.0
pod 3 3 100.0
total 19 132 14.3


line stmt bran cond sub pod time code
1             package WebService::Cmis::Property::DateTime;
2              
3             =head1 NAME
4              
5             WebService::Cmis::Property::DateTime
6              
7             Representation of a propertyDateTime of a cmis object
8              
9             =head1 SYNOPSIS
10              
11             =head1 DESCRIPTION
12              
13             =cut
14              
15 1     1   4474 use strict;
  1         2  
  1         39  
16 1     1   5 use warnings;
  1         3  
  1         28  
17 1     1   7 use WebService::Cmis::Property ();
  1         2  
  1         13  
18 1     1   6 use Time::Local ();
  1         2  
  1         935  
19             our @ISA = qw(WebService::Cmis::Property);
20             our $TZSTRING; # timezone string for servertime; "Z" or "+01:00" etc.
21              
22             =head1 METHODS
23              
24             =over 4
25              
26             =item parse($isoDate) -> $epoch
27              
28             convert the given string into epoch seconds. The date string
29             must be in ISO date format, e.g. 2011-01-18T16:05:54.951+01:00
30              
31             =cut
32              
33             sub parse {
34 0     0 1   my ($this, $isoDate) = @_;
35              
36 0 0         return unless defined $isoDate;
37              
38 0 0         if ($isoDate =~ /(\d\d\d\d)(?:-(\d\d)(?:-(\d\d))?)?(?:T(\d\d)(?::(\d\d)(?::(\d\d(?:\.\d+)?))?)?)?(Z|[-+]\d\d(?::\d\d)?)?/) {
39 0   0       my ($Y, $M, $D, $h, $m, $s, $tz) = ($1, $2 || 1, $3 || 1, $4 || 0, $5 || 0, $6 || 0, $7 || '');
      0        
      0        
      0        
      0        
      0        
40              
41             # strip milliseconds
42 0           $s =~ s/\.\d+$//;
43              
44 0           $M--;
45              
46 0           return Time::Local::timegm($s, $m, $h, $D, $M, $Y).$tz;
47             }
48              
49             # format does not match
50 0           return;
51             }
52              
53             =item unparse($perlValue) $cmisValue
54              
55             converts a perl representation back to a format understood by cmis
56              
57             =cut
58              
59             sub unparse {
60 0     0 1   my ($this, $value) = @_;
61              
62 0 0 0       $value = $this->{value} if ref($this) && !defined $value;
63 0   0       $value ||= 0;
64              
65 0           my $milliseconds;
66 0 0         if ($value =~ s/(\.\d+)$//) {
67 0           $milliseconds = $1;
68             }
69              
70 0 0 0       return 'none' if !defined $value || $value eq '';
71              
72 0           my $tz;
73 0 0         if ($value =~ /^(\d+)(Z|[-+]\d\d(?::\d\d)?)?$/) {
74 0           $value = $1;
75 0   0       $tz = $2 || '';
76             } else {
77 0           return 'none';
78             }
79              
80 0           my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = gmtime($value);
81             #print STDERR "unparsing hour=$hour\n";
82             #print STDERR "isdst=$isdst\n";
83              
84 0           my $formatString = '$year-$mo-$dayT$hour:$min:$sec$isotz';
85              
86 0           $formatString =~ s/\$m(illi)?seco?n?d?s?/sprintf('%.3u',$sec)/gei;
  0            
87 0           $formatString =~ s/\$seco?n?d?s?/sprintf('%.2u',$sec)/gei;
  0            
88 0           $formatString =~ s/\$minu?t?e?s?/sprintf('%.2u',$min)/gei;
  0            
89 0           $formatString =~ s/\$hour?s?/sprintf('%.2u',$hour)/gei;
  0            
90 0           $formatString =~ s/\$day/sprintf('%.2u',$day)/gei;
  0            
91 0           $formatString =~ s/\$mo/sprintf('%.2u',$mon+1)/gei;
  0            
92 0           $formatString =~ s/\$year?/sprintf('%.4u',$year + 1900)/gei;
  0            
93 0           $formatString =~ s/\$isotz/$tz/g;
94              
95 0           return $formatString;
96             }
97              
98             =item getTZString
99              
100             Get timezone offset of the local time from GMT in seconds.
101             Code taken from CPAN module 'Time' - "David Muir Sharnoff disclaims any
102             copyright and puts his contribution to this module in the public domain.
103              
104             =cut
105              
106             sub getTZString {
107              
108             # time zone designator (+hh:mm or -hh:mm)
109 0 0   0 1   unless (defined $TZSTRING) {
110 0           my $offset = _tzOffset();
111 0 0         my $sign = ($offset < 0) ? '-' : '+';
112 0           $offset = abs($offset);
113 0           my $hours = int($offset / 3600);
114 0           my $mins = int(($offset - $hours * 3600) / 60);
115 0 0 0       if ($hours || $mins) {
116 0           $TZSTRING = sprintf("$sign%02d:%02d", $hours, $mins);
117             } else {
118 0           $TZSTRING = 'Z';
119             }
120             }
121              
122 0           return $TZSTRING;
123             }
124              
125             sub _tzOffset {
126 0     0     my $time = time();
127 0           my @l = localtime($time);
128 0           my @g = gmtime($time);
129              
130 0           my $off = $l[0] - $g[0] + ($l[1] - $g[1]) * 60 + ($l[2] - $g[2]) * 3600;
131              
132             # subscript 7 is yday.
133              
134 0 0         if ($l[7] == $g[7]) {
    0          
    0          
    0          
135              
136             # done
137             } elsif ($l[7] == $g[7] + 1) {
138 0           $off += 86400;
139             } elsif ($l[7] == $g[7] - 1) {
140 0           $off -= 86400;
141             } elsif ($l[7] < $g[7]) {
142              
143             # crossed over a year boundary.
144             # localtime is beginning of year, gmt is end
145             # therefore local is ahead
146 0           $off += 86400;
147             } else {
148 0           $off -= 86400;
149             }
150              
151 0           return $off;
152             }
153              
154             =back
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             Copyright 2012-2013 Michael Daum
159              
160             This module is free software; you can redistribute it and/or modify it under
161             the same terms as Perl itself. See F.
162              
163             =cut
164              
165             1;