
    Ki                     @   U d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	  ej
        e          Z	 ddlmZ n# e$ r	 ddlmZ Y nw xY wdae	e         ed<   dae	e         ed	<   d
aeed<   defdZdede	e         fdZde	e         fdZdefdZdefdZddZdS )u  
Timezone-aware clock for Hermes.

Provides a single ``now()`` helper that returns a timezone-aware datetime
based on the user's configured IANA timezone (e.g. ``Asia/Kolkata``).

Resolution order:
  1. ``HERMES_TIMEZONE`` environment variable
  2. ``timezone`` key in ``~/.hermes/config.yaml``
  3. Falls back to the server's local time (``datetime.now().astimezone()``)

Invalid timezone values log a warning and fall back safely — Hermes never
crashes due to a bad timezone string.
    N)datetime)Path)get_hermes_home)Optional)ZoneInfo
_cached_tz_cached_tz_nameF_cache_resolvedreturnc                     t          j        dd                                          } | r| S 	 ddl}t	                      }|dz  }|                                rt          |          5 }|                    |          pi }ddd           n# 1 swxY w Y   |                    dd          }t          |t                    r(|                                r|                                S n# t          $ r Y nw xY wdS )zRead the configured IANA timezone string (or empty string).

    This does file I/O when falling through to config.yaml, so callers
    should cache the result rather than calling on every ``now()``.
    HERMES_TIMEZONE r   Nzconfig.yamltimezone)osgetenvstripyamlr   existsopen	safe_loadget
isinstancestr	Exception)tz_envr   hermes_homeconfig_pathfcfgtz_cfgs          (/home/ubuntu/hermes-agent/hermes_time.py_resolve_timezone_namer"   &   sF    Y("--3355F %''!M1 	&k"" .annQ''-2. . . . . . . . . . . . . . .WWZ,,F&#&& &6<<>> &||~~%    2s6   :C* 'B?C* BC* BAC* *
C76C7namec                     | sdS 	 t          |           S # t          t          f$ r'}t                              d| |           Y d}~dS d}~ww xY w)z3Validate and return a ZoneInfo, or None if invalid.Nz=Invalid timezone '%s': %s. Falling back to server local time.)r   KeyErrorr   loggerwarning)r#   excs     r!   _get_zoneinfor)   B   sq     t~~i    K#	
 	
 	
 ttttts    AAAc                  f    t           s$t                      at          t                    ada t          S )zReturn the user's configured ZoneInfo, or None (meaning server-local).

    Resolved once and cached. Call ``reset_cache()`` after config changes.
    T)r
   r"   r	   r)   r        r!   get_timezoner-   P   s/      022"?33
r,   c                  >    t           st                       t          pdS )zAReturn the IANA name of the configured timezone, or empty string.r   )r
   r-   r	   r+   r,   r!   get_timezone_namer/   ]   s        b r,   c                      t                      } | t          j        |           S t          j                                                    S )z
    Return the current time as a timezone-aware datetime.

    If a valid timezone is configured, returns wall-clock time in that zone.
    Otherwise returns the server's local time (via ``astimezone()``).
    )r-   r   now
astimezone)tzs    r!   r1   r1   e   s:     
B	~|B<>>$$&&&r,   c                      da dadadS )zBClear the cached timezone. Used by tests and after config changes.NF)r   r	   r
   r+   r,   r!   reset_cacher5   s   s     JOOOOr,   )r   N)__doc__loggingr   r   pathlibr   hermes_constantsr   typingr   	getLogger__name__r&   zoneinfor   ImportErrorbackports.zoneinfor   __annotations__r	   r   r
   boolr"   r)   r-   r/   r1   r5   r+   r,   r!   <module>rB      s      				             , , , , , ,      		8	$	$,!!!!!!! , , ,++++++++, "&
HX % % %!%# % % %       8  2    
hx( 
 
 
 
!3 ! ! ! !'X ' ' ' '     s   < A
	A
