nicer
[enigma2.git] / lib / python / Components / Timezones.py
1 from xml.sax import make_parser
2 from xml.sax.handler import ContentHandler
3
4 import os
5 import time
6
7 from enigma import *
8
9 class Timezones:
10         class parseTimezones(ContentHandler):
11                 def __init__(self, timezones):
12                         self.isPointsElement, self.isReboundsElement = 0, 0
13                         self.timezones = timezones
14         
15                 def startElement(self, name, attrs):
16                         if (name == "zone"):
17                                 self.timezones.append((attrs.get('name',""), attrs.get('zone',"")))
18         
19         def __init__(self):
20                 self.timezones = []
21                 
22                 self.readTimezonesFromFile()
23
24         def readTimezonesFromFile(self):
25                 parser = make_parser()
26                 
27                 try:
28                         timezonesHandler = self.parseTimezones(self.timezones)
29                         parser.setContentHandler(timezonesHandler)
30                         parser.parse('/etc/timezone.xml')
31                 except:
32                         pass
33                 
34                 if len(self.timezones) == 0:
35                         self.timezones = [("UTC", "UTC")]
36                 
37         def activateTimezone(self, index):
38                 if len(self.timezones) <= index:
39                         return
40                 
41                 os.environ['TZ'] = self.timezones[index][1]
42                 try:
43                         time.tzset()
44                 except:
45                         etimezone()
46                 
47         def getTimezoneList(self):
48                 list = []
49                 for x in self.timezones:
50                         list.append(str(x[0]))
51                 return list
52         
53         def getDefaultTimezone(self):
54                 # TODO return something more useful - depending on country-settings?
55                 return 27
56
57 timezones = Timezones()