Revert "Use standard spelling for two languages"
[profile/ivi/qtbase.git] / util / local_database / dateconverter.py
1 #!/usr/bin/env python
2 #############################################################################
3 ##
4 ## Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ## All rights reserved.
6 ## Contact: Nokia Corporation (qt-info@nokia.com)
7 ##
8 ## This file is part of the test suite of the Qt Toolkit.
9 ##
10 ## $QT_BEGIN_LICENSE:LGPL$
11 ## GNU Lesser General Public License Usage
12 ## This file may be used under the terms of the GNU Lesser General Public
13 ## License version 2.1 as published by the Free Software Foundation and
14 ## appearing in the file LICENSE.LGPL included in the packaging of this
15 ## file. Please review the following information to ensure the GNU Lesser
16 ## General Public License version 2.1 requirements will be met:
17 ## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ##
19 ## In addition, as a special exception, Nokia gives you certain additional
20 ## rights. These rights are described in the Nokia Qt LGPL Exception
21 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ##
23 ## GNU General Public License Usage
24 ## Alternatively, this file may be used under the terms of the GNU General
25 ## Public License version 3.0 as published by the Free Software Foundation
26 ## and appearing in the file LICENSE.GPL included in the packaging of this
27 ## file. Please review the following information to ensure the GNU General
28 ## Public License version 3.0 requirements will be met:
29 ## http://www.gnu.org/copyleft/gpl.html.
30 ##
31 ## Other Usage
32 ## Alternatively, this file may be used in accordance with the terms and
33 ## conditions contained in a signed written agreement between you and Nokia.
34 ##
35 ##
36 ##
37 ##
38 ##
39 ## $QT_END_LICENSE$
40 ##
41 #############################################################################
42
43 import re
44
45 def _convert_pattern(pattern):
46     # patterns from http://www.unicode.org/reports/tr35/#Date_Format_Patterns
47     qt_regexps = {
48         r"yyy{3,}" : "yyyy", # more that three digits hence convert to four-digit year
49         r"L" : "M",          # stand-alone month names. not supported.
50         r"g{1,}": "",        # modified julian day. not supported.
51         r"S{1,}" : "",       # fractional seconds. not supported.
52         r"A{1,}" : ""        # milliseconds in day. not supported.
53     }
54     qt_patterns = {
55         "G" : "", "GG" : "", "GGG" : "", "GGGG" : "", "GGGGG" : "", # Era. not supported.
56         "y" : "yyyy", # four-digit year without leading zeroes
57         "Q" : "", "QQ" : "", "QQQ" : "", "QQQQ" : "", # quarter. not supported.
58         "q" : "", "qq" : "", "qqq" : "", "qqqq" : "", # quarter. not supported.
59         "MMMMM" : "MMM", # narrow month name.
60         "LLLLL" : "MMM", # stand-alone narrow month name.
61         "l" : "", # special symbol for chinese leap month. not supported.
62         "w" : "", "W" : "", # week of year/month. not supported.
63         "D" : "", "DD" : "", "DDD" : "", # day of year. not supported.
64         "F" : "", # day of week in month. not supported.
65         "E" : "ddd", "EE" : "ddd", "EEE" : "ddd", "EEEEE" : "ddd", "EEEE" : "dddd", # day of week
66         "e" : "ddd", "ee" : "ddd", "eee" : "ddd", "eeeee" : "ddd", "eeee" : "dddd", # local day of week
67         "c" : "ddd", "cc" : "ddd", "ccc" : "ddd", "ccccc" : "ddd", "cccc" : "dddd", # stand-alone local day of week
68         "a" : "AP", # AM/PM
69         "K" : "h", # Hour 0-11
70         "k" : "H", # Hour 1-24
71         "j" : "", # special reserved symbol.
72         "z" : "t", "zz" : "t", "zzz" : "t", "zzzz" : "t", # timezone
73         "Z" : "t", "ZZ" : "t", "ZZZ" : "t", "ZZZZ" : "t", # timezone
74         "v" : "t", "vv" : "t", "vvv" : "t", "vvvv" : "t", # timezone
75         "V" : "t", "VV" : "t", "VVV" : "t", "VVVV" : "t"  # timezone
76     }
77     if qt_patterns.has_key(pattern):
78         return qt_patterns[pattern]
79     for r,v in qt_regexps.items():
80         pattern = re.sub(r, v, pattern)
81     return pattern
82
83 def convert_date(input):
84     result = ""
85     patterns = "GyYuQqMLlwWdDFgEecahHKkjmsSAzZvV"
86     last = ""
87     inquote = 0
88     chars_to_strip = " -"
89     for c in input:
90         if c == "'":
91             inquote = inquote + 1
92         if inquote % 2 == 0:
93             if c in patterns:
94                 if not last:
95                     last = c
96                 else:
97                     if c in last:
98                         last += c
99                     else:
100                         # pattern changed
101                         converted = _convert_pattern(last)
102                         result += converted
103                         if not converted:
104                             result = result.rstrip(chars_to_strip)
105                         last = c
106                 continue
107         if last:
108             # pattern ended
109             converted = _convert_pattern(last)
110             result += converted
111             if not converted:
112                 result = result.rstrip(chars_to_strip)
113             last = ""
114         result += c
115     if last:
116         converted = _convert_pattern(last)
117         result += converted
118         if not converted:
119             result = result.rstrip(chars_to_strip)
120     return result.lstrip(chars_to_strip)