473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Eur ope/Paris")
utc=timezone("U TC")
d=datetime(2005 ,01,24,16,59,tz info=t)
d datetime.dateti me(2005, 1, 24, 16, 59, tzinfo=<DstTzIn fo
'Europe/Paris' WET0:00:00 STD>) d.astimezone(ut c) datetime.dateti me(2005, 1, 24, 16, 59, tzinfo=<StaticT zInfo 'UTC'>) d2=datetime(200 5,06,01,16,59,t zinfo=t)
d2 datetime.dateti me(2005, 6, 1, 16, 59, tzinfo=<DstTzIn fo 'Europe/Paris'
WET0:00:00 STD>) d2.astimezone(u tc) datetime.dateti me(2005, 6, 1, 16, 59, tzinfo=<StaticT zInfo 'UTC'>)

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

datetime.dateti me(2005, 6, 1, 17, 59, tzinfo=<DstTzIn fo '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 3800
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(secon ds = -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(secon ds = -time.altzone)
def dst(self, dt):
return timedelta(secon ds = -time.altzone) - \
timedelta(secon ds = -time.timezone)
def tzname(self, dt):
return "CEST"

And you create these objects:

utc = UTC()
cet = CET()
cest = CEST()
d = datetime(2005,0 6,01,16,59,tzin fo=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(ce t)
Will print:
As CET 2005-06-01 17:59:00+01:00

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

Additional:
This:
d = datetime(2005,0 6,01,16,59,tzin fo=cet)
print cet, d

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

And this:
d = datetime(2005,0 6,01,16,59,tzin fo=cest)
print cest, d
Will print:
<__main__.CES T 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
3666
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 any other U.S. timezone by changing the number of second subtracted at the end. <? $in_dst="false";
0
5159
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 accepts messages from .NET clients. The web method call includes an object as one of it's parameters - this reflected object has been given a property called "FutureDelivery" and expects a DateTime value. The clients may be in different timezones...
1
2782
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 tzinfo (if it were present) should indicate Central European Summer Time ("Summer" indicates daylight savings). How do I convert this date/time information to UTC?
3
3256
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 Timer(_myTimerDelegate, null, nextExpiration, TimeSpan.FromDays(1)); When we make the change out of daylight time back to standard time, the timer appears to fire one hour early (e.g., 4pm instead of 5pm). Is there a way to set this timer so that it is...
9
4927
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:JadeWebServices/WebServiceProvider/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
3
4325
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 Standard Ed. Box for our Domain controller. -We are having issues STILL today in April with the Microsoft Windows Daylight Savings Issues ***Symptoms:
4
4403
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 timestamped when it is sent from a PDA device to the server. I am writing software for both the web app and the PDA. The web server is located at one location while the PDA devices are located around the world. The question is, how do I handle...
3
482
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 savings happens, the time-of-day alignment is lost. For example, if the daily task gets kicked in at 9 p.m. every evening, after daylight savings ends in fall, the task is shown as kicking in at 8 p.m. Similarly, if the daily task gets kicked in at...
1
2174
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 the default formats used by ToString, ToShortTimeString and ToLongTimeString. All of them currently consider me to be in the GMT+3 timezone, Which is when DST is supposed to be turned on. Checking IsDayLightSavingsTime() returns True currently,...
0
8888
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
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
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...
1
6702
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
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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

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.