~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
import calendar
18
20
import time
 
21
import re
19
22
 
20
23
from bzrlib import osutils
21
24
 
124
127
            date_fmt='%Y-%m-%d %H:%M:%S')
125
128
 
126
129
 
 
130
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
 
131
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
 
132
RE_PATCHDATE = re.compile("(\d+-\d+-\d+\s+\d+:\d+:\d+)\s*([+-]\d\d)(\d\d)$")
 
133
RE_PATCHDATE_NOOFFSET = re.compile("\d+-\d+-\d+\s+\d+:\d+:\d+$")
 
134
 
127
135
def parse_patch_date(date_str):
128
136
    """Parse a patch-style date into a POSIX timestamp and offset.
129
137
 
130
138
    Inverse of format_patch_date.
131
139
    """
132
 
    secs_str = date_str[:-6]
133
 
    offset_str = date_str[-5:]
134
 
    if len(offset_str) != 5:
135
 
        raise ValueError(
136
 
            "invalid timezone %r" % offset_str)
137
 
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
138
 
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
 
140
    match = RE_PATCHDATE.match(date_str)
 
141
    if match is None:
 
142
        if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
 
143
            raise ValueError("time data %r is missing a timezone offset"
 
144
                % date_str)
 
145
        else:
 
146
            raise ValueError("time data %r does not match format " % date_str
 
147
                + "'%Y-%m-%d %H:%M:%S %z'")
 
148
    secs_str = match.group(1)
 
149
    offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
 
150
    if abs(offset_hours) >= 24 or offset_mins >= 60:
 
151
        raise ValueError("invalid timezone %r" %
 
152
            (match.group(2) + match.group(3)))
 
153
    offset = offset_hours * 3600 + offset_mins * 60
139
154
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
140
155
    # adjust seconds according to offset before converting to POSIX
141
156
    # timestamp, to avoid edge problems