Image/ExifTool/Shift.pl

filter: Statementstmt 82.0% 210 / 256 Branchbran 61.9% 119 / 192 Conditioncond 62.5% 65 / 104 MC/DCmcdc 31.6% 19 / 60 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 7 totaltotal 67.1% 421 / 627 CC 135 SCAR 45.3
CC
135
Cov
71%
ShiftTime
179.8
SplitTime
55.3
ShiftComponents
31.9
CRAP 93.0
« Image/ExifTool/WritePhotoshop.pl ↑ Summary Image/ExifTool/WriteIPTC.pl »
1 #------------------------------------------------------------------------------
2 # File: Shift.pl
3 #
4 # Description: ExifTool time shifting routines
5 #
6 # Revisions: 10/28/2005 - P. Harvey Created
7 # 03/13/2019 - PH Added single-argument form of ShiftTime()
8 #------------------------------------------------------------------------------
9  
10 package Image::ExifTool;
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub ShiftTime($;$$$);
15  
16 #------------------------------------------------------------------------------
17 # apply shift to value in new value hash
18 # Inputs: 0) ExifTool ref, 1) shift type, 2) shift string, 3) raw date/time value,
19 # 4) new value hash ref
20 # Returns: error string or undef on success and updates value in new value hash
21 sub ApplyShift($$$$;$)
22 13 ▶{
Subroutine ApplyShift: called CC=12 SCAR=25.5
Pod undocumented
23 13 my ($self, $func, $shift, $val, $nvHash) = @_;
24  
25 # get shift direction from first character in shift string
26 13 ▶ my $pre = ($shift =~ s/^(\+|-)//) ? $1 : '+';
Branch: 1 branch
BranchTrueFalseTotal
$shift =~ s/^(\+|-)// ? : 8 5 13
27 13 ▶ my $dir = ($pre eq '+') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$pre eq '+' ? : 8 5 13
28 13 my $tagInfo = $$nvHash{TagInfo};
29 13 my $tag = $$tagInfo{Name};
30 13 my $shiftOffset;
31 13 ▶ if ($$nvHash{ShiftOffset}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nvHash->{'ShiftOffset'}) { } 0 13 13
32 0 $shiftOffset = $$nvHash{ShiftOffset};
33 } else {
34 13 $shiftOffset = $$nvHash{ShiftOffset} = { };
35 }
36  
37 # initialize handler for eval warnings
38 13 local $SIG{'__WARN__'} = \&SetWarning;
39 13 SetWarning(undef);
40  
41 # shift is applied to ValueConv value, so we must ValueConv-Shift-ValueConvInv
42 13 my ($type, $err);
43 13 foreach $type ('ValueConv','Shift','ValueConvInv') {
44 39 ▶ if ($type eq 'Shift') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'Shift') { } 13 26 39
elsif ($tagInfo->{$type}) { } 4 22 26
45 #### eval ShiftXxx function
46 13 $err = eval "Shift$func(\$val, \$shift, \$dir, \$shiftOffset)";
47 } elsif ($$tagInfo{$type}) {
48 4 my $conv = $$tagInfo{$type};
49 4 ▶ if (ref $conv eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conv eq 'CODE') { } 0 4 4
50 0 $val = &$conv($val, $self);
51 } else {
52 4 ▶ return "Can't handle $type for $tag in ApplyShift()" if ref $$tagInfo{$type};
Branch: 1 branch
BranchTrueFalseTotal
if ref $tagInfo->{$type} 0 4 4
53 #### eval ValueConv/ValueConvInv ($val, $self)
54 4 $val = eval $$tagInfo{$type};
55 }
56 } else {
57 22 next;
58 }
59 # handle errors
60 17 ▶ $err and return $err;
Branch: 1 branch
BranchTrueFalseTotal
$err and return $err 0 17 17
61 17 ▶ $@ and SetWarning($@);
Branch: 1 branch
BranchTrueFalseTotal
$@ and &SetWarning($@) 0 17 17
62 17 ▶ GetWarning() and return CleanWarning();
Branch: 1 branch
BranchTrueFalseTotal
&GetWarning() and return &CleanWarning() 0 17 17
63 }
64 # update value in new value hash
65 13 $nvHash->{Value} = [ $val ];
66 13 return undef; # success
67 }
68  
69 #------------------------------------------------------------------------------
70 # Check date/time shift
71 # Inputs: 0) shift type, 1) shift string (without sign)
72 # Returns: updated shift string, or undef on error (and may update shift)
73 sub CheckShift($$)
74 51 ▶{
Subroutine CheckShift: called CC=4 SCAR=14.2
Pod undocumented
75 51 my ($type, $shift) = @_;
76 51 my $err;
77 51 ▶ if ($type eq 'Time') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'Time') { } 51 0 51
78 51 ▶ return "No shift direction" unless $shift =~ s/^(\+|-)//;
Branch: 1 branch
BranchTrueFalseTotal
unless $shift =~ s/^(\+|-)// 0 51 51
79 # do a test shift to validate the shift string
80 51 my $testTime = '2005:11:02 09:00:13.25-04:00';
81 51 ▶ $err = ShiftTime($testTime, $shift, $1 eq '+' ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '+' ? : 19 32 51
82 } else {
83 0 $err = "Unknown shift type ($type)";
84 }
85 51 return $err;
86 }
87  
88 #------------------------------------------------------------------------------
89 # return the number of days in a month
90 # Inputs: 0) month number (Jan=1, may be outside range), 1) year
91 # Returns: number of days in month
92 sub DaysInMonth($$)
93 49 ▶{
Subroutine DaysInMonth: called CC=7 SCAR=25.2
Pod undocumented
94 49 my ($mon, $year) = @_;
95 49 my @days = (31,28,31,30,31,30,31,31,30,31,30,31);
96 # adjust to the range [0,11]
97 49 ▶ while ($mon < 1) { $mon += 12; --$year; }
Branch: 1 branch
BranchTrueFalseTotal
if ($mon < 1) 3 49 52
98 0 ▶ while ($mon > 12) { $mon -= 12; ++$year; }
Branch: 1 branch
BranchTrueFalseTotal
if ($mon > 12) 0 49 49
99 # return standard number of days unless february on a leap year
100 49 ▶ return $days[$mon-1] unless $mon == 2 and not $year % 4;
Branch: 1 branch
BranchTrueFalseTotal
unless $mon == 2 and not $year % 4 49 0 49
Condition66%2/3
$mon == 2 and not $year % 4
!ll&&!rl&&r
4810
MC/DC0%0/2
$mon == 2 and not $year % 4
$mon == 2 not $year % 4
Truth table66%2/3
A and not(B)
A= $mon == 2B= not $year % 4
ABresult
0X0
100
111
101 # leap years don't occur on even centuries except every 400 years
102 0 ▶ return 29 if $year % 100 or not $year % 400;
Branch: 1 branch
BranchTrueFalseTotal
if $year % 100 or not $year % 400 0 0 0
Condition0%0/3
$year % 100 or not $year % 400
l!l&&r!l&&!r
000
MC/DC0%0/2
$year % 100 or not $year % 400
$year % 100 not $year % 400
Truth table0%0/3
A or not(B)
A= $year % 100B= not $year % 400
ABresult
1X1
011
000
103 0 return 28;
104 }
105  
106 #------------------------------------------------------------------------------
107 # split times into corresponding components: YYYY mm dd HH MM SS tzh tzm
108 # Inputs: 0) date/time or shift string 1) reference to list for returned components
109 # 2) optional reference to list of time components (if shift string)
110 # Returns: true on success
111 # Returned components are 0-Y, 1-M, 2-D, 3-hr, 4-min, 5-sec, 6-tzhr, 7-tzmin
112 sub SplitTime($$;$)
113 126 ▶{
Subroutine SplitTime: called CC=40 SCAR=40.1
Pod undocumented
114 126 my ($val, $vals, $time) = @_;
115 # insert zeros if missing in shift string
116 126 ▶ if ($time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) 60 66 126
117 60 $val =~ s/(^|[-+:\s]):/${1}0:/g;
118 60 $val =~ s/:([:\s]|$)/:0$1/g;
119 }
120 # change dashes to colons in date (for XMP dates)
121 126 ▶ if ($val =~ s/^(\d{4})-(\d{2})-(\d{2})/$1:$2:$3/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/^(\d{4})-(\d{2})-(\d{2})/$1:$2:$3/) 0 126 126
122 0 $val =~ tr/T/ /; # change 'T' separator to ' '
123 }
124 # add space before timezone to split it into a separate word
125 126 $val =~ s/(\+|-)/ $1/;
126 126 my @words = split ' ', $val;
127 126 my $err = 1;
128 126 my @v;
129 126 for (;;) {
130 386 my $word = shift @words;
131 386 ▶ last unless defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 126 260 386
132 # split word into separate numbers (allow decimal points but no signs)
133 260 ▶ my @vals = $word =~ /(?=\d|\.\d)\d*(?:\.\d*)?/g or last;
Branch: 1 branch
BranchTrueFalseTotal
my(@vals) = $word =~ /(?=\d|\.\d)\d*(?:\.\d*)?/g or last 260 0 260
134 260 ▶ if ($word =~ /^(\+|-)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($word =~ /^(\+|-)/) { } 54 206 260
elsif (@words and $words[0] =~ /^\d+/ or not $time and $vals[0] =~ /^\d{3}/ or $time and not defined $time->[3] and not defined $v[0]) { } 83 123 206
135 # this is the timezone
136 54 ▶ (defined $v[6] or @vals > 2) and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
defined $v[6] || @vals > 2 and $err = 1, last 0 54 54
Condition33%1/3
defined $v[6] or @vals > 2
l!l&&r!l&&!r
0054
MC/DC0%0/2
defined $v[6] or @vals > 2
defined $v[6] @vals > 2
Truth table33%1/3
A or B
A= defined $v[6]B= @vals > 2
ABresult
1X1
011
000
137 54 ▶ my $sign = ($1 ne '-') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ne '-' ? : 1 53 54
138 # apply sign to both minutes and seconds
139 54 $v[6] = $sign * shift(@vals);
140 54 ▶ $v[7] = $sign * (shift(@vals) || 0);
Condition50%1/2
shift @vals || 0
l!l
540
MC/DC0%0/1
shift @vals || 0
shift @vals
Truth table50%1/2
A
A= shift @vals
Aresult
11
00
141 ▶ } elsif ((@words and $words[0] =~ /^\d+/) or # there is a time word to follow
Condition100%18/18
@words and $words[0] =~ /^\d+/ or not $time and $vals[0] =~ /^\d{3}/ or $time and not defined $time->[3] and not defined $v[0]
l!l&&r!l&&!r
821123
@words and $words[0] =~ /^\d+/ or not $time and $vals[0] =~ /^\d{3}/
l!l&&r!l&&!r
802124
@words and $words[0] =~ /^\d+/
!ll&&!rl&&r
725480
not $time and $vals[0] =~ /^\d{3}/
!ll&&!rl&&r
60642
$time and not defined $time->[3] and not defined $v[0]
!ll&&!rl&&r
12211
$time and not defined $time->[3]
!ll&&!rl&&r
64582
MC/DC85%6/7
@words and $words[0] =~ /^\d+/ or not $time and $vals[0] =~ /^\d{3}/ or $time and not defined $time->[3] and not defined $v[0]
@words $words[0] =~ /^\d+/ not $time $vals[0] =~ /^\d{3}/ $time not defined $time->[3] not defined $v[0]
Truth table36%7/19
A and B or not(C) and D or E and not(F) and not(G)
A= @wordsB= $words[0] =~ /^\d+/C= not $timeD= $vals[0] =~ /^\d{3}/E= $timeF= not defined $time->[3]G= not defined $v[0]
ABCDEFGresult
11XXXXX1
0X11XXX1
1011XXX1
0X0X1111
0X101111
100X1111
10101111
0X0X0XX0
0X0X10X0
0X0X1100
0X100XX0
0X1010X0
0X101100
100X0XX0
100X10X0
100X1100
10100XX0
101010X0
10101100
142 (not $time and $vals[0] =~ /^\d{3}/) or # first value is year (3 or more digits)
143 ($time and not defined $$time[3] and not defined $v[0])) # we don't have a time
144 {
145 # this is a date (must come first)
146 83 ▶ (@v or @vals > 3) and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
@v || @vals > 3 and $err = 1, last 0 83 83
Condition33%1/3
@v or @vals > 3
l!l&&r!l&&!r
0083
MC/DC0%0/2
@v or @vals > 3
@v @vals > 3
Truth table33%1/3
A or B
A= @vB= @vals > 3
ABresult
1X1
011
000
147 83 ▶ not $time and @vals != 3 and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
not $time and @vals != 3 and $err = 1, last 0 83 83
Condition66%2/3
not $time and @vals != 3
!ll&&!rl&&r
17660
MC/DC0%0/2
not $time and @vals != 3
not $time @vals != 3
Truth table66%2/3
not(A) and B
A= not $timeB= @vals != 3
ABresult
0X0
100
111
148 83 $v[2] = pop(@vals); # take day first if only one specified
149 83 ▶ $v[1] = pop(@vals) || 0;
Condition100%2/2
pop @vals || 0
l!l
767
MC/DC100%1/1
pop @vals || 0
pop @vals
Truth table100%2/2
A
A= pop @vals
Aresult
11
00
150 83 ▶ $v[0] = pop(@vals) || 0;
Condition100%2/2
pop @vals || 0
l!l
7310
MC/DC100%1/1
pop @vals || 0
pop @vals
Truth table100%2/2
A
A= pop @vals
Aresult
11
00
151 } else {
152 # this is a time (can't come after timezone)
153 123 ▶ (defined $v[3] or defined $v[6] or @vals > 3) and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
defined $v[3] || defined $v[6] || @vals > 3 and $err = 1, last 0 123 123
Condition33%2/6
defined $v[3] or defined $v[6] or @vals > 3
l!l&&r!l&&!r
00123
defined $v[3] or defined $v[6]
l!l&&r!l&&!r
00123
MC/DC0%0/3
defined $v[3] or defined $v[6] or @vals > 3
defined $v[3] defined $v[6] @vals > 3
Truth table25%1/4
A or B or C
A= defined $v[3]B= defined $v[6]C= @vals > 3
ABCresult
1XX1
01X1
0011
0000
154 123 ▶ not $time and @vals != 3 and @vals != 2 and $err = 1, last;
Branch: 1 branch
BranchTrueFalseTotal
not $time and @vals != 3 and @vals != 2 and $err = 1, last 0 123 123
Condition50%3/6
not $time and @vals != 3 and @vals != 2
!ll&&!rl&&r
12300
not $time and @vals != 3
!ll&&!rl&&r
59640
MC/DC0%0/3
not $time and @vals != 3 and @vals != 2
not $time @vals != 3 @vals != 2
Truth table50%2/4
not(A) and B and C
A= not $timeB= @vals != 3C= @vals != 2
ABCresult
0XX0
10X0
1100
1111
155 123 $v[3] = shift(@vals); # take hour first if only one specified
156 123 ▶ $v[4] = shift(@vals) || 0;
Condition100%2/2
shift @vals || 0
l!l
11013
MC/DC100%1/1
shift @vals || 0
shift @vals
Truth table100%2/2
A
A= shift @vals
Aresult
11
00
157 123 ▶ $v[5] = shift(@vals) || 0;
Condition100%2/2
shift @vals || 0
l!l
6459
MC/DC100%1/1
shift @vals || 0
shift @vals
Truth table100%2/2
A
A= shift @vals
Aresult
11
00
158 }
159 260 $err = 0;
160 }
161 126 ▶ return 0 if $err or not @v;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not @v 0 126 126
Condition33%1/3
$err or not @v
l!l&&r!l&&!r
00126
MC/DC0%0/2
$err or not @v
$err not @v
Truth table33%1/3
A or not(B)
A= $errB= not @v
ABresult
1X1
011
000
162 126 ▶ if ($time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) 60 66 126
163 # zero any required shift entries which aren't yet defined
164 60 ▶ $v[0] = $v[1] = $v[2] = 0 if defined $$time[0] and not defined $v[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $time->[0] and not defined $v[0] 43 17 60
Condition66%2/3
defined $time->[0] and not defined $v[0]
!ll&&!rl&&r
01743
MC/DC50%1/2
defined $time->[0] and not defined $v[0]
defined $time->[0] not defined $v[0]
Truth table66%2/3
A and not(B)
A= defined $time->[0]B= not defined $v[0]
ABresult
0X0
100
111
165 60 ▶ $v[3] = $v[4] = $v[5] = 0 if defined $$time[3] and not defined $v[3];
Branch: 1 branch
BranchTrueFalseTotal
if defined $time->[3] and not defined $v[3] 0 60 60
Condition66%2/3
defined $time->[3] and not defined $v[3]
!ll&&!rl&&r
2580
MC/DC0%0/2
defined $time->[3] and not defined $v[3]
defined $time->[3] not defined $v[3]
Truth table66%2/3
A and not(B)
A= defined $time->[3]B= not defined $v[3]
ABresult
0X0
100
111
166 60 ▶ $v[6] = $v[7] = 0 if defined $$time[6] and not defined $v[6];
Branch: 1 branch
BranchTrueFalseTotal
if defined $time->[6] and not defined $v[6] 54 6 60
Condition66%2/3
defined $time->[6] and not defined $v[6]
!ll&&!rl&&r
6054
MC/DC50%1/2
defined $time->[6] and not defined $v[6]
defined $time->[6] not defined $v[6]
Truth table66%2/3
A and not(B)
A= defined $time->[6]B= not defined $v[6]
ABresult
0X0
100
111
167 }
168 126 @$vals = @v; # return split time components
169 126 return 1;
170 }
171  
172 #------------------------------------------------------------------------------
173 # shift date/time by components
174 # Inputs: 0) split date/time list ref, 1) split shift list ref,
175 # 2) shift direction, 3) reference to output list of shifted components
176 # 4) number of decimal points in seconds
177 # 5) reference to return time difference due to rounding
178 # Returns: error string or undef on success
179 sub ShiftComponents($$$$$;$)
180 60 ▶{
Subroutine ShiftComponents: called CC=21 SCAR=34.6
Pod undocumented
181 60 my ($time, $shift, $dir, $toTime, $dec, $rndPt) = @_;
182 # min/max for Y, M, D, h, m, s
183 60 my @min = ( 0, 1, 1, 0, 0, 0);
184 60 my @max = (10000,12,28,24,60,60);
185 60 my $i;
186 #
187 # apply the shift
188 #
189 60 my $c = 0;
190 60 ▶ for ($i=0; $i<@$time; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$time) 462 60 522
191 462 ▶ my $v = ($$time[$i] || 0) + $dir * ($$shift[$i] || 0) + $c;
Condition100%4/4
$time->[$i] || 0
l!l
40854
$shift->[$i] || 0
l!l
120342
MC/DC100%1/1
$time->[$i] || 0
$time->[$i]
MC/DC100%1/1
$shift->[$i] || 0
$shift->[$i]
Truth table100%2/2
A
A= $time->[$i]
Aresult
11
00
Truth table100%2/2
A
A= $shift->[$i]
Aresult
11
00
192 # handle fractional values by propagating remainders downwards
193 462 ▶ if ($v != int($v) and $i < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v != int $v and $i < 5) { } 0 462 462
Condition66%2/3
$v != int $v and $i < 5
!ll&&!rl&&r
411510
MC/DC0%0/2
$v != int $v and $i < 5
$v != int $v $i < 5
Truth table66%2/3
A and B
A= $v != int $vB= $i < 5
ABresult
0X0
100
111
194 0 my $iv = int($v);
195 0 $c = ($v - $iv) * $max[$i+1];
196 0 $v = $iv;
197 } else {
198 462 $c = 0;
199 }
200 462 $$toTime[$i] = $v;
201 }
202 # round off seconds to the required number of decimal points
203 60 my $sec = $$toTime[5];
204 60 ▶ if (defined $sec and $sec != int($sec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sec and $sec != int $sec) 51 9 60
Condition100%3/3
defined $sec and $sec != int $sec
!ll&&!rl&&r
2751
MC/DC100%2/2
defined $sec and $sec != int $sec
defined $sec $sec != int $sec
Truth table100%3/3
A and B
A= defined $secB= $sec != int $sec
ABresult
0X0
100
111
205 51 my $mult = 10 ** $dec;
206 51 my $rndSec = int($sec * $mult + 0.5 * ($sec <=> 0)) / $mult;
207 51 ▶ $rndPt and $$rndPt = $sec - $rndSec;
Branch: 1 branch
BranchTrueFalseTotal
$rndPt and $$rndPt = $sec - $rndSec 51 0 51
208 51 $$toTime[5] = $rndSec;
209 }
210 #
211 # handle overflows, starting with least significant number first (seconds)
212 #
213 60 $c = 0;
214 60 ▶ for ($i=5; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 360 60 420
215 360 ▶ defined $$time[$i] or $c = 0, next;
Branch: 1 branch
BranchTrueFalseTotal
defined $time->[$i] or $c = 0, next 354 6 360
216 # apply shift and adjust for previous overflow
217 354 my $v = $$toTime[$i] + $c;
218 354 $c = 0; # set carry to zero
219 # adjust for over/underflow
220 354 my ($min, $max) = ($min[$i], $max[$i]);
221 354 ▶ if ($v < $min) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v < $min) { } 35 319 354
elsif ($v >= $max + $min) { } 0 319 319
222 35 ▶ if ($i == 2) { # 2 = day of month
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 2) { } 7 28 35
223 7 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$v < 1 and do { do { --$c; my $mon = $$toTime[$i - 1] + $c; $v += &DaysInMonth($mon, $$toTime[$i - 2]) } } 42 7 49
224 # add number of days in previous month
225 49 --$c;
226 49 my $mon = $$toTime[$i-1] + $c;
227 49 $v += DaysInMonth($mon, $$toTime[$i-2]);
228 } while ($v < 1);
229 } else {
230 28 my $fc = ($v - $min) / $max;
231 # carry ($c) must be largest integer equal to or less than $fc
232 28 $c = int($fc);
233 28 ▶ --$c if $c > $fc;
Branch: 1 branch
BranchTrueFalseTotal
if $c > $fc 28 0 28
234 28 $v -= $c * $max;
235 }
236 } elsif ($v >= $max + $min) {
237 0 ▶ if ($i == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 2) { } 0 0 0
238 0 for (;;) {
239 # test against number of days in current month
240 0 my $mon = $$toTime[$i-1] + $c;
241 0 my $days = DaysInMonth($mon, $$toTime[$i-2]);
242 0 ▶ last if $v <= $days;
Branch: 1 branch
BranchTrueFalseTotal
if $v <= $days 0 0 0
243 0 $v -= $days;
244 0 ++$c;
245 0 ▶ last if $v <= 28;
Branch: 1 branch
BranchTrueFalseTotal
if $v <= 28 0 0 0
246 }
247 } else {
248 0 my $fc = ($v - $max - $min) / $max;
249 # carry ($c) must be smallest integer greater than $fc
250 0 $c = int($fc);
251 0 ▶ ++$c if $c <= $fc;
Branch: 1 branch
BranchTrueFalseTotal
if $c <= $fc 0 0 0
252 0 $v -= $c * $max;
253 }
254 }
255 354 $$toTime[$i] = $v; # save the new value
256 }
257 # handle overflows in timezone
258 60 ▶ if (defined $$toTime[6]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $toTime->[6]) 54 6 60
259 54 my $m = $$toTime[6] * 60 + $$toTime[7];
260 54 $m += 0.5 * ($m <=> 0); # avoid round-off errors
261 54 $$toTime[6] = int($m / 60);
262 54 $$toTime[7] = int($m - $$toTime[6] * 60);
263 }
264 60 return undef; # success
265 }
266  
267 #------------------------------------------------------------------------------
268 # Shift an integer or floating-point number
269 # Inputs: 0) date/time string, 1) shift string, 2) shift direction (+1 or -1)
270 # 3) (unused)
271 # Returns: undef and updates input value
272 sub ShiftNumber($$$;$)
273 6 ▶{
Subroutine ShiftNumber: called CC=1 SCAR=0.0
Pod undocumented
274 6 my ($val, $shift, $dir) = @_;
275 6 $_[0] = $val + $shift * $dir; # return shifted value
276 6 return undef; # success!
277 }
278  
279 #------------------------------------------------------------------------------
280 # Shift date/time string
281 # Inputs: 0) date/time string, 1) shift string, 2) shift direction (+1 or -1),
282 # or 0 or undef to take shift direction from sign of shift,
283 # 3) reference to ShiftOffset hash (with Date, DateTime, Time, Timezone keys)
284 # or 0) shift string (and operates on $_)
285 # Returns: error string or undef on success and date/time string is updated
286 sub ShiftTime($;$$$)
287 66 ▶{
Subroutine ShiftTime: called CC=56 SCAR=51.9
Pod undocumented
288 66 my ($val, $shift, $dir, $shiftOffset);
289 66 my (@time, @shift, @toTime, $mode, $needShiftOffset, $dec);
290  
291 66 ▶ if (@_ == 1) { # single argument form of ShiftTime()?
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 66 66
292 0 $val = $_;
293 0 $shift = $_[0];
294 } else {
295 66 ($val, $shift, $dir, $shiftOffset) = @_;
296 }
297 66 ▶ $dir or $dir = ($shift =~ s/^(\+|-)// and $1 eq '-') ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$dir or $dir = $shift =~ s/^(\+|-)// && $1 eq "-" ? -1 : 1 66 0 66
$shift =~ s/^(\+|-)// && $1 eq '-' ? : 0 0 0
Condition0%0/3
$shift =~ s/^(\+|-)// and $1 eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$shift =~ s/^(\+|-)// and $1 eq "-"
$shift =~ s/^(\+|-)// $1 eq "-"
Truth table0%0/3
A and B
A= $shift =~ s/^(\+|-)//B= $1 eq "-"
ABresult
0X0
100
111
298 #
299 # figure out what we are dealing with (time, date or date/time)
300 #
301 66 ▶ SplitTime($val, \@time) or return "Invalid time string ($val)";
Branch: 1 branch
BranchTrueFalseTotal
&SplitTime($val, \@time) or return "Invalid time string ($val)" 66 0 66
302 66 ▶ if (defined $time[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $time[0]) { } 66 0 66
elsif (defined $time[3]) { } 0 0 0
303 66 ▶ return "Can't shift from year 0000" if $time[0] eq '0000';
Branch: 1 branch
BranchTrueFalseTotal
if $time[0] eq "0000" 0 66 66
304 66 ▶ $mode = defined $time[3] ? 'DateTime' : 'Date';
Branch: 1 branch
BranchTrueFalseTotal
defined $time[3] ? : 64 2 66
305 } elsif (defined $time[3]) {
306 0 $mode = 'Time';
307 } else {
308 0 $mode = '';
309 }
310 # get number of digits after the seconds decimal point
311 66 ▶ if (defined $time[5] and $time[5] =~ /\.(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[5] and $time[5] =~ /\.(\d+)/) { } 51 15 66
Condition100%3/3
defined $time[5] and $time[5] =~ /\.(\d+)/
!ll&&!rl&&r
21351
MC/DC100%2/2
defined $time[5] and $time[5] =~ /\.(\d+)/
defined $time[5] $time[5] =~ /\.(\d+)/
Truth table100%3/3
A and B
A= defined $time[5]B= $time[5] =~ /\.(\d+)/
ABresult
0X0
100
111
312 51 $dec = length($1);
313 } else {
314 15 $dec = 0;
315 }
316 66 ▶ if ($shiftOffset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shiftOffset) { } 15 51 66
317 15 ▶ $needShiftOffset = 1 unless defined $$shiftOffset{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shiftOffset->{$mode} 9 6 15
318 15 ▶ $needShiftOffset = 1 if defined $time[6] and not defined $$shiftOffset{Timezone};
Branch: 1 branch
BranchTrueFalseTotal
if defined $time[6] and not defined $shiftOffset->{"Timezone"} 3 12 15
Condition66%2/3
defined $time[6] and not defined $shiftOffset->{"Timezone"}
!ll&&!rl&&r
1203
MC/DC50%1/2
defined $time[6] and not defined $shiftOffset->{"Timezone"}
defined $time[6] not defined $shiftOffset->{"Timezone"}
Truth table66%2/3
A and not(B)
A= defined $time[6]B= not defined $shiftOffset->{"Timezone"}
ABresult
0X0
100
111
319 } else {
320 51 $needShiftOffset = 1;
321 }
322 66 ▶ if ($needShiftOffset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needShiftOffset) { } 60 6 66
323 #
324 # apply date/time shift the hard way
325 #
326 60 ▶ SplitTime($shift, \@shift, \@time) or return "Invalid shift string ($shift)";
Branch: 1 branch
BranchTrueFalseTotal
&SplitTime($shift, \@shift, \@time) or return "Invalid shift string ($shift)" 60 0 60
327  
328 # change 'Z' timezone to '+00:00' only if necessary
329 60 ▶ if (@shift > 6 and @time <= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shift > 6 and @time <= 6) 0 60 60
Condition66%2/3
@shift > 6 and @time <= 6
!ll&&!rl&&r
6540
MC/DC0%0/2
@shift > 6 and @time <= 6
@shift > 6 @time <= 6
Truth table66%2/3
A and B
A= @shift > 6B= @time <= 6
ABresult
0X0
100
111
330 0 ▶ $time[6] = $time[7] = 0 if $val =~ s/Z$/\+00:00/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ s/Z$/+00:00/ 0 0 0
331 }
332 60 my $rndDiff;
333 60 my $err = ShiftComponents(\@time, \@shift, $dir, \@toTime, $dec, \$rndDiff);
334 60 ▶ $err and return $err;
Branch: 1 branch
BranchTrueFalseTotal
$err and return $err 0 60 60
335 #
336 # calculate and save the shift offsets for next time
337 #
338 60 ▶ if ($shiftOffset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shiftOffset) 9 51 60
339 9 ▶ if (defined $time[0] or defined $time[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[0] or defined $time[3]) 9 0 9
Condition33%1/3
defined $time[0] or defined $time[3]
l!l&&r!l&&!r
900
MC/DC0%0/2
defined $time[0] or defined $time[3]
defined $time[0] defined $time[3]
Truth table33%1/3
A or B
A= defined $time[0]B= defined $time[3]
ABresult
1X1
011
000
340 9 my @tm1 = (0, 0, 0, 1, 0, 2000);
341 9 my @tm2 = (0, 0, 0, 1, 0, 2000);
342 9 ▶ if (defined $time[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[0]) 9 0 9
343 9 @tm1[3..5] = reverse @time[0..2];
344 9 @tm2[3..5] = reverse @toTime[0..2];
345 9 --$tm1[4]; # month should start from 0
346 9 --$tm2[4];
347 }
348 9 my $diff = 0;
349 9 ▶ if (defined $time[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[3]) 7 2 9
350 7 @tm1[0..2] = reverse @time[3..5];
351 7 @tm2[0..2] = reverse @toTime[3..5];
352 # handle fractional seconds separately
353 7 $diff = $tm2[0] - int($tm2[0]) - ($tm1[0] - int($tm1[0]));
354 7 ▶ $diff += $rndDiff if defined $rndDiff; # un-do rounding
Branch: 1 branch
BranchTrueFalseTotal
if defined $rndDiff 0 7 7
355 7 $tm1[0] = int($tm1[0]);
356 7 $tm2[0] = int($tm2[0]);
357 }
358 9 eval q{
359 require Time::Local;
360 $diff += Time::Local::timegm(@tm2) - Time::Local::timegm(@tm1);
361 };
362 # not a problem if we failed here since we'll just try again next time,
363 # so don't return error message
364 9 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 9 0 9
365 9 my $mode;
366 9 ▶ if (defined $time[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[0]) { } 9 0 9
367 9 ▶ $mode = defined $time[3] ? 'DateTime' : 'Date';
Branch: 1 branch
BranchTrueFalseTotal
defined $time[3] ? : 7 2 9
368 } else {
369 0 $mode = 'Time';
370 }
371 9 $$shiftOffset{$mode} = $diff;
372 }
373 }
374 9 ▶ if (defined $time[6]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[6]) 3 6 9
375 3 $$shiftOffset{Timezone} = ($toTime[6] - $time[6]) * 60 +
376 $toTime[7] - $time[7];
377 }
378 }
379  
380 } else {
381 #
382 # apply shift from previously calculated offsets
383 #
384 6 ▶ if ($$shiftOffset{Timezone} and @time <= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shiftOffset->{"Timezone"} and @time <= 6) 0 6 6
Condition33%1/3
$shiftOffset->{"Timezone"} and @time <= 6
!ll&&!rl&&r
600
MC/DC0%0/2
$shiftOffset->{"Timezone"} and @time <= 6
$shiftOffset->{"Timezone"} @time <= 6
Truth table33%1/3
A and B
A= $shiftOffset->{"Timezone"}B= @time <= 6
ABresult
0X0
100
111
385 # change 'Z' timezone to '+00:00' only if necessary
386 0 ▶ $time[6] = $time[7] = 0 if $val =~ s/Z$/\+00:00/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ s/Z$/+00:00/ 0 0 0
387 }
388 # apply the previous date/time shift if necessary
389 6 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 6 0 6
390 6 my @tm = (0, 0, 0, 1, 0, 2000);
391 6 ▶ if (defined $time[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[0]) 6 0 6
392 6 @tm[3..5] = reverse @time[0..2];
393 6 --$tm[4]; # month should start from 0
394 }
395 6 ▶ @tm[0..2] = reverse @time[3..5] if defined $time[3];
Branch: 1 branch
BranchTrueFalseTotal
if defined $time[3] 6 0 6
396 # save fractional seconds
397 6 my $frac = $tm[0] - int($tm[0]);
398 6 $tm[0] = int($tm[0]);
399 6 my $tm;
400 6 eval q{
401 require Time::Local;
402 $tm = Time::Local::timegm(@tm) + $frac;
403 };
404 6 ▶ $@ and return CleanWarning($@);
Branch: 1 branch
BranchTrueFalseTotal
$@ and return &CleanWarning($@) 0 6 6
405 6 $tm += $$shiftOffset{$mode}; # apply the shift
406 # save fractional seconds in shifted time
407 6 $frac = $tm - int($tm);
408 6 ▶ if ($frac) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frac) 0 6 6
409 0 $tm = int($tm);
410 # must account for any rounding that could occur
411 0 ▶ $frac + 0.5 * 10 ** (-$dec) >= 1 and ++$tm, $frac = 0;
Branch: 1 branch
BranchTrueFalseTotal
$frac + "0.5" * 10 ** (-$dec) >= 1 and ++$tm, $frac = 0 0 0 0
412 }
413 6 @tm = gmtime($tm);
414 6 @toTime = reverse @tm[0..5];
415 6 $toTime[0] += 1900;
416 6 ++$toTime[1];
417 6 $toTime[5] += $frac; # add the fractional seconds back in
418 }
419 # apply the previous timezone shift if necessary
420 6 ▶ if (defined $time[6]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time[6]) 0 6 6
421 0 my $m = $time[6] * 60 + $time[7];
422 0 $m += $$shiftOffset{Timezone};
423 0 $m += 0.5 * ($m <=> 0); # avoid round-off errors
424 0 $toTime[6] = int($m / 60);
425 0 $toTime[7] = int($m - $toTime[6] * 60);
426 }
427 }
428 #
429 # insert shifted time components back into original string
430 #
431 66 my $i;
432 66 ▶ for ($i=0; $i<@toTime; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @toTime) 444 12 456
433 444 ▶ next unless defined $time[$i] and defined $toTime[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time[$i] and defined $toTime[$i] 0 444 444
Condition33%1/3
defined $time[$i] and defined $toTime[$i]
!ll&&!rl&&r
00444
MC/DC0%0/2
defined $time[$i] and defined $toTime[$i]
defined $time[$i] defined $toTime[$i]
Truth table33%1/3
A and B
A= defined $time[$i]B= defined $toTime[$i]
ABresult
0X0
100
111
434 444 my ($v, $d, $s);
435 444 ▶ if ($i != 6) { # not timezone hours
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 6) { } 390 54 444
436 390 ▶ last unless $val =~ /((?=\d|\.\d)\d*(\.\d*)?)/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /((?=\d|\.\d)\d*(\.\d*)?)/g 0 390 390
437 390 ▶ next if $toTime[$i] == $time[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $toTime[$i] == $time[$i] 249 141 390
438 141 $v = $1; # value
439 141 $d = $2; # decimal part of value
440 141 $s = ''; # no sign
441 } else {
442 54 ▶ last if $time[$i] == $toTime[$i] and $time[$i+1] == $toTime[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
if $time[$i] == $toTime[$i] and $time[$i + 1] == $toTime[$i + 1] 54 0 54
Condition33%1/3
$time[$i] == $toTime[$i] and $time[$i + 1] == $toTime[$i + 1]
!ll&&!rl&&r
0054
MC/DC0%0/2
$time[$i] == $toTime[$i] and $time[$i + 1] == $toTime[$i + 1]
$time[$i] == $toTime[$i] $time[$i + 1] == $toTime[$i + 1]
Truth table33%1/3
A and B
A= $time[$i] == $toTime[$i]B= $time[$i + 1] == $toTime[$i + 1]
ABresult
0X0
100
111
443 0 ▶ last unless $val =~ /((?:\+|-)(?=\d|\.\d)\d*(\.\d*)?)/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /((?:\+|-)(?=\d|\.\d)\d*(\.\d*)?)/g 0 0 0
444 0 $v = $1;
445 0 $d = $2;
446 0 ▶ if ($toTime[6] >= 0 and $toTime[7] >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toTime[6] >= 0 and $toTime[7] >= 0) { } 0 0 0
Condition0%0/3
$toTime[6] >= 0 and $toTime[7] >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$toTime[6] >= 0 and $toTime[7] >= 0
$toTime[6] >= 0 $toTime[7] >= 0
Truth table0%0/3
A and B
A= $toTime[6] >= 0B= $toTime[7] >= 0
ABresult
0X0
100
111
447 0 $s = '+';
448 } else {
449 0 $s = '-';
450 0 $toTime[6] = -$toTime[6];
451 0 $toTime[7] = -$toTime[7];
452 }
453 }
454 141 my $nv = $toTime[$i];
455 141 my $pos = pos $val;
456 141 my $len = length $v;
457 141 my $sig = $len - length $s;
458 141 ▶ my $dec = $d ? length($d) - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 141 141
459 141 ▶ my $newNum = sprintf($dec ? "$s%0$sig.${dec}f" : "$s%0${sig}d", $nv);
Branch: 1 branch
BranchTrueFalseTotal
$dec ? : 0 141 141
460 141 substr($val, $pos - $len, $len) = $newNum;
461 141 pos($val) = $pos + length($newNum) - $len;
462 }
463 66 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 66 66
464 0 $_ = $val; # set $_ to the returned value
465 } else {
466 66 $_[0] = $val; # return shifted value
467 }
468 66 return undef; # success!
469 }
470  
471  
472 3 1; # end
473  
474 __END__
« Image/ExifTool/WritePhotoshop.pl ↑ Summary Image/ExifTool/WriteIPTC.pl »