source: WAeUP_SRP/branches/joachim-azax-branch/skins/waeup_custom/getDateStr.py @ 15362

Last change on this file since 15362 was 1868, checked in by joachim, 17 years ago

fix for #248

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1## Script (Python) "getDateStr"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=dt=None, fmt='medium'
8##title=
9##
10# $Id: getDateStr.py 1868 2007-06-08 10:30:01Z joachim $
11"""
12Return a string date using the current locale.
13"""
14
15# TODO: don't call Localizer directly
16
17from DateTime.DateTime import DateTimeError
18
19if not dt:
20    return ''
21if fmt in ('iso8601_medium',):
22    dfmt = '%Y-%m-%dT%H:%MZ'
23elif fmt in ('medium','iso8601_medium_easy'):
24    dfmt = '%Y-%m-%d %H:%M'
25elif fmt in ('iso8601_short', 'iso8601'):
26    dfmt = '%Y-%m-%d'
27elif fmt == 'iso8601_long':
28    dfmt = '%Y-%m-%dT%H:%M:%SZ'
29elif fmt == 'iso8601_long_easy':
30    dfmt = '%Y-%m-%d %H:%M:%S'
31else:
32    dfmt = fmt
33try:
34    ret = dt.strftime(dfmt)
35    # XXX remove this as soon as strftime is fixed
36    # space hack to fix %p strftime bug when LC_ALL=fr_FR
37    if (dfmt.endswith('%p') and not ret.endswith('M')):
38        h = int(dt.strftime('%H'))
39        if h > 12:
40            ret += ' PM'
41        else:
42            ret += ' AM'
43except (DateTimeError,ValueError):
44    ret = 'Invalid'
45   
46return ret
47
48
49mcat = context.translation_service
50
51if fmt in ('short', 'medium', 'long'):
52    # This string will be used to retrieve format in the right .po file
53    fmt = 'date_' + fmt
54# For iso8601 dates read http://www.w3.org/TR/NOTE-datetime
55elif fmt not in ('iso8601', 'iso8601_short', 'iso8601_medium', 'iso8601_long',
56                 'iso8601_medium_easy', 'iso8601_long_easy'):
57    fmt = 'iso8601_medium_easy'
58
59try:
60    if fmt == 'iso8601_short' or fmt == 'iso8601':
61        dfmt = '%Y-%m-%d'
62    elif fmt == 'iso8601_medium':
63        dfmt = '%Y-%m-%dT%H:%MZ'
64    elif fmt == 'iso8601_medium_easy':
65        dfmt = '%Y-%m-%d %H:%M'
66    elif fmt == 'iso8601_long':
67        dfmt = '%Y-%m-%dT%H:%M:%SZ'
68    elif fmt == 'iso8601_long_easy':
69        dfmt = '%Y-%m-%d %H:%M:%S'
70    else:
71        dfmt = mcat(fmt)
72    ret = dt.strftime(dfmt)
73    # XXX remove this as soon as strftime is fixed
74    # space hack to fix %p strftime bug when LC_ALL=fr_FR
75    if (dfmt.endswith('%p') and not ret.endswith('M')):
76        h = int(dt.strftime('%H'))
77        if h > 12:
78            ret += ' PM'
79        else:
80            ret += ' AM'
81except DateTimeError:
82    ret = 'Invalid'
83
84return ret
Note: See TracBrowser for help on using the repository browser.