-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Error: The given time 'in 30 seconds' is in the past." despite time given being in the future. #100
Comments
Seems like it's a problem with time determination. I believe the current I thought that a way to simplify def _get_datetime_now(self, tz: str) -> datetime:
"""Returns a timezone-aware datetime object of the current time"""
now = dateparser.parse('now', settings={"PREFER_DATES_FROM": "future", "TIMEZONE": tz, "RETURN_AS_TIMEZONE_AWARE": True})
# Round to the nearest second for nicer display
return now.replace(microsecond=0) There may be a smarter way which doesn't rely on
For the server that I saw to exhibit "date is in the past" errors ( |
Can reproduce even with more time such as 2 minutes. Using the following distilled version of the code to debug: Click to see Python codefrom datetime import datetime, timedelta, timezone
import pytz
import dateparser
TIMEZONE = "America/New_York"
def _get_datetime_now(tz: str) -> datetime:
"""Returns a timezone-aware datetime object of the current time"""
# Get a datetime with no timezone information
no_timezone_datetime = datetime(2009, 9, 1)
# Create a datetime.timezone object with the correct offset from UTC
offset = timezone(pytz.timezone(tz).utcoffset(no_timezone_datetime))
# Get datetime.now with that offset
now = datetime.now(offset)
# Round to the nearest second for nicer display
return now.replace(microsecond=0)
def _parse_str_to_time(time_str: str, tz_aware: bool = True) -> datetime:
"""Converts a human-readable, future time string to a datetime object
Args:
time_str: The time to convert
tz_aware: Whether the returned datetime should have associated timezone
information
Returns:
datetime: A datetime if conversion was successful
Raises:
CommandError: if conversion was not successful, or time is in the past.
"""
time = dateparser.parse(
time_str,
settings={
"PREFER_DATES_FROM": "future",
"TIMEZONE": TIMEZONE,
"RETURN_AS_TIMEZONE_AWARE": tz_aware,
},
)
print(f'{time=}')
if not time:
raise Exception(f"The given time '{time_str}' is invalid.")
# Disallow times in the past
tzinfo = pytz.timezone(TIMEZONE)
now =_get_datetime_now(TIMEZONE)
print(f'{now=}')
if time.replace(tzinfo=tzinfo) < now:
raise Exception(f"The given time '{time_str}' is in the past.")
# Round datetime object to the nearest second for nicer display
time = time.replace(microsecond=0)
return time
if __name__ == "__main__":
_parse_str_to_time("in 5 min")
|
I'm getting the "Error: The given time 'in 30 seconds' is in the past." from the bot if the time frame requested is between 0 and 8 minutes 59 seconds. At 9 minutes (or 540 seconds), the bot seems to work just fine.
Made sure my server's time was correct, tried again, still no success.
The text was updated successfully, but these errors were encountered: