~bzr-pqm/bzr/bzr.dev

0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
1
#!/usr/bin/env python
2
"""\
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
3
Common entries, like strings, etc, for the bundle reading + writing code.
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
4
"""
5
0.5.79 by John Arbash Meinel
Added common to the set of tests, fixed a problem with time conversions.
6
import bzrlib
7
1185.82.130 by Aaron Bentley
Rename changesets to revision bundles
8
header_str = 'Bazaar revision bundle v'
1185.82.38 by Aaron Bentley
Changed changeset version number to 0.7
9
version = (0, 7)
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
10
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
11
0.5.7 by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information.
12
def get_header():
13
    return [
14
        header_str + '.'.join([str(v) for v in version]),
15
        ''
16
    ]
17
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
18
      
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
19
def format_highres_date(t, offset=0):
20
    """Format a date, such that it includes higher precision in the
21
    seconds field.
22
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
23
    :param t:   The local time in fractional seconds since the epoch
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
24
    :type t: float
25
    :param offset:  The timezone offset in integer seconds
26
    :type offset: int
27
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
28
    Example: format_highres_date(time.time(), -time.timezone)
29
    this will return a date stamp for right now,
30
    formatted for the local timezone.
31
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
32
    >>> from bzrlib.osutils import format_date
33
    >>> format_date(1120153132.350850105, 0)
34
    'Thu 2005-06-30 17:38:52 +0000'
35
    >>> format_highres_date(1120153132.350850105, 0)
36
    'Thu 2005-06-30 17:38:52.350850105 +0000'
37
    >>> format_date(1120153132.350850105, -5*3600)
38
    'Thu 2005-06-30 12:38:52 -0500'
39
    >>> format_highres_date(1120153132.350850105, -5*3600)
40
    'Thu 2005-06-30 12:38:52.350850105 -0500'
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
41
    >>> format_highres_date(1120153132.350850105, 7200)
42
    'Thu 2005-06-30 19:38:52.350850105 +0200'
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
43
    """
44
    import time
45
    assert isinstance(t, float)
46
    
47
    # This has to be formatted for "original" date, so that the
48
    # revision XML entry will be reproduced faithfully.
49
    if offset == None:
50
        offset = 0
51
    tt = time.gmtime(t + offset)
52
53
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
54
            + ('%.9f' % (t - int(t)))[1:] # Get the high-res seconds, but
55
                                          # ignore the 0
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
56
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
57
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
58
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
59
def unpack_highres_date(date):
60
    """This takes the high-resolution date stamp, and
61
    converts it back into the tuple (timestamp, timezone)
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
62
    Where timestamp is in real UTC since epoch seconds, and timezone is an
63
    integer number of seconds offset.
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
64
65
    :param date: A date formated by format_highres_date
66
    :type date: string
67
1185.82.125 by Aaron Bentley
More cleanups
68
    >>> import time
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
69
    >>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
70
    (1120153132.3508501, -18000)
71
    >>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
72
    (1120153132.3508501, 0)
73
    >>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
74
    (1120153132.3508501, 7200)
75
    >>> from bzrlib.osutils import local_time_offset
76
    >>> t = time.time()
77
    >>> o = local_time_offset()
78
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
79
    >>> t == t2
80
    True
81
    >>> o == o2
82
    True
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
83
    """
0.5.79 by John Arbash Meinel
Added common to the set of tests, fixed a problem with time conversions.
84
    import time, calendar
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
85
    # Up until the first period is a datestamp that is generated
86
    # as normal from time.strftime, so use time.strptime to
87
    # parse it
88
    dot_loc = date.find('.')
89
    if dot_loc == -1:
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
90
        raise ValueError('Date string does not contain high-precision seconds:'
91
                         ' %r' % date)
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
92
    base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
93
    fract_seconds, offset = date[dot_loc:].split()
94
    fract_seconds = float(fract_seconds)
95
    offset = int(offset)
96
    offset = int(offset / 100) * 3600 + offset % 100
97
    
0.5.79 by John Arbash Meinel
Added common to the set of tests, fixed a problem with time conversions.
98
    # time.mktime returns localtime, but calendar.timegm returns UTC time
99
    timestamp = calendar.timegm(base_time)
100
    timestamp -= offset
0.5.40 by John Arbash Meinel
Added some highres formatting of datestamps.
101
    # Add back in the fractional seconds
102
    timestamp += fract_seconds
103
    return (timestamp, offset)
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
104
1185.82.15 by Aaron Bentley
Disabled validate_revisions (needs info it doesn't have), updated API to repos
105
0.5.39 by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree.
106
if __name__ == '__main__':
107
    import doctest
108
    doctest.testmod()