Merge commit 'origin/master' into tmbinc/FixTimingBugs
[enigma2.git] / lib / python / Components / Timezones.py
1 from xml.sax import make_parser
2 from xml.sax.handler import ContentHandler
3
4 from os import environ, unlink, symlink
5 import time
6
7 class Timezones:
8         class parseTimezones(ContentHandler):
9                 def __init__(self, timezones):
10                         self.isPointsElement, self.isReboundsElement = 0, 0
11                         self.timezones = timezones
12         
13                 def startElement(self, name, attrs):
14                         if (name == "zone"):
15                                 self.timezones.append((attrs.get('name',""), attrs.get('zone',"")))
16         
17         def __init__(self):
18                 self.timezones = []
19                 
20                 self.readTimezonesFromFile()
21
22         def readTimezonesFromFile(self):
23                 parser = make_parser()
24                 
25                 try:
26                         timezonesHandler = self.parseTimezones(self.timezones)
27                         parser.setContentHandler(timezonesHandler)
28                         parser.parse('/etc/timezone.xml')
29                 except:
30                         pass
31                 
32                 if len(self.timezones) == 0:
33                         self.timezones = [("UTC", "UTC")]
34                 
35         def activateTimezone(self, index):
36                 if len(self.timezones) <= index:
37                         return
38                 
39                 environ['TZ'] = self.timezones[index][1]
40                 try:
41                         unlink("/etc/localtime")
42                 except OSError:
43                         pass
44                 try:
45                         symlink("/usr/share/zoneinfo/%s" %(self.timezones[index][1]), "/etc/localtime")
46                 except OSError:
47                         pass
48                 try:
49                         time.tzset()
50                 except:
51                         from enigma import e_tzset
52                         e_tzset()
53                 
54         def getTimezoneList(self):
55                 return [ str(x[0]) for x in self.timezones ]
56
57         def getDefaultTimezone(self):
58                 # TODO return something more useful - depending on country-settings?
59                 t = "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Vienna"
60                 for (a,b) in self.timezones:
61                         if a == t:
62                                 return a
63                 return self.timezones[0][0]
64
65 timezones = Timezones()