/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 common.py

  • Committer: John Arbash Meinel
  • Date: 2005-06-30 19:44:51 UTC
  • mto: (0.5.85) (1185.82.1 bzr-w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: john@arbash-meinel.com-20050630194451-8ecb7d6d880d7b42
Added some highres formatting of datestamps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
        """Build the final description of the tree, based on
65
65
        the changeset_info object.
66
66
        """
 
67
        self.base_tree = self.branch.revision_tree(self.changeset_info.base)
67
68
        
68
69
def format_highres_date(t, offset=0):
69
70
    """Format a date, such that it includes higher precision in the
70
71
    seconds field.
71
72
 
72
 
    :param t:   UTC time in fractional seconds
 
73
    :param t:   The local time in fractional seconds since the epoch
73
74
    :type t: float
74
75
    :param offset:  The timezone offset in integer seconds
75
76
    :type offset: int
76
77
 
 
78
    Example: format_highres_date(time.time(), -time.timezone)
 
79
    this will return a date stamp for right now,
 
80
    formatted for the local timezone.
 
81
 
77
82
    >>> from bzrlib.osutils import format_date
78
83
    >>> format_date(1120153132.350850105, 0)
79
84
    'Thu 2005-06-30 17:38:52 +0000'
83
88
    'Thu 2005-06-30 12:38:52 -0500'
84
89
    >>> format_highres_date(1120153132.350850105, -5*3600)
85
90
    'Thu 2005-06-30 12:38:52.350850105 -0500'
 
91
    >>> format_highres_date(1120153132.350850105, 7200)
 
92
    'Thu 2005-06-30 19:38:52.350850105 +0200'
86
93
    """
87
 
    from bzrlib.errors import BzrError
88
94
    import time
89
95
    assert isinstance(t, float)
90
96
    
107
113
    :param date: A date formated by format_highres_date
108
114
    :type date: string
109
115
 
 
116
    >>> import time, random
 
117
    >>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
 
118
    (1120153132.3508501, -18000)
 
119
    >>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
 
120
    (1120153132.3508501, 0)
 
121
    >>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
 
122
    (1120153132.3508501, 7200)
 
123
    >>> from bzrlib.osutils import local_time_offset
 
124
    >>> t = time.time()
 
125
    >>> o = local_time_offset()
 
126
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
 
127
    >>> t == t2
 
128
    True
 
129
    >>> o == o2
 
130
    True
 
131
    >>> for count in xrange(500):
 
132
    ...   t += random.random()*24*3600*365*2 - 24*3600*364 # Random time within +/- 1 year
 
133
    ...   o = random.randint(-12,12)*3600 # Random timezone
 
134
    ...   date = format_highres_date(t, o)
 
135
    ...   t2, o2 = unpack_highres_date(date)
 
136
    ...   if t != t2 or o != o2:
 
137
    ...      print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
 
138
 
110
139
    """
 
140
    #from bzrlib.errors import BzrError
 
141
    from bzrlib.osutils import local_time_offset
 
142
    import time
 
143
    # Up until the first period is a datestamp that is generated
 
144
    # as normal from time.strftime, so use time.strptime to
 
145
    # parse it
 
146
    dot_loc = date.find('.')
 
147
    if dot_loc == -1:
 
148
        raise ValueError('Date string does not contain high-precision seconds: %r' % date)
 
149
    base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
 
150
    fract_seconds, offset = date[dot_loc:].split()
 
151
    fract_seconds = float(fract_seconds)
 
152
    offset = int(offset)
 
153
    offset = int(offset / 100) * 3600 + offset % 100
 
154
    
 
155
    # mktime returns the a local timestamp, not the timestamp based
 
156
    # on the offset given in the file, so we need to adjust based
 
157
    # on what the local offset is, and then re-adjust based on
 
158
    # offset read
 
159
    timestamp = time.mktime(base_time)
 
160
    timestamp += local_time_offset(timestamp) - offset
 
161
    # Add back in the fractional seconds
 
162
    timestamp += fract_seconds
 
163
    return (timestamp, offset)
111
164
 
112
165
if __name__ == '__main__':
113
166
    import doctest
114
167
    doctest.testmod()
 
168