/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/timestamp.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 13:10:35 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521131035-xltmj0b4f6fbybo8
Rename some windows stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
from bzrlib import osutils
 
23
from brzlib import osutils
21
24
 
22
25
 
23
26
def format_highres_date(t, offset=0):
33
36
    this will return a date stamp for right now,
34
37
    formatted for the local timezone.
35
38
 
36
 
    >>> from bzrlib.osutils import format_date
 
39
    >>> from brzlib.osutils import format_date
37
40
    >>> format_date(1120153132.350850105, 0)
38
41
    'Thu 2005-06-30 17:38:52 +0000'
39
42
    >>> format_highres_date(1120153132.350850105, 0)
54
57
    # revision XML entry will be reproduced faithfully.
55
58
    if offset is None:
56
59
        offset = 0
57
 
    tt = time.gmtime(t + offset)
 
60
    tt = osutils.gmtime(t + offset)
58
61
 
59
62
    return (osutils.weekdays[tt[6]] +
60
63
            time.strftime(" %Y-%m-%d %H:%M:%S", tt)
72
75
    :param date: A date formated by format_highres_date
73
76
    :type date: string
74
77
 
75
 
    >>> import time, random
76
 
    >>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
77
 
    (1120153132.3508501, -18000)
78
 
    >>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
79
 
    (1120153132.3508501, 0)
80
 
    >>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
81
 
    (1120153132.3508501, 7200)
82
 
    >>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
83
 
    (1152428738.867522, 19800)
84
 
    >>> from bzrlib.osutils import local_time_offset
85
 
    >>> t = time.time()
86
 
    >>> o = local_time_offset()
87
 
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
88
 
    >>> t == t2
89
 
    True
90
 
    >>> o == o2
91
 
    True
92
 
    >>> t -= 24*3600*365*2 # Start 2 years ago
93
 
    >>> o = -12*3600
94
 
    >>> for count in xrange(500):
95
 
    ...   t += random.random()*24*3600*30
96
 
    ...   o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
97
 
    ...   date = format_highres_date(t, o)
98
 
    ...   t2, o2 = unpack_highres_date(date)
99
 
    ...   if t != t2 or o != o2:
100
 
    ...      print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
101
 
    ...      break
102
 
 
103
78
    """
104
79
    # Weekday parsing is locale sensitive, so drop the weekday
105
80
    space_loc = date.find(' ')
144
119
    # give the epoch in utc
145
120
    if secs == 0:
146
121
        offset = 0
147
 
    if secs + offset < 0:
148
 
        from warnings import warn
149
 
        warn("gmtime of negative time (%s, %s) may not work on Windows" %
150
 
                (secs, offset))
151
122
    return osutils.format_date(secs, offset=offset,
152
123
            date_fmt='%Y-%m-%d %H:%M:%S')
153
124
 
154
125
 
 
126
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
 
127
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
 
128
RE_PATCHDATE = re.compile("(\d+-\d+-\d+\s+\d+:\d+:\d+)\s*([+-]\d\d)(\d\d)$")
 
129
RE_PATCHDATE_NOOFFSET = re.compile("\d+-\d+-\d+\s+\d+:\d+:\d+$")
 
130
 
155
131
def parse_patch_date(date_str):
156
132
    """Parse a patch-style date into a POSIX timestamp and offset.
157
133
 
158
134
    Inverse of format_patch_date.
159
135
    """
160
 
    secs_str = date_str[:-6]
161
 
    offset_str = date_str[-5:]
162
 
    if len(offset_str) != 5:
163
 
        raise ValueError(
164
 
            "invalid timezone %r" % offset_str)
165
 
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
166
 
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
 
136
    match = RE_PATCHDATE.match(date_str)
 
137
    if match is None:
 
138
        if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
 
139
            raise ValueError("time data %r is missing a timezone offset"
 
140
                % date_str)
 
141
        else:
 
142
            raise ValueError("time data %r does not match format " % date_str
 
143
                + "'%Y-%m-%d %H:%M:%S %z'")
 
144
    secs_str = match.group(1)
 
145
    offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
 
146
    if abs(offset_hours) >= 24 or offset_mins >= 60:
 
147
        raise ValueError("invalid timezone %r" %
 
148
            (match.group(2) + match.group(3)))
 
149
    offset = offset_hours * 3600 + offset_mins * 60
167
150
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
168
151
    # adjust seconds according to offset before converting to POSIX
169
152
    # timestamp, to avoid edge problems