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

datetime and daylight savings problem

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 account local
daylight savings. However daylight savings just don't seem to be
working at all...

Python 2.3.5 (#2, May 4 2005, 08:51:39)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from pytz import timezone
from datetime import datetime
t=timezone("Europe/Paris")
utc=timezone("UTC")
d=datetime(2005,01,24,16,59,tzinfo=t)
d datetime.datetime(2005, 1, 24, 16, 59, tzinfo=<DstTzInfo
'Europe/Paris' WET0:00:00 STD>) d.astimezone(utc) datetime.datetime(2005, 1, 24, 16, 59, tzinfo=<StaticTzInfo 'UTC'>) d2=datetime(2005,06,01,16,59,tzinfo=t)
d2 datetime.datetime(2005, 6, 1, 16, 59, tzinfo=<DstTzInfo 'Europe/Paris'
WET0:00:00 STD>) d2.astimezone(utc) datetime.datetime(2005, 6, 1, 16, 59, tzinfo=<StaticTzInfo 'UTC'>)

One of these should be in DST, the other shouldn't, I'm not sure why.
Additional oddness here
d.astimezone(utc).astimezone(t) datetime.datetime(2005, 1, 24, 18, 59, tzinfo=<DstTzInfo
'Europe/Paris' CEST+2:00:00 DST>) d2.astimezone(utc).astimezone(t)

datetime.datetime(2005, 6, 1, 17, 59, tzinfo=<DstTzInfo 'Europe/Paris'
CET+1:00:00 STD>)

I'm not sure if it's just something I'm doing completely wrong here...

James
Oct 11 '05 #1
1 3778
When working with timezones datetime objects are represented in the
tzinfo object you supply, eg. when you define these classes (and run
them on a system set to Paris time):

from datetime import tzinfo, timedelta, datetime
import time

class UTC(tzinfo):
"""UTC timezone"""
def utcoffset(self, dt):
return timedelta(0)

def tzname(self, dt):
return "UTC"

def dst(self, dt):
return timedelta(0)

class CET(tzinfo):
"""CET timezone"""
def utcoffset(self, dt):
return timedelta(seconds = -time.timezone)
def dst(self, dt):
return timedelta(0)
def tzname(self, dt):
return "CET"
class CEST(tzinfo):
"""CET timezone with DST"""
def utcoffset(self, dt):
return timedelta(seconds = -time.altzone)
def dst(self, dt):
return timedelta(seconds = -time.altzone) - \
timedelta(seconds = -time.timezone)
def tzname(self, dt):
return "CEST"

And you create these objects:

utc = UTC()
cet = CET()
cest = CEST()
d = datetime(2005,06,01,16,59,tzinfo=utc)

This statement
print 'UTC %s' % d
Will print:
UTC 2005-06-01 16:59:00+00:00

And this one:
print 'As CET %s' % d.astimezone(cet)
Will print:
As CET 2005-06-01 17:59:00+01:00

And this one:
print 'As CET with DST %s' % d.astimezone(cest)
Will print:
As CET with DST 2005-06-01 18:59:00+02:00

Additional:
This:
d = datetime(2005,06,01,16,59,tzinfo=cet)
print cet, d

Will print:
<__main__.CET object at 0xb7d3aaac> 2005-06-01 16:59:00+01:00

And this:
d = datetime(2005,06,01,16,59,tzinfo=cest)
print cest, d
Will print:
<__main__.CEST object at 0xb7d3aaec> 2005-06-01 16:59:00+02:00

So at least with these tzinfo objects everything is as expected, I'm
not sure where your actual problem is, is it in the pytz module (with
which I do not have experience)?

Oct 11 '05 #2

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

Similar topics

3
by: Bathroom_Monkey | last post by:
For posterity's sake, here is an algorithm I created to take a GMT time and convert it to U.S. central time, accounting for daylight saving time. Note: this algorithm can be modified to work for...
0
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
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...
3
by: chrisdevey | last post by:
Is there any way to make a System.Timers.Timer adjust for daylight savings time change? In a long running process I set a timer as follows for a daily expiration: _myTimer = new...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
3
by: mmuras | last post by:
I did not see any discussions / threads for this but if there is one please let me know: -First, I am one of only two individuals within my company's IT Dept. -We have a Windows Server 2003 R2...
4
by: Polaris431 | last post by:
I have a web application in ASP.NET that will be used globally. Data is collected on mobile devices running Windows Mobile and sent to the web server where it is stored and can be viewed. Data is...
3
by: Generic Usenet Account | last post by:
Hi, Is there any way to make time-of-day adjustments for daylight savings using only standard time functions? We have a program that executes daily at a fixed time of day. After daylight...
1
by: chaosblade | last post by:
Hi, I've come across a small issue related to the DateTime class. It seems to not take daylight savings here (Israel, GMT+2\+3 with DST) into consideration, No matter the form used. I've tried...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.