comparison MoinMoin/support/parsedatetime/pdt_locales/icu.py @ 6098:83b1bc99457c

upgrade parsedatetime from 0.8.7 to 2.1
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 06 Sep 2016 00:09:31 +0200
parents
children
comparison
equal deleted inserted replaced
6097:815981fad7fd 6098:83b1bc99457c
1 # -*- encoding: utf-8 -*-
2
3 """
4 pdt_locales
5
6 All of the included locale classes shipped with pdt.
7 """
8 import datetime
9
10 try:
11 range = xrange
12 except NameError:
13 pass
14
15 try:
16 import PyICU as pyicu
17 except:
18 pyicu = None
19
20
21 def icu_object(mapping):
22 return type('_icu', (object,), mapping)
23
24
25 def merge_weekdays(base_wd, icu_wd):
26 result = []
27 for left, right in zip(base_wd, icu_wd):
28 if left == right:
29 result.append(left)
30 continue
31 left = set(left.split('|'))
32 right = set(right.split('|'))
33 result.append('|'.join(left | right))
34 return result
35
36
37 def get_icu(locale):
38 from . import base
39 result = dict([(key, getattr(base, key))
40 for key in dir(base) if not key.startswith('_')])
41 result['icu'] = None
42
43 if pyicu is None:
44 return icu_object(result)
45
46 if locale is None:
47 locale = 'en_US'
48 result['icu'] = icu = pyicu.Locale(locale)
49
50 if icu is None:
51 return icu_object(result)
52
53 # grab spelled out format of all numbers from 0 to 100
54 rbnf = pyicu.RuleBasedNumberFormat(pyicu.URBNFRuleSetTag.SPELLOUT, icu)
55 result['numbers'].update([(rbnf.format(i), i) for i in range(0, 100)])
56
57 symbols = result['symbols'] = pyicu.DateFormatSymbols(icu)
58
59 # grab ICU list of weekdays, skipping first entry which
60 # is always blank
61 wd = [w.lower() for w in symbols.getWeekdays()[1:]]
62 swd = [sw.lower() for sw in symbols.getShortWeekdays()[1:]]
63
64 # store them in our list with Monday first (ICU puts Sunday first)
65 result['Weekdays'] = merge_weekdays(result['Weekdays'],
66 wd[1:] + wd[0:1])
67 result['shortWeekdays'] = merge_weekdays(result['shortWeekdays'],
68 swd[1:] + swd[0:1])
69 result['Months'] = [m.lower() for m in symbols.getMonths()]
70 result['shortMonths'] = [sm.lower() for sm in symbols.getShortMonths()]
71 keys = ['full', 'long', 'medium', 'short']
72
73 createDateInstance = pyicu.DateFormat.createDateInstance
74 createTimeInstance = pyicu.DateFormat.createTimeInstance
75 icu_df = result['icu_df'] = {
76 'full': createDateInstance(pyicu.DateFormat.kFull, icu),
77 'long': createDateInstance(pyicu.DateFormat.kLong, icu),
78 'medium': createDateInstance(pyicu.DateFormat.kMedium, icu),
79 'short': createDateInstance(pyicu.DateFormat.kShort, icu),
80 }
81 icu_tf = result['icu_tf'] = {
82 'full': createTimeInstance(pyicu.DateFormat.kFull, icu),
83 'long': createTimeInstance(pyicu.DateFormat.kLong, icu),
84 'medium': createTimeInstance(pyicu.DateFormat.kMedium, icu),
85 'short': createTimeInstance(pyicu.DateFormat.kShort, icu),
86 }
87
88 result['dateFormats'] = {}
89 result['timeFormats'] = {}
90 for x in keys:
91 result['dateFormats'][x] = icu_df[x].toPattern()
92 result['timeFormats'][x] = icu_tf[x].toPattern()
93
94 am = pm = ts = ''
95
96 # ICU doesn't seem to provide directly the date or time separator
97 # so we have to figure it out
98 o = result['icu_tf']['short']
99 s = result['timeFormats']['short']
100
101 result['usesMeridian'] = 'a' in s
102 result['uses24'] = 'H' in s
103
104 # '11:45 AM' or '11:45'
105 s = o.format(datetime.datetime(2003, 10, 30, 11, 45))
106
107 # ': AM' or ':'
108 s = s.replace('11', '').replace('45', '')
109
110 if len(s) > 0:
111 ts = s[0]
112
113 if result['usesMeridian']:
114 # '23:45 AM' or '23:45'
115 am = s[1:].strip()
116 s = o.format(datetime.datetime(2003, 10, 30, 23, 45))
117
118 if result['uses24']:
119 s = s.replace('23', '')
120 else:
121 s = s.replace('11', '')
122
123 # 'PM' or ''
124 pm = s.replace('45', '').replace(ts, '').strip()
125
126 result['timeSep'] = [ts]
127 result['meridian'] = [am, pm] if am and pm else []
128
129 o = result['icu_df']['short']
130 s = o.format(datetime.datetime(2003, 10, 30, 11, 45))
131 s = s.replace('10', '').replace('30', '').replace(
132 '03', '').replace('2003', '')
133
134 if len(s) > 0:
135 ds = s[0]
136 else:
137 ds = '/'
138
139 result['dateSep'] = [ds]
140 s = result['dateFormats']['short']
141 l = s.lower().split(ds)
142 dp_order = []
143
144 for s in l:
145 if len(s) > 0:
146 dp_order.append(s[:1])
147
148 result['dp_order'] = dp_order
149 return icu_object(result)