~gnome15-team/gnome15/trunk

« back to all changes in this revision

Viewing changes to gnome15/src/main/python/gnome15/g15locale.py

  • Committer: tanktarta
  • Date: 2012-11-24 10:27:36 UTC
  • Revision ID: tanktarta-20121124102736-0drhasy3jdn862wx
0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import locale
33
33
import gettext
34
34
import g15globals
 
35
import g15util
 
36
import time
 
37
import datetime
35
38
 
36
39
# Change this variable to your app name!
37
40
#  The translation files will be under
65
68
 
66
69
# Cached translations
67
70
__translations = {}
 
71
 
 
72
# Replace these date/time formats to get a format without seconds
 
73
REPLACE_FORMATS = [
 
74
        (u'.%S', u''),
 
75
        (u':%S', u''),
 
76
        (u',%S', u''),
 
77
        (u' %S', u''),
 
78
        (u':%OS', ''),
 
79
        (u'%r', '%I:%M %p'),
 
80
        (u'%t', '%H:%M'),
 
81
        (u'%T', '%H:%M')
 
82
    ]
 
83
 
 
84
def format_time(time_val, gconf_client, display_seconds = True, show_timezone = False, compact = True):
 
85
    """
 
86
    Format a given time / datetime as a time in the 12hour format. GConf
 
87
    is checked for custom format, otherwise the default for the locale is
 
88
    used.
 
89
    
 
90
    Keyword arguments:
 
91
    time_val         --    time / datetime object
 
92
    gconf_client     --    gconf client instance
 
93
    display_seconds  --    if false, seconds will be stripped from result
 
94
    """
 
95
    fmt = g15util.get_string_or_default(gconf_client, 
 
96
                                        "/apps/gnome15/time_format", 
 
97
                                        locale.nl_langinfo(locale.T_FMT_AMPM))
 
98
    if not display_seconds:
 
99
        fmt = __strip_seconds(fmt)
 
100
    if isinstance(time_val, time.struct_time):
 
101
        time_val = datetime.datetime(*time_val[:6])
 
102
    
 
103
    if not show_timezone:
 
104
        fmt = fmt.replace("%Z", "")
 
105
    
 
106
    if compact:
 
107
        fmt = fmt.replace(" %p", "%p")
 
108
        fmt = fmt.replace(" %P", "%P")
 
109
        
 
110
    fmt = fmt.strip()
 
111
 
 
112
    if isinstance(time_val, tuple):
 
113
        return time.strftime(fmt, time_val)
 
114
    else:
 
115
        return time_val.strftime(fmt)
 
116
 
 
117
def format_time_24hour(time_val, gconf_client, display_seconds = True, show_timezone = False):
 
118
    """
 
119
    Format a given time / datetime as a time in the 24hour format. GConf
 
120
    is checked for custom format, otherwise the default for the locale is
 
121
    used.
 
122
    
 
123
    Keyword arguments:
 
124
    time_val         --    time / datetime object / tuple
 
125
    gconf_client     --    gconf client instance
 
126
    display_seconds  --    if false, seconds will be stripped from result
 
127
    """    
 
128
    fmt = g15util.get_string_or_default(gconf_client, "/apps/gnome15/time_format_24hr", locale.nl_langinfo(locale.T_FMT))
 
129
    if not display_seconds:
 
130
        fmt = __strip_seconds(fmt)
 
131
    if isinstance(time_val, time.struct_time):
 
132
        time_val = datetime.datetime(*time_val[:6])
 
133
        
 
134
    if not show_timezone:
 
135
        fmt = fmt.replace("%Z", "")
 
136
    fmt = fmt.strip()
 
137
    
 
138
    if isinstance(time_val, tuple):
 
139
        return time.strftime(fmt, time_val)
 
140
    else:
 
141
        return time_val.strftime(fmt)
 
142
 
 
143
def format_date(date_val, gconf_client):
 
144
    """
 
145
    Format a datetime as a date (without time). GConf
 
146
    is checked for custom format, otherwise the default for the locale is
 
147
    used.
 
148
    
 
149
    Keyword arguments:
 
150
    date_val         --    date / datetime object
 
151
    gconf_client     --    gconf client instance
 
152
    """    
 
153
    fmt = g15util.get_string_or_default(gconf_client, "/apps/gnome15/date_format", locale.nl_langinfo(locale.D_FMT))
 
154
    if isinstance(date_val, tuple):
 
155
        return datetime.date.strftime(fmt, date_val)
 
156
    else:
 
157
        return date_val.strftime(fmt)
 
158
 
 
159
def format_date_time(date_val, gconf_client, display_seconds = True):
 
160
    """
 
161
    Format a datetime as a date and a time. GConf
 
162
    is checked for custom format, otherwise the default for the locale is
 
163
    used.
 
164
    
 
165
    Keyword arguments:
 
166
    date_val         --    date / datetime object
 
167
    gconf_client     --    gconf client instance
 
168
    display_seconds  --    if false, seconds will be stripped from result
 
169
    """    
 
170
    fmt = g15util.get_string_or_default(gconf_client, "/apps/gnome15/date_time_format", locale.nl_langinfo(locale.D_T_FMT))
 
171
    if not display_seconds:
 
172
        fmt = __strip_seconds(fmt)
 
173
    if isinstance(date_val, tuple):
 
174
        return datetime.datetime.strftime(fmt, date_val)
 
175
    else:
 
176
        return date_val.strftime(fmt)
68
177
 
69
178
def get_translation(domain, modfile=None):
70
179
    """
93
202
    language = gettext.translation (domain, translation_location, languages=languages, fallback=True)
94
203
    __translations[domain] = language
95
204
    return language
 
205
 
 
206
"""
 
207
Private
 
208
"""
 
209
 
 
210
def __strip_seconds(fmt):
 
211
    for f in REPLACE_FORMATS:
 
212
        fmt = fmt.replace(*f)
 
213
    return fmt
 
 
b'\\ No newline at end of file'