3

When a function runs I need it to check the current system time and if its past 5:00pm to do something.

current_time = datetime.datetime.now()
if current_time > {code to represent 17:00 hours}:
    do stuff

Edit for clarification: Orders are being sent into my software which handles the picking/packing of orders in a distribution center. If the order is sent into the system after 5:00 then that order needs to get flagged so that it isn't "picked" until the following day. So the function needs to be run as orders are "imported"

Ominus
  • 5,501
  • 8
  • 40
  • 44
  • Far easier would be to use `cron` or Windows scheduled tasks....... – David Heffernan Oct 12 '11 at 15:34
  • Orders are being sent into my software which handles the picking/packing of orders in a distribution center. If the order is sent into the system after 5:00 then that order needs to get flagged so that it isn't "picked" until the following day. So the function needs to be run as orders are "imported" – Ominus Oct 12 '11 at 15:36
  • 4
    This is a typical "I did not read the docs" kind of question: http://docs.python.org/library/datetime.html#datetime-objects – patrys Oct 12 '11 at 15:37
  • I don't know why the downvote. simple question with a simple answer; Maybe it's a duplicate, but not of the question linked. – SingleNegationElimination Oct 12 '11 at 15:39
  • @TokenMacGuy - yes, I think both David and I misread the question in the same way ;^) – Nate Oct 12 '11 at 15:40
  • I'm confused on the down votes too. Granted i should have looked at the docs closer (i was over thinking this for sure) but I could easily see someone else searching for an answer to this. With two perfectly good solutions already posted. – Ominus Oct 12 '11 at 15:48
  • Well, on the bright side; empty downvotes are a great way to earn sympathetic upvotes! – SingleNegationElimination Oct 12 '11 at 15:55

2 Answers2

10
if datetime.datetime.now().hour >= 17:
    pass
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
4

Sure, it's easy:

>>> import datetime
>>> datetime.datetime.now().time()
datetime.time(10, 36, 5, 572343)
>>> datetime.datetime.now().time() >= datetime.time(17,0,0)
False
>>> datetime.datetime.now().time() >= datetime.time(8,0,0)
True
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • wow, I sure didn't expect this to be downvoted... I'm not sure I know what's wrong so that I can fix it. Can anyone suggest what I might have done wrong? – SingleNegationElimination Oct 12 '11 at 15:41
  • Both yours' and CharString's answer do exactly what i want. Between the two answers is one "better" then the other or just two different ways to accomplish the same goal? – Ominus Oct 12 '11 at 15:45
  • well, mine uses the `datetime.time` class for the comparison. if you like using classes, that's a plus; If you dislike classes, maybe it's a minus. They do the very same thing. – SingleNegationElimination Oct 12 '11 at 15:51
  • Mine is just shorter and simpler. And doesn't have the overhead of creating the time object. Its a little bit faster, but not as much as I expected: 4.1 us vs 4.85 us. So you should go with what reads easiest. – Chris Wesseling Oct 12 '11 at 16:16