~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/common.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-10 15:15:40 UTC
  • mto: This revision was merged to the branch mainline in revision 1850.
  • Revision ID: john@arbash-meinel.com-20060710151540-81ad659edf6391c0
Removed duplicated highres date code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    ]
17
17
 
18
18
      
19
 
def format_highres_date(t, offset=0):
20
 
    """Format a date, such that it includes higher precision in the
21
 
    seconds field.
22
 
 
23
 
    :param t:   The local time in fractional seconds since the epoch
24
 
    :type t: float
25
 
    :param offset:  The timezone offset in integer seconds
26
 
    :type offset: int
27
 
 
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
 
 
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'
41
 
    >>> format_highres_date(1120153132.350850105, 7200)
42
 
    'Thu 2005-06-30 19:38:52.350850105 +0200'
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)
54
 
            + ('%.9f' % (t - int(t)))[1:] # Get the high-res seconds, but
55
 
                                          # ignore the 0
56
 
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
57
 
 
58
 
 
59
 
def unpack_highres_date(date):
60
 
    """This takes the high-resolution date stamp, and
61
 
    converts it back into the tuple (timestamp, timezone)
62
 
    Where timestamp is in real UTC since epoch seconds, and timezone is an
63
 
    integer number of seconds offset.
64
 
 
65
 
    :param date: A date formated by format_highres_date
66
 
    :type date: string
67
 
 
68
 
    >>> import time
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
83
 
    """
84
 
    import time, calendar
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:
90
 
        raise ValueError('Date string does not contain high-precision seconds:'
91
 
                         ' %r' % date)
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
 
    
98
 
    # time.mktime returns localtime, but calendar.timegm returns UTC time
99
 
    timestamp = calendar.timegm(base_time)
100
 
    timestamp -= offset
101
 
    # Add back in the fractional seconds
102
 
    timestamp += fract_seconds
103
 
    return (timestamp, offset)
104
 
 
105
 
 
106
19
if __name__ == '__main__':
107
20
    import doctest
108
21
    doctest.testmod()