Cas particulier pour le Vietnam (merci Microsoft, encore)…
[auf-refer.git] / auf-employes-timezones
CommitLineData
4469f1d5
P
1#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3"""Outil de calcul des fuseaux horaires des employés AUF.
4
5Copyright ©2017 Agence universitaire de la Francophonie
6Licence : GPL version 3
7Auteur : Progfou <jean-christophe.andre@auf.org>
8
9Debian-Depends: python3, python3-tz, auf-refer
10"""
11import sys
12import os.path
13import urllib
14import xml.etree.ElementTree as etree
15import aufrefer
16import pytz # country_timezones
17
18def print_error(*k, **kv):
19 print("\033[1;31m", *k, "\033[m", **kv, sep='', file=sys.stderr)
20
21def get_tz_from_address(country, city=None):
22 tz = pytz.country_timezones(country.upper())
23 if len(tz) == 1:
24 tz = tz[0]
25 elif city == u'Montr\xe9al':
26 tz = u'America/Toronto'
27 elif city == u'Kinshasa':
28 tz = u'Africa/Kinshasa'
29 elif city == u'Lubumbashi':
30 tz = u'Africa/Lubumbashi'
31 elif city == u'S\xe3o Paulo':
32 tz = u'America/Sao_Paulo'
33 else: # show anomalies
34 print_error("Erreur TZ : county='{}',city='{}' => tz='{}'".format(country, city, tz))
35 tz = None
36 return tz
37
38#
39# récupération des fuseaux horaires « à la Microsoft », cf :
40# http://www.unicode.org/cldr/charts/latest/supplemental/zone_tzid.html
41#
42
43microsoft_timezones = {}
44_windowsZones_url = "http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml"
45_windowsZones_filename = 'windowsZones.xml'
46if not os.path.exists(_windowsZones_filename):
47 with urllib.request.urlopen(_windowsZones_url) as u:
48 with open(_windowsZones_filename, 'wb') as f:
49 f.write(u.read())
50if os.path.exists(_windowsZones_filename):
51 tree = etree.parse(_windowsZones_filename)
52 for e in tree.findall('.//mapZone'):
53 for tz in e.get('type').split():
54 microsoft_timezones[tz] = e.get('other')
13bfe633
P
55# cas particuliers… (merci Microsoft, encore)
56microsoft_timezones['Asia/Ho_Chi_Minh'] = microsoft_timezones['Asia/Saigon']
4469f1d5
P
57
58implantations = {
59 x['id']: x for x in aufrefer.get('datamaster-implantation.json')
60}
61
62print('"Email";"IANA Timezone";"Microsoft Timezone"')
63for employe in aufrefer.get('datamaster-employe.json'):
64 if not employe['courriel']:
65 print_error("Employé sans courriel : {prenom} {nom} ({id})".format(**employe))
66 continue
67 try:
68 implantation = implantations[employe['implantation_physique']]
69 except:
70 print_error("Implantation inexistante ({implantation_physique})"
71 " pour l'employé : {prenom} {nom} ({id})".format(**employe))
72 continue
73 # enrichissement dynamique des données d'implantations
74 if 'tz' not in implantation:
75 implantation['tz'] = get_tz_from_address(
76 country=implantation['adresse_physique_pays'],
77 city=implantation['adresse_physique_ville'] )
78 # résultat
79 print('"{}";"{}";"{}"'.format(employe['courriel'], implantation['tz'],
80 microsoft_timezones.get(implantation['tz'], None)) )