473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.datetime.fromtimestamp(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_time(utc, tz=tz_orig):

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

dt = datetime.datetime.fromtimestamp(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 1769

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

Similar topics

16
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,...
11
by: Christian Wilcox | last post by:
Does anyone know of any existing Python implementations of an XForms viewer? Christian
2
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...
9
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...
1
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"...
0
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...
9
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...
0
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: """...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.