def parse_time_old(time_string, base_time, timezone_string): base_time = utils.datetime_as_timezone(base_time, timezone_string) try: date_time = dateparser.parse(time_string, settings={ "PREFER_DATES_FROM": 'future', "RELATIVE_BASE": base_time.replace(tzinfo=None) }) except Exception: date_time = None if date_time is None: try: results = search_dates(time_string, languages=['en'], settings={ "PREFER_DATES_FROM": 'future', "RELATIVE_BASE": base_time.replace(tzinfo=None) }) if results is not None: temp_time = results[0][1] if temp_time.tzinfo is None: temp_time = utils.datetime_force_utc(temp_time) if temp_time > base_time: date_time = results[0][1] else: date_time = None except Exception: date_time = None if date_time is None: try: date_time, result_code = cal.parseDT(time_string, base_time) if result_code == 0: date_time = None except Exception: date_time = None if date_time is None: return None if date_time.tzinfo is None: if timezone_string is not None: date_time = pytz.timezone(timezone_string).localize(date_time) else: date_time = utils.datetime_force_utc(date_time) date_time = utils.datetime_as_utc(date_time) return date_time
def test_timezone_reminder_message(database, reddit): user = database.get_or_add_user(user_name="Watchful1") user.timezone = "America/Los_Angeles" created = utils.datetime_now() target = created + timedelta(hours=24) username = "******" message = reddit_test.RedditObject( body= f"{static.TRIGGER}! {utils.get_datetime_string(utils.datetime_as_timezone(target, user.timezone))}", author=username, created=created) messages.process_message(message, reddit, database) reminders = database.get_all_user_reminders(username) assert len(reminders) == 1 assert reminders[0].requested_date == created assert reminders[0].target_date == utils.datetime_as_utc( pytz.timezone(user.timezone).localize(target.replace(tzinfo=None)))
try: date_time_result, result_code = cal.parseDT(time_string, base_time) if result_code != 0: if date_time is None: date_time = date_time_result log.info(f"cal.parseDT: {date_time.strftime(format_string)}") else: log.info( f" cal.parseDT would have found: {date_time_result.strftime(format_string)}" ) except Exception: date_time = None if date_time is None: log.info(f"No datetime found") sys.exit(0) if date_time.tzinfo is None: if timezone_string is not None: date_time = pytz.timezone(timezone_string).localize(date_time) log.info( f"Converting to timezone: {timezone_string} : {date_time.strftime(format_string)}" ) else: date_time = utils.datetime_force_utc(date_time) log.info(f"Converting to utc: {date_time.strftime(format_string)}") date_time = utils.datetime_as_utc(date_time) log.info(f"Forcing utc: {date_time.strftime(format_string)}")