Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found