+def encode_datetime(x,r):
+ """Bencode a datetime value in UTC.
+
+ If the datetime object has time zone info, it is converted to UTC time.
+ Otherwise it is assumed that the time is already in UTC time.
+ Microseconds are removed.
+
+ @type x: C{datetime.datetime}
+ @param x: the data to encode
+ @type r: C{list}
+ @param r: the currently bencoded data, to which the bencoding of x
+ will be appended
+
+ """
+
+ date = x.replace(microsecond = 0)
+ offset = date.utcoffset()
+ if offset is not None:
+ utcdate = date.replace(tzinfo = None) + offset
+ else:
+ utcdate = date
+ r.extend(('t',utcdate.isoformat(),'e'))
+