def to_bin(n): bStr = '' if n == 0: return '0' while n > 0: bStr = str(n % 2) + bStr n = n >> 1 return bStr def mtime_to_unix(mtime): bits = to_bin(mtime) print bits year = int(bits[:5], 2) + 1980 month = int(bits[5:9], 2) day = int(bits[9:14], 2) hour = int(bits[16:20], 2) minute = int(bits[20:25], 2) second = int(bits[25:30], 2) print (year, month, day, hour, minute, second) t = time.mktime((year, month, day, hour, minute, second, -1, -1, -1)) return t def unix_to_mtime(unix): year, month, day, hour, minute, second = [ to_bin(x) for x in time.localtime(unix)[:-3] ] print time.localtime(unix)[:-3] year = to_bin(int(year,2)-1980) def pad(value, num): return ("0"*num)[len(value):]+value bits = "" bits += pad(year, 5) bits += pad(month, 4) bits += pad(day, 5) bits += "01" # BAD BAD BAD BAD, also, doesn't work bits += pad(hour, 4) bits += pad(minute, 5) bits += pad(second, 5) print bits mt = int(bits, 2) return mt ### END CODE ### Example output: >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(957849363)) 111001000101111001111100010011 (2008, 8, 23, 7, 24, 19) (2008, 8, 23, 7, 24, 19, 5, 236, 1) 111001000101110101111100010011 957832979 >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(957240124)) 111001000011100101001100111100 (2008, 8, 14, 4, 25, 28) (2008, 8, 14, 4, 25, 28, 3, 227, 1) 111001000011100101001100111100 957240124 >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(953842093)) 111000110110100111100110101101 (2008, 6, 26, 14, 13, 13) (2008, 6, 26, 14, 13, 13, 3, 178, 1) 111000110110100111100110101101 953842093 >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(953842177)) 111000110110100111101000000001 (2008, 6, 26, 14, 16, 1) (2008, 6, 26, 14, 16, 1, 3, 178, 1) 111000110110100111101000000001 953842177