Enhance NotificationScheduler with timezone initialization and current time retrieval. Refactor silent time checks to utilize the configured timezone, improving accuracy in logging and scheduling notifications.

This commit is contained in:
tigeren 2025-08-31 06:21:39 +00:00
parent d27d30b038
commit a2f909db41
1 changed files with 25 additions and 17 deletions

View File

@ -18,23 +18,30 @@ class NotificationScheduler:
self.template_manager = TemplateManager(config.templates_dir)
self.notification_client = NotificationClient(config)
self.running = False
self._timezone = None
self._timezone_name = None
self._init_timezone()
def _init_timezone(self):
"""Initialize timezone from config."""
import pytz
timezone_name = getattr(self.config, 'timezone', 'UTC')
try:
self._timezone = pytz.timezone(timezone_name)
self._timezone_name = timezone_name
except pytz.exceptions.UnknownTimeZoneError:
logger.warning(f"Unknown timezone '{timezone_name}', falling back to UTC")
self._timezone = pytz.UTC
self._timezone_name = "UTC (fallback)"
def _get_current_time(self):
"""Get current time in configured timezone."""
return datetime.now(self._timezone)
def _is_silent_time(self) -> bool:
"""Check if current time is within silent hours."""
import pytz
# Get the configured timezone
timezone_name = getattr(self.config, 'timezone', 'UTC')
try:
tz = pytz.timezone(timezone_name)
current_datetime = datetime.now(tz)
time_type = f"timezone {timezone_name}"
except pytz.exceptions.UnknownTimeZoneError:
logger.warning(f"Unknown timezone '{timezone_name}', falling back to UTC")
tz = pytz.UTC
current_datetime = datetime.now(tz)
time_type = "UTC (fallback)"
current_datetime = self._get_current_time()
current_time = current_datetime.time()
# Parse silent time configuration
@ -42,7 +49,7 @@ class NotificationScheduler:
silent_end = datetime.strptime(self.config.silent_end, "%H:%M").time()
# Log current time for debugging
logger.info(f"Current time: {current_datetime.strftime('%Y-%m-%d %H:%M:%S')} ({time_type})")
logger.info(f"Current time: {current_datetime.strftime('%Y-%m-%d %H:%M:%S')} (timezone {self._timezone_name})")
logger.info(f"Silent period: {self.config.silent_start} to {self.config.silent_end}")
# Handle overnight silent period (e.g., 20:00 to 12:00)
@ -110,8 +117,9 @@ class NotificationScheduler:
# Calculate next interval
interval = self._get_next_interval()
next_time = datetime.now() + timedelta(seconds=interval)
logger.info(f"Next notification scheduled for: {next_time.strftime('%Y-%m-%d %H:%M:%S')}")
current_time = self._get_current_time()
next_time = current_time + timedelta(seconds=interval)
logger.info(f"Next notification scheduled for: {next_time.strftime('%Y-%m-%d %H:%M:%S')} (timezone {self._timezone_name})")
# Wait for next interval
await asyncio.sleep(interval)