File: Locale.py

package info (click to toggle)
egenix-mx-base 2.0.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,028 kB
  • ctags: 4,762
  • sloc: ansic: 14,965; python: 11,739; sh: 313; makefile: 117
file content (129 lines) | stat: -rw-r--r-- 3,919 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#-*- coding: latin-1 -*-
""" Locale dependant formatting and parsing.

    XXX This module still has prototype status and is undocumented.

    XXX Check the spelling. 
    XXX Check the string format.

    Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
    Copyright (c) 2000-2001, eGenix.com Software GmbH; mailto:info@egenix.com
    See the documentation for further information on copyrights,
    or contact the author. All Rights Reserved.
"""

def _make_dict(*names):

    """ Helper to create a dictionary mapping indices to
        names and names to indices.
    """
    d = {}
    for i in range(len(names)):
        d[names[i]] = i
        d[i] = names[i]
    return d

class _TimeLocale:

    """ Base class for locale dependend formatting and parsing.
    """
    # Examples:
    Weekday = _make_dict('Monday','Tuesday','Wednesday','Thursday','Friday',
                            'Saturday','Sunday')
    Month = _make_dict(None,
             'January','February','March','April','May','June',
             'July','August','September','October','November','December')

    # Instance variables
    MonthNames = ()
    WeekdayNames = ()

    def __init__(self):

        """ Init. the instance variables.
        """
        l = []
        for i in range(1,13):
            l.append(self.Month[i])
        self.MonthNames = tuple(l)

        l = []
        for i in range(7):
            l.append(self.Weekday[i])
        self.WeekdayNames = tuple(l)

    def str(self,d):
        
        """str(datetime)

           Return the given DateTime instance formatted according to
           the locale's convention. Timezone information is not
           presented.

        """
        return '%s %02i %s %04i %02i:%02i:%02i' % (
            self.Weekday[d.day_of_week],
            d.day,self.Month[d.month],d.year,
            d.hour,d.minute,d.second)

    # Alias
    ctime = str

# Singletons that implement the specific methods.

class English(_TimeLocale):
    Weekday = _make_dict('Monday','Tuesday','Wednesday','Thursday','Friday',
                            'Saturday','Sunday')
    Month = _make_dict(None,
             'January','February','March','April','May','June',
             'July','August','September','October','November','December')

English = English()

class German(_TimeLocale):
    Weekday = _make_dict('Montag','Dienstag','Mittwoch','Donnerstag',
                            'Freitag','Samstag','Sonntag')
    Month = _make_dict(None,
             'Januar','Februar','Mrz','April','Mai','Juni',
             'Juli','August','September','Oktober','November','Dezember')

German = German()

class French(_TimeLocale):
    Weekday = _make_dict('lundi','mardi','mercredi','jeudi',
                         'vendredi','samedi','dimanche')
    Month = _make_dict(None,
             'janvier','fvrier','mars','avril','mai','juin',
             'juillet','aot','septembre','octobre','novembre','dcembre')

French = French()

class Spanish(_TimeLocale):
    Weekday = _make_dict('lunes','martes','miercoles','jueves','viernes',
                         'sabado','domingo')
    Month = _make_dict(None,
             'enero','febrero','marzo','abril','mayo','junio',
             'julio','agosto','septiembre','octubre','noviembre','diciembre')

Spanish = Spanish()

class Portuguese(_TimeLocale):
    Weekday = _make_dict('primeira feira', 'segunda feira','terceira feira',
                         'cuarta feira','quinta feira','sabado','domingo')
    Month = _make_dict(None,
             'janeiro','fevereiro','mar','abril','maio','junho',
             'julho','agosto','septembro','outubro','novembro','dezembro')

Portuguese = Portuguese()

###

def _test():

    import DateTime
    d = DateTime.now()
    for lang in (English,German,French,Spanish,Portuguese):
        print lang.__class__.__name__,':',lang.ctime(d)

if __name__ == '__main__':
    _test()