Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract time from datetime and determine if time (not date) falls within range?

The problem is that I want it to ignore the date and only factor in the time. Here is what I have:

import time from time import mktime from datetime import datetime  def getTimeCat(Datetime):     # extract time categories     str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")     ts = datetime.fromtimestamp(mktime(str_time))      # --> Morning = 0400-1000     mornStart = datetime.time(4, 0, 1)     mornEnd = datetime.time(10, 0, 0)      # --> Midday = 1000-1600     midStart = datetime.time(10, 0, 1)     midEnd = datetime.time(16, 0, 0)      # --> Evening = 1600-2200     eveStart = datetime.time(16, 0, 1)     eveEnd = datetime.time(22, 0, 0)      # --> Late Night = 2200-0400     lateStart = datetime.time(22, 0, 1)     lateEnd = datetime.time(4, 0, 0)      if time_in_range(mornStart, mornEnd, ts):       timecat = 0 #morning     elif time_in_range(midStart, midEnd, ts):       timecat = 1 #midday     elif time_in_range(eveStart, eveEnd, ts):       timecat = 2 #evening     elif time_in_range(lateStart, lateEnd, ts):       timecat = 3 #late night      return timecat 

As is, I get this error:

TypeError: argument must be 9-item sequence, not datetime.datetime 

When I change the relevant line to:

str_time = time.strptime(Datetime, "%m/%j/%y %H:%M") 

I get this error:

TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int' 

I know I'm working with two different libraries or whatnot, but I'm not sure how to convert between them or accomplish what I want to do only using one. I just want it to ignore the date and only check if the time is within the specified ranges. Python 2.6 is a MUST due to a library I'm using elsewhere in the code.

like image 596
Dan Avatar asked Apr 22 '13 02:04

Dan


People also ask

How do I determine if current time is within a specified range?

Datetime objects are comparable, so you can compare datetime objects using the < , > , <= , >= , and == comparison operators. Therefore, you can use the expression start <= current <= end to check if a current time falls into the interval [start, end] when assuming that start , end , and current are datetime objects.

How do you check if time is greater than or less than a specific time in python?

Try something like the following: >>> d = datetime. datetime. utcnow() >>> print d 2015-06-17 11:39:48.585000 >>> d.

How do I extract time from a datetime object in python?

e.g., datetime_obj = datetime. strptime(“19022002101010″,”%d%m%Y%H%M%S”) # It will return the datetime object.

How can I get time from datetime?

How to Get the Current Time with the datetime Module. To get the current time in particular, you can use the strftime() method and pass into it the string ”%H:%M:%S” representing hours, minutes, and seconds.


2 Answers

This line:

str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M") 

returns a datetime object as per the docs.

You can test this yourself by running the following command interactively in the interpreter:

>>> import datetime >>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M") datetime.datetime(2013, 1, 31, 0, 12) >>> 

The time portion of the returned datetime can then be accessed using the .time() method.

>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M").time() datetime.time(0, 12) >>> 

The datetime.time() result can then be used in your time comparisons.

like image 166
Austin Phillips Avatar answered Sep 19 '22 13:09

Austin Phillips


Use this only gives you time.

from datetime import datetime  now = datetime.now() current_time = now.strftime("%H:%M:%S") print("Current Time =", current_time) 
like image 25
aditya rao Avatar answered Sep 21 '22 13:09

aditya rao