0

I have this function for checking a specific time and day in the week. It should print 1 from Sunday 7:00PM to Friday 8:00PM And print 0 from Friday 8:00PM to Sunday 7:00PM

I checked the function on Friday around 11:00AM and 2:00PM and I'm getting the error message in the title.

Can someone explain exactly what the error means? And how I could maybe fix it? It's supposed to check the time in Eastern Standard Time always

import pytz
from datetime import datetime, time, date

est = pytz.timezone('EST')

Mon = 0
Tue = 1
Wed = 2
Thur = 3
Fri = 4 
Sat = 5 
Sun = 6

weekdays = [Mon, Tue, Wed, Thur]
edgecases = [Sun, Fri]
weekend = [Sat]

curr_day = datetime.now(tz=est).date().weekday()
curr_time = datetime.now(tz=est).time()

def checktime(curr_day, curr_time):
    if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00,tzinfo=est)) or (curr_day == Fri and curr_time < time(20,00,tzinfo=est)):
        print(1)

    elif curr_day in weekend or (curr_day == Fri and curr_time >= time(20,00,tzinfo=est)) or (curr_day == Sun and curr_time <= time(19,00,tzinfo=est)):
        print(0)

Traceback error:

Traceback (most recent call last):
  File ".\testingtime.py", line 73, in <module>
    checktime(curr_day, curr_time)
  File ".\testingtime.py", line 67, in checktime
    if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00,tzinfo=est)) or (curr_day == Fri and curr_time < time(20,00,tzinfo=est)):
TypeError: can't compare offset-naive and offset-aware times
Kinnturo
  • 141
  • 1
  • 2
  • 13
  • You should always include the full error trace in the question, it helps identify exactly where the error occurs. – Mark Ransom Jul 17 '20 at 18:22
  • True, I'll add that now – Kinnturo Jul 17 '20 at 18:24
  • why do you add a timezone in the first place? if you only compare *time*, that doesn't make sense since a timezone can have DST changes (e.g. EST to EDT) - which depend on the *date*. – FObersteiner Jul 17 '20 at 18:26
  • I'm adding a timezone because I want to ignore EDT, and only compare the EST time. If that's not possible then do you have an idea as to how I could change it? – Kinnturo Jul 17 '20 at 18:28
  • Ok I get it ;-) Well, I think since you compare against static times anyway (and `curr_time` has no timezone after `.time()`), you can just remove the timezone in your comparisons, e.g. `curr_time > time(19,00)` and so on. – FObersteiner Jul 17 '20 at 18:33
  • Just changed it, and yes it fixed it hahaha, thank you! Can you post that as your answer, so I can mark it as one? – Kinnturo Jul 17 '20 at 18:38
  • ok, glad I could help! The weird thing is actually that you can add a timezone to a `time` object but a `datetime` object looses that info after `.time()`... – FObersteiner Jul 17 '20 at 18:43

2 Answers2

1

For one, if you call the time() method on a tz aware datetime object, the resulting time object will not carry the timezone info anymore - since it is assumed that it makes no sense without the date. Second, since you compare to static times, you don't need a timezone there. You can thus simplify your function to

def checktime(curr_day, curr_time):
    if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00)) or (curr_day == Fri and curr_time < time(20,00)):
        print(1)
    elif curr_day in weekend or (curr_day == Fri and curr_time >= time(20,00)) or (curr_day == Sun and curr_time <= time(19,00)):
        print(0)
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Are you sure about your first point though? I removed the tz=est from my datetime object and actually ended up with a different time Using tz param: 13:59:43.695155 Not using tz param: 14:59:43.696137 – Kinnturo Jul 17 '20 at 19:03
  • @Kinnturo: yep, sorry this is a bit unclear - `datetime.now(tz=est)` creates a timezone-aware datetime object. `.time()` strips the date and timezone information *afterwards* - however the time itself is not modified anymore. So if you call `datetime.now()`, you already have a different time in the first place. – FObersteiner Jul 17 '20 at 19:05
0

This sorted it for me:

dt_obj = dt_obj.replace(tzinfo=None)

See python – Can’t subtract offset-naive and offset-aware datetimes ... answer 1

dizzy5
  • 1