0

I tried some suggestions from other answers to similar question (https://stackoverflow.com/a/2031297/940208) and what I have so far is:

t = datetime.datetime.today()
tomorrow = datetime.datetime(t.year,t.month,t.day+1,10,0)

The problem is, when we have the last day of month this will fail:

ValueError: day is out of range for month

So how can I rewrite this statement (without wrapping it into IF condition trying to find it this is the last day) to include such situation?

Another question would be - what is a timezone of such timer and how can I work on my current (system) timezone?

Community
  • 1
  • 1
mnowotka
  • 16,430
  • 18
  • 88
  • 134
  • Note: in some timezones `10am tomorrow` might never happen or might happen twice due to DST changes. – jfs Sep 04 '13 at 09:22
  • @J.F.Sebastian - any way to solve this problem? – mnowotka Sep 04 '13 at 09:38
  • yes, multiple (obvious) solutions are possible if you know your context e.g., skip event, repeat event, execute task in a fixed number of seconds, etc. – jfs Sep 04 '13 at 10:37
  • My context is "poor-man's cron" - execute some function everyday more or less at 10 AM. – mnowotka Sep 04 '13 at 10:39

1 Answers1

1

Use datetime.datetime.replace and datetime.timedelta:

tomorrow = t.replace(hour=10, minute=0, second=0, microsecond=0) + \
           datetime.timedelta(days=1)

Alternatives:

tomorrow = datetime.datetime.combine(t.date(), datetime.time(10)) + \
           datetime.timedelta(days=1)

tomorrow = datetime.datetime(t.year, t.month, t.day, 10) + \
           datetime.timedelta(days=1)
falsetru
  • 357,413
  • 63
  • 732
  • 636