473,800 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting windows/win32 timezone from python

I know there are many threads on this topic but I haven't yet found an
answer to this question. I'd like to map from a utc to a local time in
an arbitrary timezone (not necessary the default local timezone).

To do this on UNIX is relatively straightforward by setting the TZ
environment variable (and calling time.tzset()). On Windows, one can
get close to the answer using the pytz module (to load the timezone
based on the standard UNIX names like US/Eastern and Australia/Sydney).
However, the mapping from utc -local time won't have the correct
notion of DST for the utc you specify because the OS only knows about
the machine's true localtime. I believe that the only way to really
get the right answer is to temporarily set the machine's local
timezone, which is the standard UNIX approach.

Here's something that _almost_ works in Windows:

import datetime, pytz
d = datetime.dateti me.fromtimestam p(1143408899,
pytz.timezone(" Australia/Sydney"))
d.strftime("%Y% m%d %H:%M:%S")

-20060327 07:34:59, but it _should_ be 08:34:59

Here's code that does work in UNIX.

import datetime
import os
import time

tz_orig = os.environ['TZ']

# this code only works on unix
def utc_to_date_tim e(utc, tz=tz_orig):

try:
if (tz != tz_orig):
os.environ['TZ'] = tz
time.tzset()

dt = datetime.dateti me.fromtimestam p(utc)
return (dt.strftime("% Y%m%d"), dt.strftime("%H %M%S"))

finally:

if (os.environ['TZ'] != tz_orig):
os.environ['TZ'] = tz_orig
time.tzset()

Thanks,

- Joe

Sep 7 '06 #1
0 1792

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1952
by: Pjer Holton | last post by:
If I were to build a Windows application that is a true standard Windows application in every conceivable way and that adheres to the MS Windows standards as much as possible (installation, GUI, printing, registry, RTF etc.), and if portability to other platforms is only a minor concern, ... would Python be a good choice? What packages, libraries and modules would you recommend for GUI, installation, data storage etc.? Thank you very...
11
3502
by: Christian Wilcox | last post by:
Does anyone know of any existing Python implementations of an XForms viewer? Christian
2
7758
by: Haim Ashkenazi | last post by:
Hi I'm trying to compile pycrypto on windows. I've installed mingw and try to run the command 'python setup.py build -c mingw32 install' and I get this error: $ python setup.py build -c mingw32 install running build running build_py creating build
9
2368
by: Skip Montanaro | last post by:
I'd like to package up my typing watcher script in a bundled form (no separate Python install required) for use on Windows. (Un)fortunately, I don't have access to a Windows computer. Do any of the packaging tools (py2exe, MacMillan's Installer, etc) allow cross-platform bundling? Failing that, can I coax someone into building such a beast for me so I can include it on http://sourceforge.net/projects/watch/? Thanks,
1
6510
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
0
3219
by: helloitsme | last post by:
Hello All! I know there already exists a thread with this topic by Mike! But my problem did not get solved with that.... I have a working code in VB6 that can set the selected timezone for the system. When i converted that code in VB .NET, I am having a problem similar to what Mike was having. When I select my timezone to Eastern Time Zone (or SA Pacific), my code sets my system timezone to 'Bogota, Lima, Quito'. i guess it is becoz both...
9
16161
by: Larry Hale | last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy command; see http://darwinsys.com/file/). Generally, has anybody built this and worked with it under Windows? The only thing I've been able to find is the python-magic module at http://hupp.org/adam/hg/python-magic/. Is this "THE" python-magic module. (It seems to be to me, but obviously I don't know. :)
0
1688
by: AqD | last post by:
Hi all! I just noticed on windows (2003; php 5.2.5) the LOCAL timezone is set to UTC by default. I have never experienced this on linux before... I digged the manual and found this: """ The timezone set using the date_default_timezone_set() function (if any)
2
3325
by: SvenV | last post by:
Hello, I'm looking for a way to set the timezone on a mobile (windows CE4.0). I know there is no specific function in .NET1.1 and by looking on pinvoke.net: HomePage (misc) between coredll I understand that there is no API function to do the trick. The scenario is the following: I have a server which sends out his dateTime information using the following format "yyyyMMdd zzz" . This returns the date + timezone. This reaches the mobile as a...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.