Hello monks.

I'm looking for any standard way to convert a timestamp from one timezone to another inside perl thread.

I have no problem if this happens in main thread, but if I try to change $ENV{TZ} in any other thread, this gives nothing.

Script to demonstrate the problem:

#!/usr/bin/perl use strict; use warnings; use threads; use POSIX qw(tzset); my $t=localtime(); print "local time: ",$t,"\n"; my ($tr) = threads->create(\&thr_proc); $tr->join; $ENV{TZ}="Europe/Paris"; $t=localtime(); print "main: ",$t,"\n"; $ENV{TZ}="Europe/Moscow"; $t=localtime(); print "main: ",$t,"\n"; sub thr_proc { $ENV{TZ} = 'Europe/Paris'; $t = localtime; print "in thread: ",$t,"\n"; $ENV{TZ} = 'Europe/Moscow'; $t = localtime; print "in thread: ",$t,"\n"; }
Output:
$ ./mt-demo.pl local time: Mon Nov 23 15:14:06 2009 in thread: Mon Nov 23 15:14:06 2009 in thread: Mon Nov 23 15:14:06 2009 main: Mon Nov 23 16:14:06 2009 main: Mon Nov 23 18:14:06 2009
I tried adding of tzset() or some other tricks but with no luck. Here is my perl:
$ perl -V Summary of my perl5 (revision 5 version 10 subversion 0) configuration +: Platform: osname=linux, osvers=2.6.18-128.7.1.el5xen, archname=x86_64-linux- +thread-multi uname='linux testbox-blizzard.didit.com 2.6.18-128.7.1.el5xen #1 s +mp mon aug 24 09:14:33 edt 2009 x86_64 x86_64 x86_64 gnulinux ' config_args='-Duseshrplib -Duseithreads -d' hint=recommended, useposix=true, d_sigaction=define useithreads=define, usemultiplicity=define useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=und +ef use64bitint=define, use64bitall=define, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing + -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=6 +4', optimize='-O2', cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -I +/usr/local/include' ccversion='', gccversion='4.1.2 20080704 (Red Hat 4.1.2-46)', gcco +sandvers='' intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +6 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib /lib64 /usr/lib64 /usr/local/l +ib64 libs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=/lib/libc-2.5.so, so=so, useshrplib=true, libperl=libperl.so gnulibc_version='2.5' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E - +Wl,-rpath,/usr/local/lib/perl5/5.10.0/x86_64-linux-thread-multi/CORE' cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP USE_64_ +BIT_ALL USE_64_BIT_INT USE_ITHREADS USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API Built under linux Compiled at Nov 16 2009 16:44:01 @INC: /usr/local/lib/perl5/5.10.0/x86_64-linux-thread-multi /usr/local/lib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi /usr/local/lib/perl5/site_perl/5.10.0
I'm ready to write some thread-safe TZ manipulation module, but maybe I'm missing something obvious. Thanks.

In reply to Standard way to convert timezone in multithreaded script by whale2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.