
    NQj                     (   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  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dZdef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)get_config_path)Optional)ZoneInfo
_cached_tz_cached_tz_nameF_cache_resolvedreturnc                     t          j        dd                                          } | r| S 	 	 ddlm}  |            pi }ny# t
          $ rl ddl}t                      }|                                rAt          |d          5 }|
                    |          pi }ddd           n# 1 swxY w Y   ni }Y nw xY w|r	 ddlm} |                    |          }n# t
          $ r Y nw xY w|                    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   )read_raw_configNzutf-8)encoding)managed_scopetimezone)osgetenvstriphermes_cli.configr   	Exceptionyamlr   existsopen	safe_load
hermes_clir   apply_managed_overlayget
isinstancestr)tz_envr   cfgr   config_pathfr   tz_cfgs           //home/alina/.hermes/hermes-agent/hermes_time.py_resolve_timezone_namer&   %   s    Y("--3355F 

	999999!/##)rCC 	 	 	KKK)++K!!## +888 2A..++1rC2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 	  
	&444444#99#>>   WWZ,,F&#&& &6<<>> &||~~%    2sy   A  D= AB7B&B7&B*	*B7-B*	.B74D= 6B77D= =C D= 
C&#D= %C&&AD= =
E
	E
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-   R   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_timezoner1   `   s/      022"?33
r0   c                      da dadadS )a&  Clear the cached timezone so the next call re-resolves it.

    Call this after the configured timezone may have changed (e.g. after a
    config edit or ``HERMES_TIMEZONE`` update) to force ``get_timezone()`` /
    ``now()`` to read the new value instead of the value cached at first use.
    NF)r   r   r	   r/   r0   r%   reset_cacher3   m   s     JOOOOr0   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()``).
    )r1   r   now
astimezone)tzs    r%   r5   r5   z   s:     
B	~|B<>>$$&&&r0   )r
   N)__doc__loggingr   r   hermes_constantsr   typingr   	getLogger__name__r*   zoneinfor   ImportErrorbackports.zoneinfor   __annotations__r   r   r	   boolr&   r-   r1   r3   r5   r/   r0   r%   <module>rC      s      				       , , , , , ,      		8	$	$,!!!!!!! , , ,++++++++, "&
HX % % %!%# % % %   * * * * *Z  2    
hx( 
 
 
 

 
 
 
'X ' ' ' ' ' 's   6 AA