1

I want to write a function that returns a tuple of (start,end) where start is the Monday at 00:00:00:000000 and end is Sunday at 23:59:59:999999. start and end are datetime objects. No other information is given about day, month or year. i tried this function

def week_start_end(date):
 start= date.strptime("00:00:00.000000", "%H:%M:%S.%f")
 end = date.strptime("23:59:59.999999", "%H:%M:%S.%f")
return (start,end)

print week_start_end(datetime(2013, 8, 15, 12, 0, 0))

should return (datetime(2013, 8, 11, 0, 0, 0, 0), datetime(2013, 8, 17, 23, 59, 59, 999999))

but the function returns tuple with dates (datetime.datetime(1900, 1, 1, 0, 0), datetime.datetime(1900, 1, 1, 23, 59, 59, 999999))

5
  • 1
    Are you aware the weeks are numbered? There is something like 4th week of the year 2015 and so on. I'd say you should make use of that in your code. You probably need those ranges to comparison. If you really need this, read about datetime.timedelta. Find two Mondays 00:00 and compare the latter one with < instead of <=. Commented Sep 27, 2013 at 6:21
  • @KrzysztofSzularz: And there are at least 3 different rules defining which is the first week in a year in use across the world. Commented Sep 27, 2013 at 6:23
  • @JanHudec, if you operate on datetime objects and are using python's way of determining this you're going to always be right. Commented Sep 27, 2013 at 6:25
  • 1
    strptime is a class method. It will not fill in the remaining fields from the invocant. You have to modify the decomposed date representation yourself. Commented Sep 27, 2013 at 6:27
  • @KrzysztofSzularz: Yes, sure. Commented Sep 27, 2013 at 6:27

1 Answer 1

2

I think using datetime.isocalendar is a nice solution. This give the correct outputs for your example:

import datetime

def iso_year_start(iso_year):
    "The gregorian calendar date of the first day of the given ISO year"
    fourth_jan = datetime.date(iso_year, 1, 4)
    delta = datetime.timedelta(fourth_jan.isoweekday()-1)
    return fourth_jan - delta 

def iso_to_gregorian(iso_year, iso_week, iso_day):
    "Gregorian calendar date for the given ISO year, week and day"
    year_start = iso_year_start(iso_year)
    return year_start + datetime.timedelta(days=iso_day-1, weeks=iso_week-1)


def week_start_end(date):
    year = date.isocalendar()[0]
    week = date.isocalendar()[1]
    d1 = iso_to_gregorian(year, week, 0)
    d2 = iso_to_gregorian(year, week, 6)
    d3 = datetime.datetime(d1.year, d1.month, d1.day, 0,0,0,0)
    d4 = datetime.datetime(d2.year, d2.month, d2.day, 23,59,59,999999)
    return (d3,d4)

As an example:

>>> d = datetime.datetime(2013, 8, 15, 12, 0, 0)
>>> print week_start_end(d)
(datetime.datetime(2013, 8, 11, 0, 0), datetime.datetime(2013, 8, 17, 23, 59, 59, 999999))

And should help you with your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks Tony. You may want to check out the docs just as a reference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.