- Timestamp:
- 2011-06-01T12:33:42+12:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/cpan/Image/ExifTool/Shift.pl
r16842 r24107 15 15 #------------------------------------------------------------------------------ 16 16 # apply shift to value in new value hash 17 # Inputs: 0) shift type, 1) shift string, 2) value to shift, 3) new value hash ref17 # Inputs: 0) shift type, 1) shift string, 2) raw date/time value, 3) new value hash ref 18 18 # Returns: error string or undef on success and updates value in new value hash 19 19 sub ApplyShift($$$;$) 20 20 { 21 my ($func, $shift, $val, $n ewValueHash) = @_;21 my ($func, $shift, $val, $nvHash) = @_; 22 22 23 23 # get shift direction from first character in shift string … … 25 25 my $pre = $1; 26 26 my $dir = ($pre eq '+') ? 1 : -1; 27 my $tagInfo = $$n ewValueHash{TagInfo};27 my $tagInfo = $$nvHash{TagInfo}; 28 28 my $tag = $$tagInfo{Name}; 29 my $self = $$n ewValueHash{Self}; # (used in eval)29 my $self = $$nvHash{Self}; # (used in eval) 30 30 my $shiftOffset; 31 if ($$n ewValueHash{ShiftOffset}) {32 $shiftOffset = $$n ewValueHash{ShiftOffset};31 if ($$nvHash{ShiftOffset}) { 32 $shiftOffset = $$nvHash{ShiftOffset}; 33 33 } else { 34 $shiftOffset = $$n ewValueHash{ShiftOffset} = { };34 $shiftOffset = $$nvHash{ShiftOffset} = { }; 35 35 } 36 36 … … 63 63 } 64 64 # update value in new value hash 65 $n ewValueHash->{Value} = [ $val ];65 $nvHash->{Value} = [ $val ]; 66 66 return undef; # success 67 67 } … … 105 105 106 106 #------------------------------------------------------------------------------ 107 # split times into corresponding components: YYYY MM DD hh mm sstzh tzm107 # split times into corresponding components: YYYY mm dd HH MM SS tzh tzm 108 108 # Inputs: 0) date/time or shift string 1) reference to list for returned components 109 109 # 2) optional reference to list of time components (if shift string) … … 152 152 # this is a time (can't come after timezone) 153 153 (defined $v[3] or defined $v[6] or @vals > 3) and $err = 1, last; 154 not $time and @vals != 3 and $err = 1, last;154 not $time and @vals != 3 and @vals != 2 and $err = 1, last; 155 155 $v[3] = shift(@vals); # take hour first if only one specified 156 156 $v[4] = shift(@vals) || 0; … … 175 175 # 2) shift direction, 3) reference to output list of shifted components 176 176 # 4) number of decimal points in seconds 177 # 5) reference to return time difference due to rounding 177 178 # Returns: error string or undef on success 178 sub ShiftComponents($$$$$ )179 sub ShiftComponents($$$$$;$) 179 180 { 180 my ($time, $shift, $dir, $toTime, $dec ) = @_;181 my ($time, $shift, $dir, $toTime, $dec, $rndPt) = @_; 181 182 # min/max for Y, M, D, h, m, s 182 183 my @min = ( 0, 1, 1, 0, 0, 0); … … 203 204 if (defined $sec and $sec != int($sec)) { 204 205 my $mult = 10 ** $dec; 205 $$toTime[5] = int($sec * $mult + 0.5 * ($sec <=> 0)) / $mult; 206 my $rndSec = int($sec * $mult + 0.5 * ($sec <=> 0)) / $mult; 207 $rndPt and $$rndPt = $sec - $rndSec; 208 $$toTime[5] = $rndSec; 206 209 } 207 210 # … … 303 306 $time[6] = $time[7] = 0 if $val =~ s/Z$/\+00:00/; 304 307 } 305 my $err = ShiftComponents(\@time, \@shift, $dir, \@toTime, $dec); 308 my $rndDiff; 309 my $err = ShiftComponents(\@time, \@shift, $dir, \@toTime, $dec, \$rndDiff); 306 310 $err and return $err; 307 311 # … … 324 328 # handle fractional seconds separately 325 329 $diff = $tm2[0] - int($tm2[0]) - ($tm1[0] - int($tm1[0])); 330 $diff += $rndDiff if defined $rndDiff; # un-do rounding 326 331 $tm1[0] = int($tm1[0]); 327 332 $tm2[0] = int($tm2[0]); … … 376 381 $tm += $$shiftOffset{$mode}; # apply the shift 377 382 $tm < 0 and return 'Shift results in negative time'; 378 # strip off fractional seconds after rounding to desired precision 379 # (only round if the result isn't integral) 383 # save fractional seconds in shifted time 380 384 $frac = $tm - int($tm); 381 385 if ($frac) { 382 # round off to required number of decimal points 383 my $tmf = $tm + 0.5 * 10 ** (-$dec); 384 $tm = int($tmf); 385 $frac = $tm - $tmf; 386 $tm = int($tm); 387 # must account for any rounding that could occur 388 $frac + 0.5 * 10 ** (-$dec) >= 1 and ++$tm, $frac = 0; 386 389 } 387 390 @tm = gmtime($tm); … … 491 494 '2:0' - shift 2 hours (time, date/time), or 2 months (date) 492 495 '5:0:0' - shift 5 hours (time, date/time), or 5 years (date) 493 '0:0:1' - shift 1 s ec(time, date/time), or 1 day (date)496 '0:0:1' - shift 1 s (time, date/time), or 1 day (date) 494 497 495 498 If two arguments are given, the date shift is first, followed by the time … … 561 564 philosophy). 562 565 566 =head1 BUGS 567 568 This feature uses the standard time library functions, which typically are 569 limited to dates in the range 1970 to 2038. 570 563 571 =head1 AUTHOR 564 572 565 Copyright 2003-20 07, Phil Harvey (phil at owl.phy.queensu.ca)573 Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca) 566 574 567 575 This library is free software; you can redistribute it and/or modify it
Note:
See TracChangeset
for help on using the changeset viewer.