strptime in 2.3.3
Holger Joukl
Holger.Joukl at LBBW.de
Fri Mar 26 11:08:12 EST 2004
> Andy writes:
> Folks,
>
> I am parsing a list of directories that have a name format
> 'YearWeekNoDay', using the follwoing code
>
> print list dir[j]
> print "%s" % (time.strptime (list dir[j], '%Y%U%w'))
> ttuple = time.strptime (list dir[j], '%Y%U%w')
> year = ttuple[0]
> month = ttuple[1]
> day = ttuple[2]
> print day, month, year
>
> I get the following output:
>
> 2004123
> (2004, 1, 1, 0, 0, 0, 2, 1, -1)
> 1 1 2004
>
> 2004124
> (2004, 1, 1, 0, 0, 0, 3, 1, -1)
> 1 1 2004
>
> ====
>
> Are you sure you are using the correct format string?
> %U is week number of year [00-53], so I am not sure what happens
> if you combine that with a day of month that surely is not in this week..
>
> Cheers
> Holger
>
%w (lower case) is day of the week 0 (Sunday) to 6
============
Andy,
you are right, of course. I misread your posting - sorry.
I looked in the source code and this is how I understand it:
Funny as it is, python 2.3.3 strptime simply ignores the %U part in the
format string.
The strptime handling is actually done in lib/_strptime.py, not by the C
strptime function
from time.h any more. Alas, there is no %U handling in there!
You could patch _strptime.py in the following way to reactivate %U
functionality:
$ diff -u -1 _strptime.py.orig _strptime.py
--- _strptime.py.orig Fri Mar 26 16:44:47 2004
+++ _strptime.py Fri Mar 26 16:33:18 2004
@@ -434,2 +434,3 @@
weekday = julian = -1
+ weeknr_set = False
found_dict = found.groupdict()
@@ -506,3 +507,11 @@
tz = 1
-
+ elif group_key == 'U':
+ # if julian is set just ignore the week number
+ if julian == -1:
+ # set the julian number to be in the week with given
number
+ julian = 1 + (int(found_dict['U']) - 1) * 7
+ weeknr_set = True
+ if weekday == -1:
+ weekday = 0 # set default to 0 (Monday)
+
# Cannot pre-calculate datetime_date() since can change in Julian
@@ -517,2 +526,10 @@
datetime_result = datetime_date.fromordinal((julian - 1) +
datetime_date(year, 1, 1).toordinal())
+ if weeknr_set:
+ # correct julian to be the given weekday (which defaults to 0)
+ julian += weekday - datetime_result.weekday()
+ if julian < 1:
+ # ignore negative values, stay in the current year
+ julian = 1
+ datetime_result = datetime_date.fromordinal((julian - 1) +
datetime_date(year, 1, 1).toordinal())
+
year = datetime_result.year
Note:
Behaviour differs a bit from the results you get with e.g. the python 1..5.2
version.
Since %U expects a 0 for Sunday,..., 6 for Monday, but the week numbering
works differently
(at least in the calendar on my table :
>>>
>>> time.strptime("2004 2 0", "%Y %U %w") # 0 for %U is Sunday
(2004, 1, 11, 0, 0, 0, 6, 11, -1)
>>> time.strptime("2004 2 0", "%Y %U %w") # 0 for %U is Sunday --> Sunday
in week 2, 2004 is Jan. 11
(2004, 1, 11, 0, 0, 0, 6, 11, -1)
>>> time.strptime("2004 2 1", "%Y %U %w") # 1 for %U is Monday --> Monday
in week 2, 2004 is Jan. 5
(2004, 1, 5, 0, 0, 0, 0, 5, -1)
>>>
But this can be easily changed to behave like pre-2.3.3 strptime.
Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.
The contents of this e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail. Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.
More information about the Python-list
mailing list