refs bug #429
[enigma2.git] / lib / python / Components / Timezones.py
index 90b227f..7f70915 100644 (file)
@@ -1,6 +1,9 @@
 from xml.sax import make_parser
 from xml.sax.handler import ContentHandler
 
+from os import environ, unlink, symlink
+import time
+
 class Timezones:
        class parseTimezones(ContentHandler):
                def __init__(self, timezones):
@@ -8,26 +11,55 @@ class Timezones:
                        self.timezones = timezones
        
                def startElement(self, name, attrs):
-                       print "Name: " + str(name)
                        if (name == "zone"):
-                               self.timezones[attrs.get('name',"")] = attrs.get('zone',"")
-                               #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
-                               #tpos = attrs.get('position',"")
-                               #tname = attrs.get('name',"")
-                               #self.satellites[tpos] = tname
-                               #self.satList.append( (tname, tpos) )
-                               #self.parsedSat = int(tpos)
+                               self.timezones.append((attrs.get('name',""), attrs.get('zone',"")))
        
        def __init__(self):
-               self.timezones = {}
+               self.timezones = []
                
                self.readTimezonesFromFile()
 
        def readTimezonesFromFile(self):
                parser = make_parser()
-               timezonesHandler = self.parseTimezones(self.timezones)
-               parser.setContentHandler(timezonesHandler)
-               parser.parse('/etc/timezone.xml')
                
+               try:
+                       timezonesHandler = self.parseTimezones(self.timezones)
+                       parser.setContentHandler(timezonesHandler)
+                       parser.parse('/etc/timezone.xml')
+               except:
+                       pass
+               
+               if len(self.timezones) == 0:
+                       self.timezones = [("UTC", "UTC")]
+               
+       def activateTimezone(self, index):
+               if len(self.timezones) <= index:
+                       return
+               
+               environ['TZ'] = self.timezones[index][1]
+               try:
+                       unlink("/etc/localtime")
+               except OSError:
+                       pass
+               try:
+                       symlink("/usr/share/zoneinfo/%s" %(self.timezones[index][1]), "/etc/localtime")
+               except OSError:
+                       pass
+               try:
+                       time.tzset()
+               except:
+                       from enigma import e_tzset
+                       e_tzset()
+               
+       def getTimezoneList(self):
+               return [ str(x[0]) for x in self.timezones ]
+
+       def getDefaultTimezone(self):
+               # TODO return something more useful - depending on country-settings?
+               t = "(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Vienna"
+               for (a,b) in self.timezones:
+                       if a == t:
+                               return a
+               return self.timezones[0][0]
 
 timezones = Timezones()