annotate MoinMoin/support/parsedatetime/__init__.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 0a6fe22644e3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6098
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1 # -*- coding: utf-8 -*-
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
2 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
3 # vim: sw=2 ts=2 sts=2
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
4 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
5 # Copyright 2004-2016 Mike Taylor
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
6 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
7 # Licensed under the Apache License, Version 2.0 (the "License");
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
8 # you may not use this file except in compliance with the License.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
9 # You may obtain a copy of the License at
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
10 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
11 # http://www.apache.org/licenses/LICENSE-2.0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
12 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
13 # Unless required by applicable law or agreed to in writing, software
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
14 # distributed under the License is distributed on an "AS IS" BASIS,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
16 # See the License for the specific language governing permissions and
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
17 # limitations under the License.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
18
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
19 """parsedatetime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
20
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
21 Parse human-readable date/time text.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
22
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
23 Requires Python 2.6 or later
5185
0a6fe22644e3 updated parsedatetime to 0.8.7
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1463
diff changeset
24 """
0a6fe22644e3 updated parsedatetime to 0.8.7
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1463
diff changeset
25
6098
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
26 from __future__ import with_statement, absolute_import, unicode_literals
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
27
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
28 import re
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
29 import time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
30 import logging
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
31 import warnings
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
32 import datetime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
33 import calendar
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
34 import contextlib
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
35 import email.utils
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
36
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
37 from .pdt_locales import (locales as _locales,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
38 get_icu, load_locale)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
39 from .context import pdtContext, pdtContextStack
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
40 from .warns import pdt20DeprecationWarning
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
41
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
42
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
43 __author__ = 'Mike Taylor'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
44 __email__ = 'bear@bear.im'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
45 __copyright__ = 'Copyright (c) 2016 Mike Taylor'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
46 __license__ = 'Apache License 2.0'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
47 __version__ = '2.1'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
48 __url__ = 'https://github.com/bear/parsedatetime'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
49 __download_url__ = 'https://pypi.python.org/pypi/parsedatetime'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
50 __description__ = 'Parse human-readable date/time text.'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
51
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
52 # as a library, do *not* setup logging
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
53 # see docs.python.org/2/howto/logging.html#configuring-logging-for-a-library
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
54 # Set default logging handler to avoid "No handler found" warnings.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
55
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
56 try: # Python 2.7+
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
57 from logging import NullHandler
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
58 except ImportError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
59 class NullHandler(logging.Handler):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
60
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
61 def emit(self, record):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
62 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
63
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
64 log = logging.getLogger(__name__)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
65 log.addHandler(NullHandler())
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
66
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
67 debug = False
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
68
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
69 pdtLocales = dict([(x, load_locale(x)) for x in _locales])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
70
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
71
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
72 # Copied from feedparser.py
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
73 # Universal Feedparser
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
74 # Copyright (c) 2002-2006, Mark Pilgrim, All rights reserved.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
75 # Originally a def inside of _parse_date_w3dtf()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
76 def _extract_date(m):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
77 year = int(m.group('year'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
78 if year < 100:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
79 year = 100 * int(time.gmtime()[0] / 100) + int(year)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
80 if year < 1000:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
81 return 0, 0, 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
82 julian = m.group('julian')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
83 if julian:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
84 julian = int(julian)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
85 month = julian / 30 + 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
86 day = julian % 30 + 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
87 jday = None
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
88 while jday != julian:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
89 t = time.mktime((year, month, day, 0, 0, 0, 0, 0, 0))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
90 jday = time.gmtime(t)[-2]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
91 diff = abs(jday - julian)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
92 if jday > julian:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
93 if diff < day:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
94 day = day - diff
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
95 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
96 month = month - 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
97 day = 31
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
98 elif jday < julian:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
99 if day + diff < 28:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
100 day = day + diff
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
101 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
102 month = month + 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
103 return year, month, day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
104 month = m.group('month')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
105 day = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
106 if month is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
107 month = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
108 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
109 month = int(month)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
110 day = m.group('day')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
111 if day:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
112 day = int(day)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
113 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
114 day = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
115 return year, month, day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
116
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
117
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
118 # Copied from feedparser.py
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
119 # Universal Feedparser
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
120 # Copyright (c) 2002-2006, Mark Pilgrim, All rights reserved.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
121 # Originally a def inside of _parse_date_w3dtf()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
122 def _extract_time(m):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
123 if not m:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
124 return 0, 0, 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
125 hours = m.group('hours')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
126 if not hours:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
127 return 0, 0, 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
128 hours = int(hours)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
129 minutes = int(m.group('minutes'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
130 seconds = m.group('seconds')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
131 if seconds:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
132 seconds = seconds.replace(',', '.').split('.', 1)[0]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
133 seconds = int(seconds)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
134 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
135 seconds = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
136 return hours, minutes, seconds
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
137
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
138
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
139 def _pop_time_accuracy(m, ctx):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
140 if not m:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
141 return
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
142 if m.group('hours'):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
143 ctx.updateAccuracy(ctx.ACU_HOUR)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
144 if m.group('minutes'):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
145 ctx.updateAccuracy(ctx.ACU_MIN)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
146 if m.group('seconds'):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
147 ctx.updateAccuracy(ctx.ACU_SEC)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
148
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
149
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
150 # Copied from feedparser.py
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
151 # Universal Feedparser
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
152 # Copyright (c) 2002-2006, Mark Pilgrim, All rights reserved.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
153 # Modified to return a tuple instead of mktime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
154 #
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
155 # Original comment:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
156 # W3DTF-style date parsing adapted from PyXML xml.utils.iso8601, written by
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
157 # Drake and licensed under the Python license. Removed all range checking
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
158 # for month, day, hour, minute, and second, since mktime will normalize
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
159 # these later
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
160 def __closure_parse_date_w3dtf():
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
161 # the __extract_date and __extract_time methods were
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
162 # copied-out so they could be used by my code --bear
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
163 def __extract_tzd(m):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
164 '''Return the Time Zone Designator as an offset in seconds from UTC.'''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
165 if not m:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
166 return 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
167 tzd = m.group('tzd')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
168 if not tzd:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
169 return 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
170 if tzd == 'Z':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
171 return 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
172 hours = int(m.group('tzdhours'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
173 minutes = m.group('tzdminutes')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
174 if minutes:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
175 minutes = int(minutes)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
176 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
177 minutes = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
178 offset = (hours * 60 + minutes) * 60
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
179 if tzd[0] == '+':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
180 return -offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
181 return offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
182
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
183 def _parse_date_w3dtf(dateString):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
184 m = __datetime_rx.match(dateString)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
185 if m is None or m.group() != dateString:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
186 return
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
187 return _extract_date(m) + _extract_time(m) + (0, 0, 0)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
188
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
189 __date_re = (r'(?P<year>\d\d\d\d)'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
190 r'(?:(?P<dsep>-|)'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
191 r'(?:(?P<julian>\d\d\d)'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
192 r'|(?P<month>\d\d)(?:(?P=dsep)(?P<day>\d\d))?))?')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
193 __tzd_re = r'(?P<tzd>[-+](?P<tzdhours>\d\d)(?::?(?P<tzdminutes>\d\d))|Z)'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
194 # __tzd_rx = re.compile(__tzd_re)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
195 __time_re = (r'(?P<hours>\d\d)(?P<tsep>:|)(?P<minutes>\d\d)'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
196 r'(?:(?P=tsep)(?P<seconds>\d\d(?:[.,]\d+)?))?' +
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
197 __tzd_re)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
198 __datetime_re = '%s(?:T%s)?' % (__date_re, __time_re)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
199 __datetime_rx = re.compile(__datetime_re)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
200
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
201 return _parse_date_w3dtf
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
202
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
203
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
204 _parse_date_w3dtf = __closure_parse_date_w3dtf()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
205 del __closure_parse_date_w3dtf
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
206
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
207 _monthnames = set([
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
208 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
209 'aug', 'sep', 'oct', 'nov', 'dec',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
210 'january', 'february', 'march', 'april', 'may', 'june', 'july',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
211 'august', 'september', 'october', 'november', 'december'])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
212 _daynames = set(['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
213
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
214
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
215 # Copied from feedparser.py
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
216 # Universal Feedparser
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
217 # Copyright (c) 2002-2006, Mark Pilgrim, All rights reserved.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
218 # Modified to return a tuple instead of mktime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
219 def _parse_date_rfc822(dateString):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
220 '''Parse an RFC822, RFC1123, RFC2822, or asctime-style date'''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
221 data = dateString.split()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
222 if data[0][-1] in (',', '.') or data[0].lower() in _daynames:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
223 del data[0]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
224 if len(data) == 4:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
225 s = data[3]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
226 s = s.split('+', 1)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
227 if len(s) == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
228 data[3:] = s
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
229 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
230 data.append('')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
231 dateString = " ".join(data)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
232 if len(data) < 5:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
233 dateString += ' 00:00:00 GMT'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
234 return email.utils.parsedate_tz(dateString)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
235
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
236
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
237 # rfc822.py defines several time zones, but we define some extra ones.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
238 # 'ET' is equivalent to 'EST', etc.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
239 # _additional_timezones = {'AT': -400, 'ET': -500,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
240 # 'CT': -600, 'MT': -700,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
241 # 'PT': -800}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
242 # email.utils._timezones.update(_additional_timezones)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
243
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
244 VERSION_FLAG_STYLE = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
245 VERSION_CONTEXT_STYLE = 2
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
246
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
247
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
248 class Calendar(object):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
249
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
250 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
251 A collection of routines to input, parse and manipulate date and times.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
252 The text can either be 'normal' date values or it can be human readable.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
253 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
254
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
255 def __init__(self, constants=None, version=VERSION_FLAG_STYLE):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
256 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
257 Default constructor for the L{Calendar} class.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
258
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
259 @type constants: object
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
260 @param constants: Instance of the class L{Constants}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
261 @type version: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
262 @param version: Default style version of current Calendar instance.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
263 Valid value can be 1 (L{VERSION_FLAG_STYLE}) or
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
264 2 (L{VERSION_CONTEXT_STYLE}). See L{parse()}.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
265
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
266 @rtype: object
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
267 @return: L{Calendar} instance
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
268 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
269 # if a constants reference is not included, use default
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
270 if constants is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
271 self.ptc = Constants()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
272 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
273 self.ptc = constants
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
274
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
275 self.version = version
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
276 if version == VERSION_FLAG_STYLE:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
277 warnings.warn(
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
278 'Flag style will be deprecated in parsedatetime 2.0. '
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
279 'Instead use the context style by instantiating `Calendar()` '
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
280 'with argument `version=parsedatetime.VERSION_CONTEXT_STYLE`.',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
281 pdt20DeprecationWarning)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
282 self._ctxStack = pdtContextStack()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
283
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
284 @contextlib.contextmanager
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
285 def context(self):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
286 ctx = pdtContext()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
287 self._ctxStack.push(ctx)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
288 yield ctx
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
289 ctx = self._ctxStack.pop()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
290 if not self._ctxStack.isEmpty():
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
291 self.currentContext.update(ctx)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
292
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
293 @property
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
294 def currentContext(self):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
295 return self._ctxStack.last()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
296
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
297 def _convertUnitAsWords(self, unitText):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
298 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
299 Converts text units into their number value.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
300
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
301 @type unitText: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
302 @param unitText: number text to convert
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
303
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
304 @rtype: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
305 @return: numerical value of unitText
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
306 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
307 word_list, a, b = re.split(r"[,\s-]+", unitText), 0, 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
308 for word in word_list:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
309 x = self.ptc.small.get(word)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
310 if x is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
311 a += x
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
312 elif word == "hundred":
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
313 a *= 100
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
314 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
315 x = self.ptc.magnitude.get(word)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
316 if x is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
317 b += a * x
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
318 a = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
319 elif word in self.ptc.ignore:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
320 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
321 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
322 raise Exception("Unknown number: " + word)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
323 return a + b
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
324
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
325 def _buildTime(self, source, quantity, modifier, units):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
326 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
327 Take C{quantity}, C{modifier} and C{unit} strings and convert them
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
328 into values. After converting, calcuate the time and return the
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
329 adjusted sourceTime.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
330
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
331 @type source: time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
332 @param source: time to use as the base (or source)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
333 @type quantity: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
334 @param quantity: quantity string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
335 @type modifier: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
336 @param modifier: how quantity and units modify the source time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
337 @type units: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
338 @param units: unit of the quantity (i.e. hours, days, months, etc)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
339
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
340 @rtype: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
341 @return: C{struct_time} of the calculated time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
342 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
343 ctx = self.currentContext
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
344 debug and log.debug('_buildTime: [%s][%s][%s]',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
345 quantity, modifier, units)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
346
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
347 if source is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
348 source = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
349
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
350 if quantity is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
351 quantity = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
352 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
353 quantity = quantity.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
354
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
355 qty = self._quantityToReal(quantity)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
356
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
357 if modifier in self.ptc.Modifiers:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
358 qty = qty * self.ptc.Modifiers[modifier]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
359
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
360 if units is None or units == '':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
361 units = 'dy'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
362
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
363 # plurals are handled by regex's (could be a bug tho)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
364
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
365 (yr, mth, dy, hr, mn, sec, _, _, _) = source
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
366
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
367 start = datetime.datetime(yr, mth, dy, hr, mn, sec)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
368 target = start
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
369 # realunit = next((key for key, values in self.ptc.units.items()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
370 # if any(imap(units.__contains__, values))), None)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
371 realunit = units
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
372 for key, values in self.ptc.units.items():
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
373 if units in values:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
374 realunit = key
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
375 break
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
376
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
377 debug and log.debug('units %s --> realunit %s (qty=%s)',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
378 units, realunit, qty)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
379
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
380 try:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
381 if realunit in ('years', 'months'):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
382 target = self.inc(start, **{realunit[:-1]: qty})
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
383 elif realunit in ('days', 'hours', 'minutes', 'seconds', 'weeks'):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
384 delta = datetime.timedelta(**{realunit: qty})
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
385 target = start + delta
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
386 except OverflowError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
387 # OverflowError is raise when target.year larger than 9999
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
388 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
389 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
390 ctx.updateAccuracy(realunit)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
391
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
392 return target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
393
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
394 def parseDate(self, dateString, sourceTime=None):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
395 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
396 Parse short-form date strings::
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
397
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
398 '05/28/2006' or '04.21'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
399
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
400 @type dateString: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
401 @param dateString: text to convert to a C{datetime}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
402 @type sourceTime: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
403 @param sourceTime: C{struct_time} value to use as the base
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
404
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
405 @rtype: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
406 @return: calculated C{struct_time} value of dateString
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
407 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
408 if sourceTime is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
409 yr, mth, dy, hr, mn, sec, wd, yd, isdst = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
410 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
411 yr, mth, dy, hr, mn, sec, wd, yd, isdst = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
412
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
413 # values pulled from regex's will be stored here and later
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
414 # assigned to mth, dy, yr based on information from the locale
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
415 # -1 is used as the marker value because we want zero values
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
416 # to be passed thru so they can be flagged as errors later
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
417 v1 = -1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
418 v2 = -1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
419 v3 = -1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
420 accuracy = []
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
421
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
422 s = dateString
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
423 m = self.ptc.CRE_DATE2.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
424 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
425 index = m.start()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
426 v1 = int(s[:index])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
427 s = s[index + 1:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
428
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
429 m = self.ptc.CRE_DATE2.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
430 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
431 index = m.start()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
432 v2 = int(s[:index])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
433 v3 = int(s[index + 1:])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
434 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
435 v2 = int(s.strip())
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
436
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
437 v = [v1, v2, v3]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
438 d = {'m': mth, 'd': dy, 'y': yr}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
439
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
440 # yyyy/mm/dd format
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
441 dp_order = self.ptc.dp_order if v1 <= 31 else ['y', 'm', 'd']
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
442
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
443 for i in range(0, 3):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
444 n = v[i]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
445 c = dp_order[i]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
446 if n >= 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
447 d[c] = n
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
448 accuracy.append({'m': pdtContext.ACU_MONTH,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
449 'd': pdtContext.ACU_DAY,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
450 'y': pdtContext.ACU_YEAR}[c])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
451
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
452 # if the year is not specified and the date has already
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
453 # passed, increment the year
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
454 if v3 == -1 and ((mth > d['m']) or (mth == d['m'] and dy > d['d'])):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
455 yr = d['y'] + self.ptc.YearParseStyle
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
456 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
457 yr = d['y']
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
458
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
459 mth = d['m']
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
460 dy = d['d']
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
461
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
462 # birthday epoch constraint
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
463 if yr < self.ptc.BirthdayEpoch:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
464 yr += 2000
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
465 elif yr < 100:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
466 yr += 1900
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
467
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
468 daysInCurrentMonth = self.ptc.daysInMonth(mth, yr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
469 debug and log.debug('parseDate: %s %s %s %s',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
470 yr, mth, dy, daysInCurrentMonth)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
471
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
472 with self.context() as ctx:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
473 if mth > 0 and mth <= 12 and dy > 0 and \
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
474 dy <= daysInCurrentMonth:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
475 sourceTime = (yr, mth, dy, hr, mn, sec, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
476 ctx.updateAccuracy(*accuracy)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
477 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
478 # return current time if date string is invalid
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
479 sourceTime = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
480
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
481 return sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
482
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
483 def parseDateText(self, dateString, sourceTime=None):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
484 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
485 Parse long-form date strings::
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
486
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
487 'May 31st, 2006'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
488 'Jan 1st'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
489 'July 2006'
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
490
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
491 @type dateString: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
492 @param dateString: text to convert to a datetime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
493 @type sourceTime: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
494 @param sourceTime: C{struct_time} value to use as the base
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
495
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
496 @rtype: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
497 @return: calculated C{struct_time} value of dateString
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
498 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
499 if sourceTime is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
500 yr, mth, dy, hr, mn, sec, wd, yd, isdst = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
501 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
502 yr, mth, dy, hr, mn, sec, wd, yd, isdst = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
503
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
504 currentMth = mth
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
505 currentDy = dy
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
506 accuracy = []
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
507
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
508 debug and log.debug('parseDateText currentMth %s currentDy %s',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
509 mth, dy)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
510
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
511 s = dateString.lower()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
512 m = self.ptc.CRE_DATE3.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
513 mth = m.group('mthname')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
514 mth = self.ptc.MonthOffsets[mth]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
515 accuracy.append('month')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
516
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
517 if m.group('day') is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
518 dy = int(m.group('day'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
519 accuracy.append('day')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
520 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
521 dy = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
522
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
523 if m.group('year') is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
524 yr = int(m.group('year'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
525 accuracy.append('year')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
526
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
527 # birthday epoch constraint
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
528 if yr < self.ptc.BirthdayEpoch:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
529 yr += 2000
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
530 elif yr < 100:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
531 yr += 1900
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
532
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
533 elif (mth < currentMth) or (mth == currentMth and dy < currentDy):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
534 # if that day and month have already passed in this year,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
535 # then increment the year by 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
536 yr += self.ptc.YearParseStyle
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
537
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
538 with self.context() as ctx:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
539 if dy > 0 and dy <= self.ptc.daysInMonth(mth, yr):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
540 sourceTime = (yr, mth, dy, hr, mn, sec, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
541 ctx.updateAccuracy(*accuracy)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
542 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
543 # Return current time if date string is invalid
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
544 sourceTime = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
545
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
546 debug and log.debug('parseDateText returned '
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
547 'mth %d dy %d yr %d sourceTime %s',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
548 mth, dy, yr, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
549
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
550 return sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
551
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
552 def evalRanges(self, datetimeString, sourceTime=None):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
553 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
554 Evaluate the C{datetimeString} text and determine if
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
555 it represents a date or time range.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
556
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
557 @type datetimeString: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
558 @param datetimeString: datetime text to evaluate
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
559 @type sourceTime: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
560 @param sourceTime: C{struct_time} value to use as the base
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
561
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
562 @rtype: tuple
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
563 @return: tuple of: start datetime, end datetime and the invalid flag
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
564 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
565 rangeFlag = retFlag = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
566 startStr = endStr = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
567
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
568 s = datetimeString.strip().lower()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
569
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
570 if self.ptc.rangeSep in s:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
571 s = s.replace(self.ptc.rangeSep, ' %s ' % self.ptc.rangeSep)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
572 s = s.replace(' ', ' ')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
573
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
574 for cre, rflag in [(self.ptc.CRE_TIMERNG1, 1),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
575 (self.ptc.CRE_TIMERNG2, 2),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
576 (self.ptc.CRE_TIMERNG4, 7),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
577 (self.ptc.CRE_TIMERNG3, 3),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
578 (self.ptc.CRE_DATERNG1, 4),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
579 (self.ptc.CRE_DATERNG2, 5),
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
580 (self.ptc.CRE_DATERNG3, 6)]:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
581 m = cre.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
582 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
583 rangeFlag = rflag
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
584 break
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
585
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
586 debug and log.debug('evalRanges: rangeFlag = %s [%s]', rangeFlag, s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
587
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
588 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
589 if (m.group() != s):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
590 # capture remaining string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
591 parseStr = m.group()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
592 chunk1 = s[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
593 chunk2 = s[m.end():]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
594 s = '%s %s' % (chunk1, chunk2)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
595
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
596 sourceTime, ctx = self.parse(s, sourceTime,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
597 VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
598
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
599 if not ctx.hasDateOrTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
600 sourceTime = None
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
601 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
602 parseStr = s
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
603
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
604 if rangeFlag in (1, 2):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
605 m = re.search(self.ptc.rangeSep, parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
606 startStr = parseStr[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
607 endStr = parseStr[m.start() + 1:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
608 retFlag = 2
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
609
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
610 elif rangeFlag in (3, 7):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
611 m = re.search(self.ptc.rangeSep, parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
612 # capturing the meridian from the end time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
613 if self.ptc.usesMeridian:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
614 ampm = re.search(self.ptc.am[0], parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
615
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
616 # appending the meridian to the start time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
617 if ampm is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
618 startStr = parseStr[:m.start()] + self.ptc.meridian[0]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
619 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
620 startStr = parseStr[:m.start()] + self.ptc.meridian[1]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
621 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
622 startStr = parseStr[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
623
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
624 endStr = parseStr[m.start() + 1:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
625 retFlag = 2
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
626
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
627 elif rangeFlag == 4:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
628 m = re.search(self.ptc.rangeSep, parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
629 startStr = parseStr[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
630 endStr = parseStr[m.start() + 1:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
631 retFlag = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
632
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
633 elif rangeFlag == 5:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
634 m = re.search(self.ptc.rangeSep, parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
635 endStr = parseStr[m.start() + 1:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
636
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
637 # capturing the year from the end date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
638 date = self.ptc.CRE_DATE3.search(endStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
639 endYear = date.group('year')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
640
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
641 # appending the year to the start date if the start date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
642 # does not have year information and the end date does.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
643 # eg : "Aug 21 - Sep 4, 2007"
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
644 if endYear is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
645 startStr = (parseStr[:m.start()]).strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
646 date = self.ptc.CRE_DATE3.search(startStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
647 startYear = date.group('year')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
648
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
649 if startYear is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
650 startStr = startStr + ', ' + endYear
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
651 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
652 startStr = parseStr[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
653
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
654 retFlag = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
655
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
656 elif rangeFlag == 6:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
657 m = re.search(self.ptc.rangeSep, parseStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
658
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
659 startStr = parseStr[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
660
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
661 # capturing the month from the start date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
662 mth = self.ptc.CRE_DATE3.search(startStr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
663 mth = mth.group('mthname')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
664
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
665 # appending the month name to the end date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
666 endStr = mth + parseStr[(m.start() + 1):]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
667
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
668 retFlag = 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
669
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
670 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
671 # if range is not found
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
672 startDT = endDT = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
673
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
674 if retFlag:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
675 startDT, sctx = self.parse(startStr, sourceTime,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
676 VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
677 endDT, ectx = self.parse(endStr, sourceTime,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
678 VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
679
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
680 if not sctx.hasDateOrTime or not ectx.hasDateOrTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
681 retFlag = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
682
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
683 return startDT, endDT, retFlag
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
684
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
685 def _CalculateDOWDelta(self, wd, wkdy, offset, style, currentDayStyle):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
686 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
687 Based on the C{style} and C{currentDayStyle} determine what
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
688 day-of-week value is to be returned.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
689
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
690 @type wd: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
691 @param wd: day-of-week value for the current day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
692 @type wkdy: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
693 @param wkdy: day-of-week value for the parsed day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
694 @type offset: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
695 @param offset: offset direction for any modifiers (-1, 0, 1)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
696 @type style: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
697 @param style: normally the value
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
698 set in C{Constants.DOWParseStyle}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
699 @type currentDayStyle: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
700 @param currentDayStyle: normally the value
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
701 set in C{Constants.CurrentDOWParseStyle}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
702
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
703 @rtype: integer
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
704 @return: calculated day-of-week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
705 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
706 diffBase = wkdy - wd
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
707 origOffset = offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
708
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
709 if offset == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
710 # no modifier is present.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
711 # i.e. string to be parsed is just DOW
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
712 if wkdy * style > wd * style or \
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
713 currentDayStyle and wkdy == wd:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
714 # wkdy located in current week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
715 offset = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
716 elif style in (-1, 1):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
717 # wkdy located in last (-1) or next (1) week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
718 offset = style
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
719 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
720 # invalid style, or should raise error?
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
721 offset = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
722
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
723 # offset = -1 means last week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
724 # offset = 0 means current week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
725 # offset = 1 means next week
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
726 diff = diffBase + 7 * offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
727 if style == 1 and diff < -7:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
728 diff += 7
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
729 elif style == -1 and diff > 7:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
730 diff -= 7
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
731
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
732 debug and log.debug("wd %s, wkdy %s, offset %d, "
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
733 "style %d, currentDayStyle %d",
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
734 wd, wkdy, origOffset, style, currentDayStyle)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
735
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
736 return diff
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
737
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
738 def _quantityToReal(self, quantity):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
739 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
740 Convert a quantity, either spelled-out or numeric, to a float
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
741
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
742 @type quantity: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
743 @param quantity: quantity to parse to float
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
744 @rtype: int
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
745 @return: the quantity as an float, defaulting to 0.0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
746 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
747 if not quantity:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
748 return 1.0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
749
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
750 try:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
751 return float(quantity.replace(',', '.'))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
752 except ValueError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
753 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
754
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
755 try:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
756 return float(self.ptc.numbers[quantity])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
757 except KeyError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
758 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
759
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
760 return 0.0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
761
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
762 def _evalModifier(self, modifier, chunk1, chunk2, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
763 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
764 Evaluate the C{modifier} string and following text (passed in
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
765 as C{chunk1} and C{chunk2}) and if they match any known modifiers
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
766 calculate the delta and apply it to C{sourceTime}.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
767
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
768 @type modifier: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
769 @param modifier: modifier text to apply to sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
770 @type chunk1: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
771 @param chunk1: text chunk that preceded modifier (if any)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
772 @type chunk2: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
773 @param chunk2: text chunk that followed modifier (if any)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
774 @type sourceTime: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
775 @param sourceTime: C{struct_time} value to use as the base
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
776
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
777 @rtype: tuple
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
778 @return: tuple of: remaining text and the modified sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
779 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
780 ctx = self.currentContext
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
781 offset = self.ptc.Modifiers[modifier]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
782
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
783 if sourceTime is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
784 (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
785 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
786 (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
787
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
788 if self.ptc.StartTimeFromSourceTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
789 startHour = hr
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
790 startMinute = mn
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
791 startSecond = sec
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
792 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
793 startHour = 9
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
794 startMinute = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
795 startSecond = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
796
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
797 # capture the units after the modifier and the remaining
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
798 # string after the unit
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
799 m = self.ptc.CRE_REMAINING.search(chunk2)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
800 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
801 index = m.start() + 1
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
802 unit = chunk2[:m.start()]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
803 chunk2 = chunk2[index:]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
804 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
805 unit = chunk2
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
806 chunk2 = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
807
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
808 debug and log.debug("modifier [%s] chunk1 [%s] "
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
809 "chunk2 [%s] unit [%s]",
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
810 modifier, chunk1, chunk2, unit)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
811
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
812 if unit in self.ptc.units['months']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
813 currentDaysInMonth = self.ptc.daysInMonth(mth, yr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
814 if offset == 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
815 dy = currentDaysInMonth
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
816 sourceTime = (yr, mth, dy, startHour, startMinute,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
817 startSecond, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
818 elif offset == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
819 # if day is the last day of the month, calculate the last day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
820 # of the next month
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
821 if dy == currentDaysInMonth:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
822 dy = self.ptc.daysInMonth(mth + 1, yr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
823
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
824 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
825 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
826 target = self.inc(start, month=1)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
827 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
828 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
829 start = datetime.datetime(yr, mth, 1, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
830 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
831 target = self.inc(start, month=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
832 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
833 ctx.updateAccuracy(ctx.ACU_MONTH)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
834
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
835 elif unit in self.ptc.units['weeks']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
836 if offset == 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
837 start = datetime.datetime(yr, mth, dy, 17, 0, 0)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
838 target = start + datetime.timedelta(days=(4 - wd))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
839 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
840 elif offset == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
841 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
842 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
843 target = start + datetime.timedelta(days=7)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
844 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
845 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
846 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
847 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
848 target = start + offset * datetime.timedelta(weeks=1)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
849 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
850 ctx.updateAccuracy(ctx.ACU_WEEK)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
851
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
852 elif unit in self.ptc.units['days']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
853 if offset == 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
854 sourceTime = (yr, mth, dy, 17, 0, 0, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
855 ctx.updateAccuracy(ctx.ACU_HALFDAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
856 elif offset == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
857 start = datetime.datetime(yr, mth, dy, hr, mn, sec)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
858 target = start + datetime.timedelta(days=1)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
859 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
860 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
861 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
862 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
863 target = start + datetime.timedelta(days=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
864 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
865 ctx.updateAccuracy(ctx.ACU_DAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
866
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
867 elif unit in self.ptc.units['hours']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
868 if offset == 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
869 sourceTime = (yr, mth, dy, hr, 0, 0, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
870 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
871 start = datetime.datetime(yr, mth, dy, hr, 0, 0)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
872 target = start + datetime.timedelta(hours=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
873 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
874 ctx.updateAccuracy(ctx.ACU_HOUR)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
875
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
876 elif unit in self.ptc.units['years']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
877 if offset == 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
878 sourceTime = (yr, 12, 31, hr, mn, sec, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
879 elif offset == 2:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
880 sourceTime = (yr + 1, mth, dy, hr, mn, sec, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
881 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
882 sourceTime = (yr + offset, 1, 1, startHour, startMinute,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
883 startSecond, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
884 ctx.updateAccuracy(ctx.ACU_YEAR)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
885
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
886 elif modifier == 'eom':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
887 dy = self.ptc.daysInMonth(mth, yr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
888 sourceTime = (yr, mth, dy, startHour, startMinute,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
889 startSecond, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
890 ctx.updateAccuracy(ctx.ACU_DAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
891
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
892 elif modifier == 'eoy':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
893 mth = 12
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
894 dy = self.ptc.daysInMonth(mth, yr)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
895 sourceTime = (yr, mth, dy, startHour, startMinute,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
896 startSecond, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
897 ctx.updateAccuracy(ctx.ACU_MONTH)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
898
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
899 elif self.ptc.CRE_WEEKDAY.match(unit):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
900 m = self.ptc.CRE_WEEKDAY.match(unit)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
901 debug and log.debug('CRE_WEEKDAY matched')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
902 wkdy = m.group()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
903
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
904 if modifier == 'eod':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
905 ctx.updateAccuracy(ctx.ACU_HOUR)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
906 # Calculate the upcoming weekday
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
907 sourceTime, subctx = self.parse(wkdy, sourceTime,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
908 VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
909 sTime = self.ptc.getSource(modifier, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
910 if sTime is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
911 sourceTime = sTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
912 ctx.updateAccuracy(ctx.ACU_HALFDAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
913 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
914 # unless one of these modifiers is being applied to the
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
915 # day-of-week, we want to start with target as the day
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
916 # in the current week.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
917 dowOffset = offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
918 if modifier not in ['next', 'last', 'prior', 'previous']:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
919 dowOffset = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
920
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
921 wkdy = self.ptc.WeekdayOffsets[wkdy]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
922 diff = self._CalculateDOWDelta(
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
923 wd, wkdy, dowOffset, self.ptc.DOWParseStyle,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
924 self.ptc.CurrentDOWParseStyle)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
925 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
926 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
927 target = start + datetime.timedelta(days=diff)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
928
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
929 if chunk1 != '':
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
930 # consider "one day before thursday": we need to parse chunk1 ("one day")
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
931 # and apply according to the offset ("before"), rather than allowing the
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
932 # remaining parse step to apply "one day" without the offset direction.
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
933 t, subctx = self.parse(chunk1, sourceTime, VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
934 if subctx.hasDateOrTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
935 delta = time.mktime(t) - time.mktime(sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
936 target = start + datetime.timedelta(days=diff) + datetime.timedelta(seconds=delta * offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
937 chunk1 = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
938
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
939 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
940 ctx.updateAccuracy(ctx.ACU_DAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
941
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
942 elif self.ptc.CRE_TIME.match(unit):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
943 m = self.ptc.CRE_TIME.match(unit)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
944 debug and log.debug('CRE_TIME matched')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
945 (yr, mth, dy, hr, mn, sec, wd, yd, isdst), subctx = \
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
946 self.parse(unit, None, VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
947
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
948 start = datetime.datetime(yr, mth, dy, hr, mn, sec)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
949 target = start + datetime.timedelta(days=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
950 sourceTime = target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
951
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
952 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
953 # check if the remaining text is parsable and if so,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
954 # use it as the base time for the modifier source time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
955
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
956 debug and log.debug('check for modifications '
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
957 'to source time [%s] [%s]',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
958 chunk1, unit)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
959
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
960 unit = unit.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
961 if unit:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
962 s = '%s %s' % (unit, chunk2)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
963 t, subctx = self.parse(s, sourceTime, VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
964
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
965 if subctx.hasDate: # working with dates
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
966 u = unit.lower()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
967 if u in self.ptc.Months or \
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
968 u in self.ptc.shortMonths:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
969 yr, mth, dy, hr, mn, sec, wd, yd, isdst = t
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
970 start = datetime.datetime(
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
971 yr, mth, dy, hr, mn, sec)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
972 t = self.inc(start, year=offset).timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
973 elif u in self.ptc.Weekdays:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
974 t = t + datetime.timedelta(weeks=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
975
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
976 if subctx.hasDateOrTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
977 sourceTime = t
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
978 chunk2 = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
979
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
980 chunk1 = chunk1.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
981
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
982 # if the word after next is a number, the string is more than
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
983 # likely to be "next 4 hrs" which we will have to combine the
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
984 # units with the rest of the string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
985 if chunk1:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
986 try:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
987 m = list(self.ptc.CRE_NUMBER.finditer(chunk1))[-1]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
988 except IndexError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
989 pass
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
990 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
991 qty = None
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
992 debug and log.debug('CRE_NUMBER matched')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
993 qty = self._quantityToReal(m.group()) * offset
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
994 chunk1 = '%s%s%s' % (chunk1[:m.start()],
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
995 qty, chunk1[m.end():])
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
996 t, subctx = self.parse(chunk1, sourceTime,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
997 VERSION_CONTEXT_STYLE)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
998
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
999 chunk1 = ''
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1000
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1001 if subctx.hasDateOrTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1002 sourceTime = t
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1003
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1004 debug and log.debug('looking for modifier %s', modifier)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1005 sTime = self.ptc.getSource(modifier, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1006 if sTime is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1007 debug and log.debug('modifier found in sources')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1008 sourceTime = sTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1009 ctx.updateAccuracy(ctx.ACU_HALFDAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1010
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1011 debug and log.debug('returning chunk = "%s %s" and sourceTime = %s',
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1012 chunk1, chunk2, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1013
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1014 return '%s %s' % (chunk1, chunk2), sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1015
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1016 def _evalDT(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1017 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1018 Calculate the datetime from known format like RFC822 or W3CDTF
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1019
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1020 Examples handled::
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1021 RFC822, W3CDTF formatted dates
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1022 HH:MM[:SS][ am/pm]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1023 MM/DD/YYYY
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1024 DD MMMM YYYY
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1025
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1026 @type datetimeString: string
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1027 @param datetimeString: text to try and parse as more "traditional"
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1028 date/time text
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1029 @type sourceTime: struct_time
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1030 @param sourceTime: C{struct_time} value to use as the base
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1031
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1032 @rtype: datetime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1033 @return: calculated C{struct_time} value or current C{struct_time}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1034 if not parsed
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1035 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1036 ctx = self.currentContext
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1037 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1038
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1039 # Given string date is a RFC822 date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1040 if sourceTime is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1041 sourceTime = _parse_date_rfc822(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1042 debug and log.debug(
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1043 'attempt to parse as rfc822 - %s', str(sourceTime))
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1044
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1045 if sourceTime is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1046 (yr, mth, dy, hr, mn, sec, wd, yd, isdst, _) = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1047 ctx.updateAccuracy(ctx.ACU_YEAR, ctx.ACU_MONTH, ctx.ACU_DAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1048
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1049 if hr != 0 and mn != 0 and sec != 0:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1050 ctx.updateAccuracy(ctx.ACU_HOUR, ctx.ACU_MIN, ctx.ACU_SEC)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1051
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1052 sourceTime = (yr, mth, dy, hr, mn, sec, wd, yd, isdst)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1053
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1054 # Given string date is a W3CDTF date
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1055 if sourceTime is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1056 sourceTime = _parse_date_w3dtf(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1057
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1058 if sourceTime is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1059 ctx.updateAccuracy(ctx.ACU_YEAR, ctx.ACU_MONTH, ctx.ACU_DAY,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1060 ctx.ACU_HOUR, ctx.ACU_MIN, ctx.ACU_SEC)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1061
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1062 if sourceTime is None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1063 sourceTime = time.localtime()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1064
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1065 return sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1066
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1067 def _evalUnits(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1068 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1069 Evaluate text passed by L{_partialParseUnits()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1070 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1071 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1072 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1073
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1074 # Given string is a time string with units like "5 hrs 30 min"
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1075 modifier = '' # TODO
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1076
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1077 m = self.ptc.CRE_UNITS.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1078 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1079 units = m.group('units')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1080 quantity = s[:m.start('units')]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1081
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1082 sourceTime = self._buildTime(sourceTime, quantity, modifier, units)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1083 return sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1084
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1085 def _evalQUnits(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1086 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1087 Evaluate text passed by L{_partialParseQUnits()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1088 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1089 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1090 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1091
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1092 # Given string is a time string with single char units like "5 h 30 m"
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1093 modifier = '' # TODO
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1094
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1095 m = self.ptc.CRE_QUNITS.search(s)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1096 if m is not None:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1097 units = m.group('qunits')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1098 quantity = s[:m.start('qunits')]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1099
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1100 sourceTime = self._buildTime(sourceTime, quantity, modifier, units)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1101 return sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1102
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1103 def _evalDateStr(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1104 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1105 Evaluate text passed by L{_partialParseDateStr()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1106 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1107 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1108 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1109
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1110 # Given string is in the format "May 23rd, 2005"
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1111 debug and log.debug('checking for MMM DD YYYY')
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1112 return self.parseDateText(s, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1113
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1114 def _evalDateStd(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1115 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1116 Evaluate text passed by L{_partialParseDateStd()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1117 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1118 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1119 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1120
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1121 # Given string is in the format 07/21/2006
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1122 return self.parseDate(s, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1123
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1124 def _evalDayStr(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1125 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1126 Evaluate text passed by L{_partialParseDaystr()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1127 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1128 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1129 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1130
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1131 # Given string is a natural language date string like today, tomorrow..
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1132 (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1133
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1134 try:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1135 offset = self.ptc.dayOffsets[s]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1136 except KeyError:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1137 offset = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1138
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1139 if self.ptc.StartTimeFromSourceTime:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1140 startHour = hr
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1141 startMinute = mn
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1142 startSecond = sec
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1143 else:
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1144 startHour = 9
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1145 startMinute = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1146 startSecond = 0
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1147
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1148 self.currentContext.updateAccuracy(pdtContext.ACU_DAY)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1149 start = datetime.datetime(yr, mth, dy, startHour,
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1150 startMinute, startSecond)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1151 target = start + datetime.timedelta(days=offset)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1152 return target.timetuple()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1153
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1154 def _evalWeekday(self, datetimeString, sourceTime):
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1155 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1156 Evaluate text passed by L{_partialParseWeekday()}
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1157 """
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1158 s = datetimeString.strip()
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1159 sourceTime = self._evalDT(datetimeString, sourceTime)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1160
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1161 # Given string is a weekday
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1162 yr, mth, dy, hr, mn, sec, wd, yd, isdst = sourceTime
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1163
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1164 start = datetime.datetime(yr, mth, dy, hr, mn, sec)
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1165 wkdy = self.ptc.WeekdayOffsets[s]
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff changeset
1166
83b1bc99457c upgrade parsedatetime from 0.8.7 to 2.1
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5185
diff