473,573 Members | 2,780 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in Time module, or in my understanding?

I am getting results like these with the time module:
>>import time
int(time.mkti me(time.strptim e('2007-03-11 02:00:00', '%Y-%m-%d %H:%M
%S')))
1173610800
>>int(time.mkti me(time.strptim e('2007-03-11 03:00:00', '%Y-%m-%d %H:%M
%S')))
1173610800
>>time.strftime ('%Y-%m-%d %H:%M:%S', time.localtime( 1173610800))
'2007-03-11 03:00:00'

I know it probably has something to do with daylight savings, but how can I
get back out that which I put in? The times concerned are all standard
time, so how can I keep the time module from asserting its belief that I
want to convert to daylight savings?

Incidentally, it only happens with times on 2007-03-11 from 02:00:00 to
02:59:59, and not with time data from past years.

Thanks for any pointers!

j

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

Aug 1 '07 #1
2 1494
Joshua J. Kugler wrote:
I am getting results like these with the time module:
>import time
int(time.mktim e(time.strptime ('2007-03-11 02:00:00', '%Y-%m-%d %H:%M:%S')))
1173610800
>int(time.mktim e(time.strptime ('2007-03-11 03:00:00', '%Y-%m-%d %H:%M:%S')))
1173610800
>time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( 1173610800))
'2007-03-11 03:00:00'
This is quite a nice test case, actually. :-)
I know it probably has something to do with daylight savings, but how can I
get back out that which I put in? The times concerned are all standard
time, so how can I keep the time module from asserting its belief that I
want to convert to daylight savings?
Well, I think that if you inspect the result of strptime, you'll see
that the last element of the time "tuple" - in fact, the tm_isdst
member of a time "structure" - is set to -1:
>>time.strptime ('2007-03-11 02:00:00', '%Y-%m-%d %H:%M:%S')
(2007, 3, 11, 2, 0, 0, 6, 70, -1)
>>time.strptime ('2007-03-11 03:00:00', '%Y-%m-%d %H:%M:%S')
(2007, 3, 11, 3, 0, 0, 6, 70, -1)

What is likely to happen when such results are passed to mktime is
that the underlying library function will use its discretion in
determining whether daylight savings time is in operation or not.
Incidentally, it only happens with times on 2007-03-11 from 02:00:00 to
02:59:59, and not with time data from past years.
This suggests that the timezone database is being used to provide the
results. In fact, you can override the behaviour of mktime by
constructing time tuples or structures with other values for the last
element or tm_isdst respectively:
>>time.mktime(( 2007, 3, 11, 2, 0, 0, 6, 70, -1))
1173574800.0
>>time.mktime(( 2007, 3, 11, 2, 0, 0, 6, 70, 0))
1173574800.0
>>time.mktime(( 2007, 3, 11, 2, 0, 0, 6, 70, 1))
1173571200.0
>>time.mktime(( 2007, 3, 11, 3, 0, 0, 6, 70, -1))
1173578400.0
>>time.mktime(( 2007, 3, 11, 3, 0, 0, 6, 70, 0))
1173578400.0
>>time.mktime(( 2007, 3, 11, 3, 0, 0, 6, 70, 1))
1173574800.0

By asserting tm_isdst as being 0, the usual apparent interval between
the times is preserved.

Paul

Aug 1 '07 #2
On Wednesday 01 August 2007 14:45, Paul Boddie wrote:
Well, I think that if you inspect the result of strptime, you'll see
that the last element of the time "tuple" - in fact, the tm_isdst
member of a time "structure" - is set to -1:
>>>time.strptim e('2007-03-11 02:00:00', '%Y-%m-%d %H:%M:%S')
(2007, 3, 11, 2, 0, 0, 6, 70, -1)
>>>time.strptim e('2007-03-11 03:00:00', '%Y-%m-%d %H:%M:%S')
(2007, 3, 11, 3, 0, 0, 6, 70, -1)

What is likely to happen when such results are passed to mktime is
that the underlying library function will use its discretion in
determining whether daylight savings time is in operation or not.
That sounds about right.
By asserting tm_isdst as being 0, the usual apparent interval between
the times is preserved.
I'll do that. This isn't so much about DST vs. non-DST (as I may be putting
values in here that were recorded during DST), but in this case it was just
trying to get the same value out that I had put in.

Thanks for the pointers!

j

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

Aug 1 '07 #3

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

Similar topics

37
2971
by: jwaixs | last post by:
arg... I've lost 1.5 hours of my precious time to try letting re work correcty. There's really not a single good re tutorial or documentation I could found! There are only reference, and if you don't know how a module work you won't learn it from a reference! This is the problem: >>> import re >>> str = "blabla<python>Re modules...
25
7735
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30 minutes with Python 3 times a week since, have 14 years of computing experience, 8 years in mathematical computing and 4 years in unix admin and perl, i...
17
26525
by: Pam Ammond | last post by:
I need to use Microsoft Access Automation within a Visual Studio 2003 program written in C# for Windows Forms. When a button is clicked in my VS.NET program, I want it to run a Microsoft Access Module. Here is the info on the Microsoft Access Module: Microsoft Access 2003 mdb is located at: C:\C#.NET\Esperanza The mdb is called: EspThr.mdb...
7
2906
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of...
30
2050
by: Franck PEREZ | last post by:
Hello, I'm developing a small XML marshaller and I'm facing an annoying issue. Here's some sample code: ########### My test application ############ class Foo(object): #The class I'd like to serialize pass
8
1614
by: digitalorganics | last post by:
Hi all. If I have an instance of class A, called say foo, and I need to mix-in the functions and variables of another class (class B) to this instance at runtime, how do I do it? In other words, I want to make foo an instance of an anonymous and temporary class that inherits its functionality from classes A and B, while at the same time I want...
2
1806
by: dfj225 | last post by:
Hello All, A software project that I am working on currently has a C++ library that runs on a Beowulf cluster for parallel computation. The library code uses MPI for its implementation. We are currently developing python wrappers for the library. Our current design uses one Python process as a front end with processes created from the...
4
1650
by: Peter J. Bismuti | last post by:
I'm having trouble understanding how namespaces work in modules. I want to execute a module within the interpreter and then have values that are calculated persist so that other modules that get executed can retrieve them. For example, consider the two simple modules below. The first method fails and I'm not sure exactly why. (Note: ...
5
1565
by: Jeff Schwab | last post by:
Q1: When a module is imported, is there any way for the module to determine the name of the client code's module? Q2: My understanding is that the code in a module is executed only on the first import of that module. Is there any way to have a hook invoked on subsequent imports, and for that hook (as in Q1) to determine the name of the...
0
7753
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...
0
8187
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...
1
7751
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8045
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...
0
6376
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...
1
5567
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...
0
3714
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...
0
3718
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2178
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.