473,396 Members | 1,707 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,396 software developers and data experts.

datetime, tzinfo ... strange conversion?

Hi!

I'm surprised about the following code, maybe you can give me a hint whether
that's bug or feature? I'm just trying to convert local time to GMT and one
method gives a strange result:

######################
#! /usr/bin/env python

import datetime, pytz

_tz_utc = pytz.timezone("UTC")
_tz_germany = pytz.timezone("Europe/Berlin")

local = datetime.datetime.now(_tz_germany)
print "Local: ", local

gmt = local.astimezone(_tz_utc)
print "GMT (1)", gmt

gmt = datetime.datetime.combine(local.date(), local.timetz()).astimezone(_tz_utc) print "GMT (2)", gmt

gmt = datetime.datetime.combine(datetime.date.today(), datetime.time(local.hour, local.minute, local.second, local.microsecond,
_tz_germany)).astimezone(_tz_utc) print "GMT (3)", gmt

gmt = datetime.datetime.combine(datetime.date.today(), datetime.time(local.hour, local.minute, local.second, local.microsecond,
local.tzinfo)).astimezone(_tz_utc) print "GMT (4)", gmt

print "_tz_germany:", _tz_germany, "(id: %s)" % id(_tz_germany), ", local.tzinfo:", local.tzinfo, "(id: %s)" % id(local.tzinfo)

######################

Result:
tlange@tinux:~/is.work/Scripts$ ./timeconv.py
Local: 2006-04-01 20:09:26.469445+02:00
GMT (1) 2006-04-01 18:09:26.469445+00:00
GMT (2) 2006-04-01 18:09:26.469445+00:00
GMT (3) 2006-04-01 19:09:26.469445+00:00
GMT (4) 2006-04-01 18:09:26.469445+00:00
_tz_germany: Europe/Berlin (id: -1212869684) , local.tzinfo: Europe/Berlin

(id: -1212868756)

--> Why is GMT (3) wrong?

Cheers,

Tino

Apr 1 '06 #1
1 2607
In article <e0**********@newsreader3.netcologne.de>,
Tino Lange <tl*****@nexgo.de> wrote:
--> Why is GMT (3) wrong?


At a guess, it's because there's nothing in the parameters passed to
indicate that daylight saving is currently in effect.

Tip: always do your date/time calculations as far as possible in UTC.
Only convert to local time at the last possible step, before displaying
results to the user.
Apr 3 '06 #2

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

Similar topics

0
by: John Hunter | last post by:
I am using the tzinfo classes from the datetime docs, eg UTC, USTimeZone, and the instantiations Pacific, Central, Eastern, etc. I would like to take a datetime instance in the UTC timezone and...
4
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
3
by: Harald Hanche-Olsen | last post by:
I'm confused. I was going to try linkchecker, and it dies with a traceback ending in File "/usr/local/lib/python2.4/calendar.py", line 32, in _localized_month _months = AttributeError:...
1
by: Adam Monsen | last post by:
Say I have the following datetime object: >>> from datetime import datetime >>> d = datetime(2005, 8, 10, 15, 43) I happen to know this is a local time from the Netherlands, so I assume the...
2
by: Samuel | last post by:
Hello, I am trying to convert a local time into UTC ISO8601, then parse it back into local time. I tried the following: ---------------------- #!/usr/bin/python import time import datetime...
1
by: James | last post by:
I need to import a bunch of data into our database for which there's a single entry each day which occurs at the same time every day in local time - so I need to convert this to UTC taking into...
0
by: David Pratt | last post by:
Hi. I am creating a couple of small methods to help me manage time from UTC as standard but I am getting strange results. If I start with a datetime of 2005-12-12 14:30:00 in timezone...
4
by: Philippe Martin | last post by:
Hi, I need to get the date and time under Windows and Linux but need the information visible to the user (cannot find my words) not the sytem information (ex: a PC setup on greenwich but the...
3
by: insyte | last post by:
With assistance from Gabriel and Frederik (and a few old threads in c.l.p.) I've been making headway on my specialized datetime class. Now I'm puzzled by behavior I didn't expect while attempting...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.