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

time.strftime in 2.4.1 claims data out of range when not

I have a web app that has been running just fine for several months under
Python 2.2.2.

We are preparing to upgrade the server to run Python 2.4.1.

However, part of my web app is throwing an error on this code (that has
previously worked without exception):
time.strftime("%Y-%m-%d", (Y, M, D, 0,0,0,0,0,0)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day of year out of range Y 2005 M 5 D

15L
I don't see what parts of the date that I have submitted to strftime are out
of range?

Also, the phrasing of the error message is a bit odd?
"day of year out of range"

I'm not sure what the
day of a year

would be???

Sheila King
http://www.thinkspot.net/sheila/
Jul 19 '05 #1
5 2914
Sheila King said unto the world upon 2005-04-22 02:45:
I have a web app that has been running just fine for several months under
Python 2.2.2.

We are preparing to upgrade the server to run Python 2.4.1.

However, part of my web app is throwing an error on this code (that has
previously worked without exception):

time.strftime("%Y-%m-%d", (Y, M, D, 0,0,0,0,0,0))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day of year out of range
Y
2005
M
5
D


15L
I don't see what parts of the date that I have submitted to strftime are out
of range?

Also, the phrasing of the error message is a bit odd?
"day of year out of range"

I'm not sure what the
day of a year

would be???

Sheila King
http://www.thinkspot.net/sheila/


The day of the year is the ordinal number of the day, counting from
Jan. 1st. So, Feb. 1st is the 32nd day, Dec 31 the 365/6 (depending on
leap year).

The docs for the time module indicate that the 8th (counting from 1)
position of a struct_time is the day of the year, and that it can
range from 1-366. So, naturally, the 0 value in your 8th position is
the problem.

That does leave the mystery of why your code worked on 2.2.2; I've no
idea. The docs do seem to indicate that there were a number of changes
at 2.2, though.

Best,

Brian vdB
Jul 19 '05 #2
[Sheila King]
I have a web app that has been running just fine for several months under
Python 2.2.2.

We are preparing to upgrade the server to run Python 2.4.1.

However, part of my web app is throwing an error on this code (that has
previously worked without exception):
time.strftime("%Y-%m-%d", (Y, M, D, 0,0,0,0,0,0)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day of year out of range Y 2005 M 5 D 15L


From the docs for time.strptime:
"The default values used to fill in any missing data are (1900, 1, 1, 0, 0,
0, 0, 1, -1) ".

So, you could change the offending code line to:
strftime("%Y-%m-%d", (Y, M, D, 0, 0, 0, 0, 1, -1) ) '2005-05-15'

Since the rules for handling missing, inconsistent, or out-of-range tuple fields
are not defined, even that revision has some risk. To future-proof the code,
use strptime() to generate a well-formed time tuple:
strptime('%d-%d-%d' % (y,m,d), '%Y-%m-%d') (2005, 5, 15, 0, 0, 0, 6, 135, -1) strftime("%Y-%m-%d", _)

'2005-05-15'

This somewhat circular technique sticks with the documented API but allows you
to access all of the time module's options (like accessing the locale's names
for days of the week and months of the year).
Raymond Hettinger



Jul 19 '05 #3
Raymond Hettinger wrote:
Since the rules for handling missing, inconsistent, or out-of-range tuple fields
are not defined, even that revision has some risk. To future-proof the code,
use strptime() to generate a well-formed time tuple:
strptime('%d-%d-%d' % (y,m,d), '%Y-%m-%d')
(2005, 5, 15, 0, 0, 0, 6, 135, -1)
strftime("%Y-%m-%d", _)
'2005-05-15'


or use datetime.date which only needs y, m, d:
from datetime import date
d=date(2005, 5, 15)
d.strftime("%Y-%m-%d")

'2005-05-15'

Kent
Jul 19 '05 #4
[Sheila King]
I have a web app that has been running just fine for several months under
Python 2.2.2.

We are preparing to upgrade the server to run Python 2.4.1.

However, part of my web app is throwing an error on this code (that has
previously worked without exception):
time.strftime("%Y-%m-%d", (Y, M, D, 0,0,0,0,0,0)) Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: day of year out of range Y 2005 M 5 D

15L

I don't see what parts of the date that I have submitted to strftime are out
of range?

Also, the phrasing of the error message is a bit odd?
"day of year out of range"


That was explained already, so I won't again. The NEWS file for
Python 2.4a1 explains why:

"""
- time.strftime() now checks that the values in its time tuple argument
are within the proper boundaries to prevent possible crashes from the
platform's C library implementation of strftime(). Can possibly
break code that uses values outside the range that didn't cause
problems previously (such as sitting day of year to 0). Fixes bug
#897625.
"""

See the referenced bug report for examples of platforms whose
strftime()s crashed the app, or just returned utter gibberish, when
passed senseless values. Python's strftime wrapper now verifies that
all passed-in values are in range, no longer trusting the platform C's
strftime() to do error-checking.
Jul 19 '05 #5
Hello,

Thank you to all who replied. Yes, obviously the extra values I'm passing are
out of range, such as the ordinal day-number of the year. Oy.

I've been having a number of issues with switching from 2.2.2 to 2.4.1 and
last night when I started trying to address this problem (at a late hour) I'm
afraid my thunker sure wasn't thunking.

I like the datetime.date suggestion best, probably.

Again, thanks, and this will help me to get through this niggle...

Sheila King
http://www.thinkspot.net/sheila/

On Fri, 22 Apr 2005 05:26:02 -0700, Kent Johnson wrote
(in article <42**********@newspeer2.tds.net>):
Raymond Hettinger wrote:
Since the rules for handling missing, inconsistent, or out-of-range tuple
fields
are not defined, even that revision has some risk. To future-proof the
code,
use strptime() to generate a well-formed time tuple:
> strptime('%d-%d-%d' % (y,m,d), '%Y-%m-%d')


(2005, 5, 15, 0, 0, 0, 6, 135, -1)
> strftime("%Y-%m-%d", _)


'2005-05-15'


or use datetime.date which only needs y, m, d:
>>> from datetime import date
>>> d=date(2005, 5, 15)
>>> d.strftime("%Y-%m-%d")

'2005-05-15'

Kent

Jul 19 '05 #6

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

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
1
by: Allen Unueco | last post by:
I feel that the '%Z' format specifier from strftime() returns the wrong value when daylight savings is in effect. Today the following is always true: time.strftime('%Z') == time.tzname ...
4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
4
by: Andy Leszczynski | last post by:
Python 2.2/Unix >>time.strftime("%T") '22:12:15' >>time.strftime("%X") '22:12:17' Python 2.3/Windows >>time.strftime("%X")
8
by: Adam Monsen | last post by:
Anyone know of something that works like time.strptime(), but for other languages? Specifically, Dutch (ex: "31 augustus 2005, 17:26") and German? Thinking out loud... since "31 augustus 2005,...
5
by: Swansea University Psychology | last post by:
Hi all, I have a utility that uses the C library function strftime() to return the time zone name, but it returns "GMT Standard Time" on one computer, and "BST" (which it should be at the...
20
by: Jean Johnson | last post by:
Hello - I have a start and end time that is written using the following: time.strftime("%b %d %Y %H:%M:%S") How do I calculate the elapsed time? JJ
5
by: HMS Surprise | last post by:
I wish to generate a datetime string that has the following format. '05/02/2007 12:46'. The leading zeros are required. I found '14.2 time' in the library reference and have pulled in localtime....
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...

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.