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 | """ |
---|
12 | Return a string date using the current locale. |
---|
13 | """ |
---|
14 | |
---|
15 | # TODO: don't call Localizer directly |
---|
16 | |
---|
17 | from DateTime.DateTime import DateTimeError |
---|
18 | |
---|
19 | if not dt: |
---|
20 | return '' |
---|
21 | if fmt in ('iso8601_medium',): |
---|
22 | dfmt = '%Y-%m-%dT%H:%MZ' |
---|
23 | elif fmt in ('medium','iso8601_medium_easy'): |
---|
24 | dfmt = '%Y-%m-%d %H:%M' |
---|
25 | elif fmt in ('iso8601_short', 'iso8601'): |
---|
26 | dfmt = '%Y-%m-%d' |
---|
27 | elif fmt == 'iso8601_long': |
---|
28 | dfmt = '%Y-%m-%dT%H:%M:%SZ' |
---|
29 | elif fmt == 'iso8601_long_easy': |
---|
30 | dfmt = '%Y-%m-%d %H:%M:%S' |
---|
31 | else: |
---|
32 | dfmt = fmt |
---|
33 | try: |
---|
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' |
---|
43 | except (DateTimeError,ValueError): |
---|
44 | ret = 'Invalid' |
---|
45 | |
---|
46 | return ret |
---|
47 | |
---|
48 | |
---|
49 | mcat = context.translation_service |
---|
50 | |
---|
51 | if 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 |
---|
55 | elif fmt not in ('iso8601', 'iso8601_short', 'iso8601_medium', 'iso8601_long', |
---|
56 | 'iso8601_medium_easy', 'iso8601_long_easy'): |
---|
57 | fmt = 'iso8601_medium_easy' |
---|
58 | |
---|
59 | try: |
---|
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' |
---|
81 | except DateTimeError: |
---|
82 | ret = 'Invalid' |
---|
83 | |
---|
84 | return ret |
---|