
    Vh_                     6   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dlmZ  G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: timezone
short_description: Configure timezone setting
description:
  - This module configures the timezone setting, both of the system clock and of the hardware clock. If you want to set up
    the NTP, use M(ansible.builtin.service) module.
  - It is recommended to restart C(crond) after changing the timezone, otherwise the jobs may run at the wrong time.
  - Several different tools are used depending on the OS/Distribution involved. For Linux it can use C(timedatectl) or edit
    C(/etc/sysconfig/clock) or C(/etc/timezone) and C(hwclock). On SmartOS, C(sm-set-timezone), for macOS, C(systemsetup),
    for BSD, C(/etc/localtime) is modified. On AIX, C(chtz) is used.
  - Make sure that the zoneinfo files are installed with the appropriate OS package, like C(tzdata) (usually always installed,
    when not using a minimal installation like Alpine Linux).
  - Windows and HPUX are not supported, please let us know if you find any other OS/distro in which this fails.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
options:
  name:
    description:
      - Name of the timezone for the system clock.
      - Default is to keep current setting.
      - B(At least one) of O(name) and O(hwclock) are required.
    type: str
  hwclock:
    description:
      - Whether the hardware clock is in UTC or in local timezone.
      - Default is to keep current setting.
      - Note that this option is recommended not to change and may fail to configure, especially on virtual environments such
        as AWS.
      - B(At least one) of O(name) and O(hwclock) are required.
      - I(Only used on Linux).
    type: str
    aliases: [rtc]
    choices: [local, UTC]
notes:
  - On Ubuntu 24.04 the C(util-linux-extra) package is required to provide the C(hwclock) command.
  - On SmartOS the C(sm-set-timezone) utility (part of the smtools package) is required to set the zone timezone.
  - On AIX only Olson/tz database timezones are usable (POSIX is not supported). An OS reboot is also required on AIX for
    the new timezone setting to take effect. Note that AIX 6.1+ is needed (OS level 61 or newer).
author:
  - Shinichi TAMURA (@tmshn)
  - Jasper Lievisse Adriaanse (@jasperla)
  - Indrajit Raychaudhuri (@indrajitr)
a  
diff:
  description: The differences about the given arguments.
  returned: success
  type: complex
  contains:
    before:
      description: The values before change.
      type: dict
    after:
      description: The values after change.
      type: dict
zf
- name: Set timezone to Asia/Tokyo
  become: true
  community.general.timezone:
    name: Asia/Tokyo
N)AnsibleModuleget_distribution)	iteritemsc                   ^     e Zd ZdZ fdZ fdZd Zd ZddZd Z	d Z
d	 Zd
 Zd Z xZS )Timezonea(  This is a generic Timezone manipulation class that is subclassed based on platform.

    A subclass may wish to override the following action methods:
        - get(key, phase)   ... get the value from the system at `phase`
        - set(key, value)   ... set the value to the current system
    c                    t        j                         dk(  r|j                  d      }|d|j                  |      \  }}}|dk(  rt        t
        t          t              S |j                  d|z         t        t
        t          t              S t        t
        t          t              S t        j                  dt        j                               rm|j                  d      }|?|j                  |      \  }}}|dk(  r%|j                         dk(  r|j                  d	
       t        t
        t          t              S t        j                         dk(  rt        t
        t           t               S t        j                  dt        j                                rt        t
        t"          t"              S t        j                         dk(  rht%        t        j                         t        j&                         z         }|dk\  rt        t
        t(          t(              S |j                  d|z  
       yt        t
        t
          t
              S )zReturn the platform-specific subclass.

        It does not use load_platform_subclass() because it needs to judge based
        on whether the `timedatectl` command exists and is available.

        Args:
            module: The AnsibleModule.
        LinuxtimedatectlNr   zEtimedatectl command was found but not usable: %s. using other method.z^joyent_.*Zzonenameglobalz2Adjusting timezone is not supported in Global ZonemsgDarwinz^(Free|Net|Open)BSDAIX=   z<AIX os level must be >= 61 for timezone module (Target: %s).)platformsystemget_bin_pathrun_commandsuperr
   SystemdTimezone__new__debugNosystemdTimezonerematchversionstrip	fail_jsonSmartOSTimezoneDarwinTimezoneBSDTimezoneintreleaseAIXTimezone)
clsmoduler   rcstdoutstderrzonename_cmddummy
AIXoslevel	__class__s
            n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/timezone.pyr   zTimezone.__new__e   s    ??' --m<K&%+%7%7%D"FF7 ?COTTLL!hkq!qr +<EFWXXX'8ABSTTXXmX%5%5%78 "..z:L'&,&8&8&F#VU7v||~9$$)]$^?;OLL__(*>:>JJXX+X->->-@A;7DD__%'X--/(2B2B2DDEJRX{;KHH  %cfp%p q 84X>>    c                     t         t        |           g | _        t	               | _        |j                  D ]-  }|j                  |   }|t	        |      | j
                  |<   / || _        y)zWInitialize of the class.

        Args:
            module: The AnsibleModule.
        N)planned)	r   r
   __init__r   dictvalueargument_specparamsr*   )selfr*   keyr8   r1   s       r2   r6   zTimezone.__init__   sh     	h&( V
'' 	6CMM#&E "&u"5

3	6 r3   c                     d|g}t        | j                        dkD  r,|j                  d       |j                  | j                         | j                  j                  dj                  |             y)zAbort the process with error message.

        This is just the wrapper of module.fail_json().

        Args:
            msg: The error message.
        zError message:r   zOther message(s):
r   N)lenr   appendextendr*   r"   join)r;   r   	error_msgs      r2   abortzTimezone.abort   s]     &s+	txx=101TXX&$))I"67r3   c                     dj                  |      }| j                  j                  |d      \  }}}|j                  dd      r| j                  j                  d|z         |S )a  Execute the shell command.

        This is just the wrapper of module.run_command().

        Args:
            *commands: The command to execute.
                It will be concatenated with single space.
            **kwargs:  Only 'log' key is checked.
                If kwargs['log'] is true, record the command to self.msg.

        Returns:
            stdout: Standard output of the command.
         T)check_rclogFzexecuted `%s`)rB   r*   r   getr   r@   )r;   commandskwargscommandr+   r,   r-   s          r2   executezTimezone.execute   sZ     ((8$#{{66w6NVV::eU#HHOOOg56r3   c                 x    |i |i i}t        | j                        D ]  \  }}||   ||   |<   ||   ||   |<    |S )aP  Calculate the difference between given 2 phases.

        Args:
            phase1, phase2: The names of phase to compare.

        Returns:
            diff: The difference of value between phase1 and phase2.
                This is in the format which can be used with the
                `--diff` option of ansible-playbook.
        )r   r8   )r;   phase1phase2diffr<   r8   s         r2   rQ   zTimezone.diff   sY     FB'#DJJ/ 	.JC %fDL %fDL	. r3   c                 t    |dk(  ryt        | j                        D ]  \  }}| j                  ||      ||<    y)zCheck the state in given phase and set it to `self.value`.

        Args:
            phase: The name of the phase to check.

        Returns:
            NO RETURN VALUE
        r5   N)r   r8   rI   )r;   phaser<   r8   s       r2   checkzTimezone.check   s?     I#DJJ/ 	0JC88C/E%L	0r3   c                     t        | j                        D ]&  \  }}|d   |d   k7  s| j                  ||d          ( y)z.Make the changes effect based on `self.value`.beforer5   N)r   r8   setr;   r<   r8   s      r2   changezTimezone.change   sB    #DJJ/ 	0JCX%	"22eI./	0r3   c                 &    | j                  d       y)a  Get the value for the key at the given phase.

        Called from self.check().

        Args:
            key:   The key to get the value
            phase: The phase to get the value

        Return:
            value: The value for the key at the given phase.
        z5get(key, phase) is not implemented on target platformNrD   r;   r<   rS   s      r2   rI   zTimezone.get   s     	

JKr3   c                 &    | j                  d       y)zSet the value for the key (of course, for the phase 'after').

        Called from self.change().

        Args:
            key: Key to set the value
            value: Value to set
        z5set(key, value) is not implemented on target platformNr[   rX   s      r2   rW   zTimezone.set   s     	

JKr3   c                     | j                   d   d   }d|z  }t        j                  j                  |      s| j	                  d|z         |S )Nnamer5   z/usr/share/zoneinfo/%s$given timezone "%s" is not available)r8   ospathisfilerD   )r;   tztzfiles      r2   _verify_timezonezTimezone._verify_timezone  sF    ZZ	*)B.ww~~f%JJ=BCr3   )rV   after)__name__
__module____qualname____doc__r   r6   rD   rM   rQ   rT   rY   rI   rW   rf   __classcell__r1   s   @r2   r
   r
   ]   s=    +?Z$8("00L	Lr3   r
   c                        e Zd ZdZ e ej                  dej                         ej                  dej                              Z edd      Z	 fdZ
d Zd	 Zd
 Z xZS )r   zThis is a Timezone manipulation class for systemd-powered Linux.

    It uses the `timedatectl` command to check/set all arguments.
    z"^\s*RTC in local TZ\s*:\s*([^\s]+)z^\s*Time ?zone\s*:\s*([^\s]+)hwclockr_   zset-local-rtczset-timezonec                     t         t        |   |       |j                  dd      | _        t               | _        d| j                  v r| j                          y y )Nr   Trequiredr_   )	r   r   r6   r   r   r7   statusr8   rf   r;   r*   r1   s     r2   r6   zSystemdTimezone.__init__  sP    ot-f5!..}t.LfTZZ!!#  r3   c                     || j                   vr)| j                  | j                  d      | j                   |<   | j                   |   S )Nrt   )rt   rM   r   r;   rS   s     r2   _get_statuszSystemdTimezone._get_status'  s<    #!%d.>.>!IDKK{{5!!r3   c                     | j                  |      }| j                  |   j                  |      j                  d      }|dk(  r!| j                  j                  |      rd}|S d}|S )N   rp   localUTC)rx   regexpssearchgroupr*   booleanr;   r<   rS   rt   r8   s        r2   rI   zSystemdTimezone.get,  sh    !!%(S!((066q9){{""5)  r3   c                 z    |dk(  r
|dk(  rd}nd}| j                  | j                  | j                  |   |d       y )Nrp   r{   yesnoTrH   )rM   r   subcmdsrX   s      r2   rW   zSystemdTimezone.set7  s?    )T%%t||C'8%TJr3   )rh   ri   rj   rk   r7   r   compile	MULTILINEr}   r   r6   rx   rI   rW   rl   rm   s   @r2   r   r     se    
 

@",,ORZZ8",,GG
 G
$"
	Kr3   r   c                       e Zd ZdZ eddd      Z eddd      Z ed ej                  dej                         ej                  dej                              Z
 e ej                  dej                         ej                  d	ej                        
      Z edd
      Z fdZd Zd Zd Zd Zd Zd Zd Z xZS )r   af  This is a Timezone manipulation class for non systemd-powered Linux.

    For timezone setting, it edits the following file and reflect changes:
        - /etc/sysconfig/clock  ... RHEL/CentOS
        - /etc/timezone         ... Debian/Ubuntu
    For hwclock setting, it executes `hwclock --systohc` command with the
    '--utc' or '--localtime' option.
    Nz/etc/adjtime)r_   rp   adjtimeTz^UTC\s*=\s*([^\s]+)z^(UTC|LOCAL)$z^TIMEZONE\s*=\s*"?([^"\s]+)"?z^ZONE\s*=\s*"?([^"\s]+)"?)SuSEredhatzTIMEZONE="%s"
z
ZONE="%s"
c                 N   t         t        |   |       d}d| j                  v rK| j	                         }| j                  d   d   }| j
                  j                  dd      d|dg| _        | j
                  j                  d	d      | _        t               }d
| j                  d<   t        j                  dt        j                        | j                  d<   d| _        | j
                  j                  d      fd| j                  v rH| j
                  j                  dd      ddd| j
                  j                  dd      z  g| _        d| j                  d	<   y |dk(  s|dk(  r>d| j                  d	<   |dk(  r)| j
                  j                  dd      d|g| _        y y | j
                  j                  d      Bt         j"                  j%                  d      s#| j
                  j                  dd      g| _        d| j                  d<   d| j                  d	<   	 t'        | j                  d   d      5 }|j)                         }d d d        t        j*                  dt        j                        r1| j,                  d   | j                  d<   | j.                  d   | _        y | j,                  d   | j                  d<   | j.                  d   | _        y # 1 sw Y   xY w# t0        $ r}| j3                  |d      rg|dk(  r1| j,                  d   | j                  d<   | j.                  d   | _        n\| j,                  d   | j                  d<   | j.                  d   | _        n&| j5                  d| j                  d   z         Y d }~y Y d }~y Y d }~y d }~ww xY w)N r_   r5   cpTrr   z --remove-destination z /etc/localtimerp   z/etc/timezonez	^([^\s]+)z%s
zdpkg-reconfigurelnz -sf z#%s --frontend noninteractive tzdataz/etc/default/rcSAlpineGentooz/etc/conf.d/hwclockzsetup-timezonez -z ztzdata-update/etc/localtimez/etc/sysconfig/clockrz^TIMEZONE\s*=r   r   z&could not read configuration file "%s")r   r   r6   r8   rf   r*   r   update_timezoneupdate_hwclockr   
conf_filesr   r   r   r}   tzline_formatra   rb   islinkopenreadr~   dist_regexpsdist_tzline_formatIOError_allow_ioerrorrD   )	r;   r*   
planned_tzre   distributionfsysconfig_clockerrr1   s	           r2   r6   zNosystemdTimezone.__init__h  sn   /7
TZZ**,FF+I6J TXS^S^SkSklp{Sk  TA  CI  %J  $KD "kk66y46P')"1!zz,EV#;;##$67C#FJkkF^F^_cnrF^Fsu{(|(MPTP[P[PhPhi{  GKPh  QL  )L(M$);DOOI&X%)A)>DOOI&x'6:kk6N6NO_jn6N6oq{(|'}$ ( {{''8D ww~~&67,0KK,D,D__c,D,d+eD( '=DOOF#)?DOOI&K$//&137 /1&'ffhO/& 99-M+/+<+<V+DDLL()-)@)@)HD& ,0+<+<X+FDLL()-)@)@)JD&5/ / c&&sF3#v-/3/@/@/HV,-1-D-DV-L* 04/@/@/JV,-1-D-DX-N*JJG$//Z`Jaabb +	 +cs1   (K- K!K- !K*&K- -	N$6BNN$c                 v    |j                   t         j                  k7  ry| j                  j                  |d      S )NF)errnoENOENTallow_no_filerI   )r;   r   r<   s      r2   r   z NosystemdTimezone._allow_ioerror  s2     99$ !!%%c511r3   c                    	 t        |d      5 }|j                         }ddd       g }t              D ](  \  }	}
|j                  |
      s|j                  |	       * t        |      dkD  r|d   }nd}|ddd   D ]  }	||	=  |j                  ||       	 t        |d      5 }|j                  |       ddd       | j                  j                  d
t        |      d|       y# 1 sw Y   xY w# t        $ r8}| j                  ||      rg }n| j	                  d|d|d       Y d}~
d}~ww xY w# 1 sw Y   xY w# t        $ r | j	                  d|d|d	       Y w xY w)a  Replace the first matched line with given `value`.

        If `regexp` matched more than once, other than the first line will be deleted.

        Args:
            filename: The name of the file to edit.
            regexp:   The regular expression to search with.
            value:    The line which will be inserted.
            key:      For what key the file is being edited.
        r   Ntried to configure  using a file "", but could not read itr   wz", but could not write to itzAdded 1 line and deleted z line(s) on )r   	readlinesr   r   rD   	enumerater~   r@   r?   insert
writelinesr   )r;   filenameregexpr8   r<   filelinesr   matched_indicesilineinsert_lines               r2   
_edit_filezNosystemdTimezone._edit_file  sh   	oh$ )()  ' 	*GAt}}T"&&q)	* !#)!,KK 2& 	Aa	 	[%(	oh$ '&' 	s?G[]efg7) ) 	o""3,

_bdlmn		o*' ' 	oJJ_bdlmn	osW   C5 C)C5 E $D96E )C2.C5 5	D6>-D11D69E>E !E)(E)c                    | j                   |   }	 t        |d      5 }|j                         }d d d        	 | j                  |   j	                        j                  d      }|dk(  r#| j                  j                  |      rd}|S d}|S |dk(  r|dk7  r|j                         }|S |S # 1 sw Y   }xY w# t        $ r6 |dk(  rY y|dk(  rY y|d	k(  r|d
k(  rY y| j                  d|d|d       Y S w xY w# t        $ rS}| j                  ||      r|dk(  rY d }~y|dk(  rY d }~y|d	k(  rY d }~y| j                  d|d|d       Y d }~S d }~ww xY w)Nr   )moderz   rp   r|   r{   r   n/ar_   rV   r   r   z)", but could not find a valid value in itr   )r   r   r   r}   r~   r   r*   r   lowerAttributeErrorrD   r   r   )r;   r<   rS   r   r   rt   r8   r   s           r2   _get_value_from_configz(NosystemdTimezone._get_value_from_config  s   ??3')	.hS) %T%.S)008>>qA" )#{{**51 %  !(
 	 I%~ %uS% % " H)# I% !F](  %

x{  ~F  $G  H 5H  		o""3,)# I% F] 

_bdlmn< O		osb   C9 B+C9 -B7 +B40C9 7C6C6
C6C65C69	EEE(E2EEc                    | j                   |   d   }|dk(  r+| j                  ||      }||k(  r| j                  d|      }|S |dk(  r| j                  ||      }||k(  rt        j                  j	                  d      r}t        j                  j                  d      rZt        j                  d      }t        j                  d|t        j                        }|r|j                  d      }||k7  r*|}|S d}|S d}|S 	 t        j                  dd	|z         sy	 |S |S | j                  d
|z         S # t        $ r Y yw xY w)Nr5   rp   r   r_   r   z$(?:/(?:usr/share|etc)/zoneinfo/)(.*)rz   r   /usr/share/zoneinfo/unknown parameter "%s")r8   r   ra   rb   r   existsreadlinkr   r~   r   r   filecmpcmp	ExceptionrD   )r;   r<   rS   r5   r8   rb   linktz	valuelinks           r2   rI   zNosystemdTimezone.get  sf   **S/),)//U;E 33IuEL K F]//U;E 77>>"23 ww~~&67  "{{+;< "$+RTXZ\ZfZf!g!(.QI(G3(1$  %*E  !& %&{{+;=SV]=]^#(  _ u JJ/#56	 % %$%s   <D1 1	D=<D=c                     | j                  | j                  d   | j                  d   | j                  |z  d       | j                  D ]  }| j                  |        y )Nr_   r   r   r8   r<   )r   r   r}   r   r   rM   )r;   r8   cmds      r2   set_timezonezNosystemdTimezone.set_timezone<  s^    !8#||F3"0058" 	 	$ '' 	CLL	r3   c                     |dk(  rd}d}nd}d}| j                   d   2| j                  | j                   d   | j                  d   d|z  d       | j                  | j                  d	|d
       y )Nr{   z--localtimer   z--utcr   rp   zUTC=%s
r   z	--systohcTr   )r   r   r}   rM   r   )r;   r8   optionutcs       r2   set_hwclockzNosystemdTimezone.set_hwclockD  s|    G"FCFC??9%1OOT__Y%?#'<<	#:",s"2 )  + 	T((+v4Hr3   c                     |dk(  r| j                  |       y |dk(  r| j                  |       y | j                  d|z         y )Nr_   rp   r   )r   r   rD   rX   s      r2   rW   zNosystemdTimezone.setR  s?    &=e$IU#JJ/#56r3   )rh   ri   rj   rk   r7   r   r   r   r   r   r}   r   r   r6   r   r   r   rI   r   r   rW   rl   rm   s   @r2   r   r   A  s     J M 

12<<@

+R\\:G RZZ8",,Grzz6EL
 
AKF2(hT,\-^I7r3   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )r#   a  This is a Timezone manipulation class for SmartOS instances.

    It uses the C(sm-set-timezone) utility to set the timezone, and
    inspects C(/etc/default/init) to determine the current timezone.

    NB: A zone needs to be rebooted in order for the change to be
    activated.
    c                     t         t        |   |       | j                  j	                  dd      | _        | j
                  s|j                  d       y y )Nzsm-set-timezoneFrr   zFsm-set-timezone not found. Make sure the smtools package is installed.r   )r   r#   r6   r*   r   settimezoner"   ru   s     r2   r6   zSmartOSTimezone.__init__e  sP    ot-f5;;334EPU3V!ij  r3   c                    |dk(  rd	 t        dd      5 }|D ]F  }t        j                  d|j                               }|s*|j	                         d   c cddd       S  	 ddd       yy| j                  j                  d	|z         y# 1 sw Y   *xY w# t
        $ r | j                  j                  d       Y yw xY w)
zLookup the current timezone name in `/etc/default/init`. If anything else
        is requested, or if the TZ field is not set we fail.
        r_   z/etc/default/initr   	^TZ=(.*)$r   Nz Failed to read /etc/default/initr   /%s is not a supported option on target platform)r   r   r   r!   groupsr   r*   r"   )r;   r<   rS   r   r   ms         r2   rI   zSmartOSTimezone.getk  s     &=N-s3 1q ! 1HH[$**,?#$88:a=0	1 111 1 KK!!&WZ]&]!^1 1
  N%%*L%MNs:   B ,B
 B
	B B
 B 
BB %B>=B>c                    |dk(  rd|z  }| j                   j                  |      \  }}}|dk7  r| j                   j                  |       t        j                  d|z  |j                         d         }|r|j                         d   |k(  s| j                   j                  d       y
y
| j                   j                  d	|z         y
)zSet the requested timezone through sm-set-timezone, an invalid timezone name
        will be rejected and we have no further input validation to perform.
        r_   zsm-set-timezone %sr   r   z'^\* Changed (to)? timezone (to)? (%s).*rz   r   zFailed to set timezoner   N)r*   r   r"   r   r   
splitlinesr   )r;   r<   r8   r   r+   r,   r-   r   s           r2   rW   zSmartOSTimezone.set{  s     &=&.C#';;#:#:3#? RQw%%&%1 CeKVM^M^M`abMcdA!((*R.E1%%*B%C 2 KK!!&WZ]&]!^r3   )rh   ri   rj   rk   r6   rI   rW   rl   rm   s   @r2   r#   r#   [  s    k_ _r3   r#   c                        e Zd ZdZ e ej                  dej                              Z fdZ	d Z
d Zd Zd Z xZS )	r$   zThis is the timezone implementation for Darwin which, unlike other *BSD
    implementations, uses the `systemsetup` command on Darwin to check/set
    the timezone.
    z^\s*Time ?Zone\s*:\s*([^\s]+))r_   c                     t         t        |   |       |j                  dd      | _        t               | _        d| j                  v r| j                          y y )NsystemsetupTrr   r_   )	r   r$   r6   r   r   r7   rt   r8   rf   ru   s     r2   r6   zDarwinTimezone.__init__  sP    nd,V4!..}t.LfTZZ!!#  r3   c                     || j                   vr)| j                  | j                  d      | j                   |<   | j                   |   S )z;Lookup the current timezone via `systemsetup -gettimezone`.z-gettimezone)rt   rM   r   rw   s     r2   _get_current_timezonez$DarwinTimezone._get_current_timezone  s<    #!%d.>.>!ODKK{{5!!r3   c                     | j                   d   d   }| j                  | j                  d      j                         dd  }t	        t        d |            }||vr| j                  d|z         |S )Nr_   r5   z-listtimezonesrz   c                 "    | j                         S N)r!   )xs    r2   <lambda>z1DarwinTimezone._verify_timezone.<locals>.<lambda>  s    QWWY r3   r`   )r8   rM   r   r   listmaprD   )r;   rd   outtz_lists       r2   rf   zDarwinTimezone._verify_timezone  so    ZZ	* ll4++-=>IIKABOs.45WJJ=BC	r3   c                     |dk(  r@| j                  |      }| j                  |   j                  |      j                  d      }|S | j                  j                  d|z         y )Nr_   rz   r   r   )r   r}   r~   r   r*   r"   r   s        r2   rI   zDarwinTimezone.get  s^    &=//6FLL%,,V4::1=ELKK!!&WZ]&]!^r3   c                     |dk(  r | j                  | j                  d|d       y | j                  j                  d|z         y )Nr_   z-settimezoneTr   r   r   )rM   r   r*   r"   rX   s      r2   rW   zDarwinTimezone.set  s?    &=LL))>5dLKKK!!&WZ]&]!^r3   )rh   ri   rj   rk   r7   r   r   r   r}   r6   r   rf   rI   rW   rl   rm   s   @r2   r$   r$     sD    
 RZZ8",,GG$"__r3   r$   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )r%   zThis is the timezone implementation for *BSD which works simply through
    updating the `/etc/localtime` symlink to point to a valid timezone name under
    `/usr/share/zoneinfo`.
    c                 ,    t         t        |   |       y r   )r   r%   r6   ru   s     r2   r6   zBSDTimezone.__init__  s    k4)&1r3   c                    d}d}t         j                  j                  |      s| j                  j	                  d       y|}|j                  |      s(	 t        j                  |      }|j                  |      s(|j                  |d      S # t        $ r Y nw xY wt        t        j                  |            D ]  \  }}}t        |      D ]n  }t         j                  j                  ||      }t         j                  j                  |      rCt        j                  ||      sZ|j                  |d      c c S   | j                  j	                  d       y)Nr   r   z,Could not read /etc/localtime. Assuming UTC.r|   r   zCCould not identify timezone name from /etc/localtime. Assuming UTC.)ra   rb   r   r*   warn
startswithr   OSErrorreplacesortedwalkrB   r   r   r   )r;   zoneinfo_dirlocaltime_filezoneinfo_filednamer/   fnamesfnames           r2   __get_timezonezBSDTimezone.__get_timezone  s/   -) ww~~n-KKKL '**<8 "N ;  **<8 !((r::	   %+277<+@$A 	C E5& C "UE :ww~~m4]Tb9c(00rBBC	C 	^_s   B 	BBc                 l    |dk(  r| j                         S | j                  j                  d|z         y)z:Lookup the current timezone by resolving `/etc/localtime`.r_   r   r   N)_BSDTimezone__get_timezoner*   r"   r\   s      r2   rI   zBSDTimezone.get  s4    &=&&((KK!!&WZ]&]!^r3   c           	         |dk(  rd|z   }	 t         j                  j                  |      s| j                  j	                  d|z         dj                  t        dd      D cg c]6  }t        j                  t        j                  t        j                  z         8 c}      }d	|z   }	 t        j                  ||       t        j                  |d
       y | j                  j	                  d|z         y # t
        $ r" | j                  j	                  d|z         Y w xY wc c}w # t
        $ r4 t        j                  |       | j                  j	                  d       Y y w xY w)Nr_   r   z%s is not a recognized timezoner   zFailed to stat %sr   r   
   z/etc/localtime.r   zCould not update /etc/localtimer   )ra   rb   rc   r*   r"   r   rB   rangerandomchoicestringascii_lettersdigitssymlinkrenameremove)r;   r<   r8   zonefiler   suffixnew_localtimes          r2   rW   zBSDTimezone.set  s8   &= .5HJww~~h/KK)).ORW.W)X WW[`abdf[ghVWfmmF,@,@6==,PQhiF-6MM

8]3		-)9:
 KK!!&WZ]&]!^%  J%%*=*H%IJ i  M		-(%%*K%LMs)   >C8 #;D&+,D+ 8(D#"D#+:E('E()	rh   ri   rj   rk   r6   r   rI   rW   rl   rm   s   @r2   r%   r%     s    
2"H__r3   r%   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )r(   a  This is a Timezone manipulation class for AIX instances.

    It uses the C(chtz) utility to set the timezone, and
    inspects C(/etc/environment) to determine the current timezone.

    While AIX time zones can be set using two formats (POSIX and
    Olson) the preferred method is Olson.
    See the following article for more information:
    https://developer.ibm.com/articles/au-aix-posix/

    NB: AIX needs to be rebooted in order for the change to be
    activated.
    c                 p    t         t        |   |       | j                  j	                  dd      | _        y )NchtzTrr   )r   r(   r6   r*   r   r   ru   s     r2   r6   zAIXTimezone.__init__!  s.    k4)&1;;33FT3Jr3   c                 .   	 t        dd      5 }|j                         }ddd       t        j                  dt
        j                        }|r|j                  d      S y# 1 sw Y   BxY w# t        $ r | j                  j	                  d       Y lw xY w)z5 Return the current value of TZ= in /etc/environment z/etc/environmentr   Nz*Issue reading contents of /etc/environmentr   r   rz   )	r   r   r   r*   r"   r   r~   r   r   )r;   r   etcenvironmentr   s       r2   r   zAIXTimezone.__get_timezone%  s    	T(#. *!!"*
 		,E;;q>!* * 	TKK!!&R!S	Ts'   A, A A,  A)%A, ,%BBc                 l    |dk(  r| j                         S | j                  j                  d|z         y)zLookup the current timezone name in `/etc/environment`. If anything else
        is requested, or if the TZ field is not set we fail.
        r_   r   r   N)_AIXTimezone__get_timezoner*   r"   r\   s      r2   rI   zAIXTimezone.get3  s6     &=&&((KK!!&WZ]&]!^r3   c                 2   |dk(  rd|z   }	 t         j                  j                  |      s| j                  j	                  d|z         d|z  }| j                  j                  |      \  }}}|dk7  r| j                  j	                  |       | j                         }||k7  r&d|d	|d
}	| j                  j	                  |	       yy| j                  j	                  d|z         y# t
        $ r" | j                  j	                  d|z         Y w xY w)zSet the requested timezone through chtz, an invalid timezone name
        will be rejected and we have no further input validation to perform.
        r_   z/usr/share/lib/zoneinfo/z %s is not a recognized timezone.r   zFailed to check %s.zchtz %sr   z-TZ value does not match post-change (Actual: z, Expected: z).r   N)ra   rb   rc   r*   r"   r   r   r  )
r;   r<   r8   r  r   r+   r,   r-   TZr   s
             r2   rW   zAIXTimezone.set<  s    &=  2E9HLww~~h/KK)).PSX.X)Y
 e#C#';;#:#:3#? RQw%%&%1 $$&BU{[]_de%%#%. 
 KK!!&WZ]&]!^%  L%%*?(*J%KLs   >C+ +(DD)	rh   ri   rj   rk   r6   r  rI   rW   rl   rm   s   @r2   r(   r(     s    K_*_r3   r(   c            	         t        t        t        dddgdg      t        d            dd	ggd
      } t        |       }|j                  d       | j                  r'|j                  dd      }|j                  d      |d<   n|j                          |j                  d       |j                  dd      j                         \  }}||k7  r)|j                  dt        |      dt        |             |j                  dd      }|d   |d   k7  }t        |j                        dkD  r.| j                  ||dj                  |j                               y | j                  ||       y )Nstrr{   r|   rtc)typechoicesaliases)r  ro   rp   r_   T)r9   required_one_ofsupports_check_moderV   )rS   r5   rg   z=still not desired state, though changes have made - planned: z	, after: r   r>   )changedrQ   r   )r   rQ   )r   r7   r
   rT   
check_moderQ   poprY   valuesrD   r  r?   r   	exit_jsonrB   )r*   rd   rQ   rg   r5   r   s         r2   mainr%  i  sO   egu-=wO5!

 
 !	F 
&	B HH8Hwwx++W 			
w777I6==?GHH14Ws5zK Lwwx)H~g.G
266{Qt2669JKt4r3   __main__)
__future__r   r   r   r  __metaclass__DOCUMENTATIONRETURNEXAMPLESr   ra   r   r  r   r  r   ansible.module_utils.basicr   r   ansible.module_utils.sixr   objectr
   r   r   r#   r$   r%   r(   r%  rh    r3   r2   <module>r0     s    A @/b
  	   	   F .ov od/Kh /KdW7 W7t2_h 2_j._X ._bN_( N_bT_( T_n$5N zF r3   