Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extensions/cpsection/datetime/model.py
blob: 84e12598227531c8ce5e26821f1d84e63d933528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2007, 2008 One Laptop Per Child
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
# The timezone config is based on the system-config-date
# (http://fedoraproject.org/wiki/SystemConfig/date) tool.
# Parts of the code were reused.
#

import os
from gettext import gettext as _
import gconf

_zone_tab = '/usr/share/zoneinfo/zone.tab'


def _initialize():
    """Initialize the docstring of the set function"""
    if set_timezone.__doc__ is None:
        # when running under 'python -OO', all __doc__ fields are None,
        # so += would fail -- and this function would be unnecessary anyway.
        return
    timezones = read_all_timezones()
    for timezone in timezones:
        set_timezone.__doc__ += timezone + '\n'


def read_all_timezones(fn=_zone_tab):
    fd = open(fn, 'r')
    lines = fd.readlines()
    fd.close()
    timezones = []
    for line in lines:
        if line.startswith('#'):
            continue
        line = line.split()
        if len(line) > 1:
            timezones.append(line[2])
    timezones.sort()

    for offset in xrange(-12, 13):
        if offset < 0:
            tz = 'GMT%d' % offset
        elif offset > 0:
            tz = 'GMT+%d' % offset
        else:
            tz = 'GMT'
        timezones.append(tz)
    for offset in xrange(-12, 13):
        if offset < 0:
            tz = 'UTC%d' % offset
        elif offset > 0:
            tz = 'UTC+%d' % offset
        else:
            tz = 'UTC'
        timezones.append(tz)
    return timezones


def get_timezone():
    client = gconf.client_get_default()
    return client.get_string('/desktop/sugar/date/timezone')


def print_timezone():
    print get_timezone()


def set_timezone(timezone):
    """Set the system timezone
    timezone : e.g. 'America/Los_Angeles'
    """
    timezones = read_all_timezones()
    if timezone in timezones:
        os.environ['TZ'] = timezone
        client = gconf.client_get_default()
        client.set_string('/desktop/sugar/date/timezone', timezone)
    else:
        raise ValueError(_('Error timezone does not exist.'))
    return 1

# inilialize the docstrings for the timezone
_initialize()