Reputation: 43
i'm using my backup script since 2011 and had no problems at all. But since start of 2013 I'm getting problem with the correct week number.
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.strftime("%W", time.localtime())
'02'
But we have week number 3
Am i doing something wrong? It was working till 2013.
Thanks.
Upvotes: 4
Views: 422
Reputation: 80346
All days in a new year preceding the first Monday are considered to be in week 0.docs
Take a look at the calendar:
import calendar
print calendar.month(2013,1)
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
In the standard library you can use use datetime.isocalendar()
:
from datetime import datetime
datetime.date(datetime.now()).isocalendar()[1]
Upvotes: 5
Reputation:
"""Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0."""
What you are looking for is the value for the week number as defined through the ISO standard.
Look at the isoweek module.
>>> from isoweek import Week
>>> Week.thisweek()
isoweek.Week(2013, 3)
>>> Week.thisweek().week
3
Upvotes: 5