util/local_database/dateconverter.py
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 #!/usr/bin/env python
       
     2 #############################################################################
       
     3 ##
       
     4 ## Copyright (C) 2010 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 ## No Commercial Usage
       
    12 ## This file contains pre-release code and may not be distributed.
       
    13 ## You may use this file in accordance with the terms and conditions
       
    14 ## contained in the Technology Preview License Agreement accompanying
       
    15 ## this package.
       
    16 ##
       
    17 ## GNU Lesser General Public License Usage
       
    18 ## Alternatively, this file may be used under the terms of the GNU Lesser
       
    19 ## General Public License version 2.1 as published by the Free Software
       
    20 ## Foundation and appearing in the file LICENSE.LGPL included in the
       
    21 ## packaging of this file.  Please review the following information to
       
    22 ## ensure the GNU Lesser General Public License version 2.1 requirements
       
    23 ## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    24 ##
       
    25 ## In addition, as a special exception, Nokia gives you certain additional
       
    26 ## rights.  These rights are described in the Nokia Qt LGPL Exception
       
    27 ## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    28 ##
       
    29 ## If you have questions regarding the use of this file, please contact
       
    30 ## Nokia at qt-info@nokia.com.
       
    31 ##
       
    32 ##
       
    33 ##
       
    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)