user3497
user3497

Reputation: 1

Python datetime and day of week comparison

I have time defined in the following format: "%A %H:%M:%S" i.e "Monday 06:00:00" Now, using the above format, I define a time window: "Monday 06:00:00" - "Monday 18:00:00"

I now want to check if the current time falls within the time window or not. To do that, I am using the datetime utility.

    import datetime

    # current time in str
    current_time = datetime.datetime.strftime(datetime.datetime.now(),'%A %H:%M:%S')

    # convert to datetime object
    current_time = datetime.datetime.strptime(current_time, '%A %H:%M:%S')

    # print current_time produces: 1900-01-01 19:13:53
    # and day of week information is lost

However, the moment, I convert the current_time to a datetime object, the day of the week information is lost.

What's the best way to compare time windows that includes day-of-week in Python? BTW: I want the time window to be repeated in the future. For eg, "Monday 06:00 - Tuesday 05:00" would apply to all weeks in the future.

Upvotes: 0

Views: 2268

Answers (2)

Potrebic
Potrebic

Reputation: 386

datetime would know nothing about which monday you're talking about. There have been 1000's since the epoch.

I suggest you look into the dateutil package. It applies "human-ish" semantics:

>>> import dateutil.parser
>>> dateutil.parser.parse('Monday')
datetime.datetime(2014, 2, 3, 0, 0)
>>> dateutil.parser.parse('Monday 18:30:00')
datetime.datetime(2014, 2, 3, 18, 30)

Note how it assumes that Monday means today (it's Monday). And Tuesday and Sunday (below) mean this coming Tuesday, Sunday:

>>> dateutil.parser.parse('Tuesday 18:30:00')
datetime.datetime(2014, 2, 4, 18, 30)
>>> dateutil.parser.parse('Sunday 18:30:00')
datetime.datetime(2014, 2, 9, 18, 30)

To take this further one take a look at the rrule module/class in dateutil. And the built-in calendar module. For example, with rrule I can do:

next_five = rrule.rrule(
    rrule.WEEKLY, 
    count=5, 
    dtstart=dateutil.parser.parse('Sunday 18:30:00'),
)  

This returns an iterator of the next 5 weeks starting at 6:30pm Sunday. The iterator will produce

[datetime.datetime(2014, 2, 5, 18, 30), 
 datetime.datetime(2014, 2, 12, 18, 30), 
 datetime.datetime(2014, 2, 19, 18, 30), 
 datetime.datetime(2014, 2, 26, 18, 30), 
 datetime.datetime(2014, 3, 5, 18, 30)]

This should get you going on the final solution.

Upvotes: 2

Marc
Marc

Reputation: 1987

In my experience Datetime objects are YYYY:MM:DD HH:MM:SS (more or less). The day of week can be derived from that object using datetime's weekday() - see http://docs.python.org/2/library/datetime.html#datetime.date.weekday

You can solve your problem using greater/less than "<" and ">" operators for datetime evaluation. Here is a proof for simple comparison http://pastebin.com/miTUW9nF

Your logic would be (paraphrased): "if Xtime > windowstart and Xtime < windowend then Xtime is in window"

Datetime compares down to the second, my example is now and (now-10 days), that should cover your needs.

Upvotes: 0

Related Questions