cvsimport: work around perl tzset issue
On many platforms, the first invocation of localtime_r will check $TZ in the environment, but subsequent invocations will use a cached value. That means that setting $ENV{TZ} in the middle of the program may or may not have an effect on later calls to localtime. Perl 5.10.0 and later handles this automatically for us, but we try to remain portable back to 5.8. Work around it by calling tzset ourselves.
Jeff King committed
Nov 4, 2012 at 07:25 UTC
c2b3af0537e0b2c7624913b0f26191e992beb12c
1 file changed
+13
-4
git-cvsimport.perl
+13
-4
@@ -24,11 +24,11 @@ use File::Basename qw(basename dirname);
24
use Time::Local;
25
use IO::Socket;
26
use IO::Pipe;
27
-use POSIX qw(strftime dup2 ENOENT);
27
+use POSIX qw(strftime tzset dup2 ENOENT);
28
use IPC::Open2;
29
30
$SIG{'PIPE'}="IGNORE";
31
-$ENV{'TZ'}="UTC";
31
+set_timezone('UTC');
32
33
our ($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,@opt_M,$opt_A,$opt_S,$opt_L, $opt_a, $opt_r, $opt_R);
34
my (%conv_author_name, %conv_author_email, %conv_author_tz);
@@ -99,6 +99,15 @@ sub write_author_info($) {
99
close ($f);
100
}
101
102
+# Versions of perl before 5.10.0 may not automatically check $TZ each
103
+# time localtime is run (most platforms will do so only the first time).
104
+# We can work around this by using tzset() to update the internal
105
+# variable whenever we change the environment.
106
+sub set_timezone {
107
+ $ENV{TZ} = shift;
108
+ tzset();
109
+}
110
+
111
# convert getopts specs for use by git config
112
my %longmap = (
113
'A:' => 'authors-file',
@@ -854,9 +863,9 @@ sub commit {
863
}
864
}
865
857
- $ENV{'TZ'}=$author_tz;
866
+ set_timezone($author_tz);
867
my $commit_date = strftime("%s %z", localtime($date));
859
- $ENV{'TZ'}="UTC";
868
+ set_timezone('UTC');
869
$ENV{GIT_AUTHOR_NAME} = $author_name;
870
$ENV{GIT_AUTHOR_EMAIL} = $author_email;
871
$ENV{GIT_AUTHOR_DATE} = $commit_date;