~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: Samuel Bronson
  • Date: 2012-08-30 20:36:18 UTC
  • mto: (6015.57.3 2.4)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: naesten@gmail.com-20120830203618-y2dzw91nqpvpgxvx
Update INSTALL for switch to Python 2.6 and up.

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
 
 
19
17
import calendar
20
18
import time
21
 
import re
22
19
 
23
20
from bzrlib import osutils
24
21
 
127
124
            date_fmt='%Y-%m-%d %H:%M:%S')
128
125
 
129
126
 
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
 
 
135
127
def parse_patch_date(date_str):
136
128
    """Parse a patch-style date into a POSIX timestamp and offset.
137
129
 
138
130
    Inverse of format_patch_date.
139
131
    """
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
 
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
154
139
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
155
140
    # adjust seconds according to offset before converting to POSIX
156
141
    # timestamp, to avoid edge problems