Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

rockbox mtime conversion
Tuesday, August 26th, 2008 at 6:47:55pm UTC 

  1. def to_bin(n):
  2.     bStr = ''
  3.     if n == 0:
  4.         return '0'
  5.     while n > 0:
  6.         bStr = str(n % 2) + bStr
  7.         n = n >> 1
  8.     return bStr
  9.  
  10. def mtime_to_unix(mtime):
  11.     bits = to_bin(mtime)
  12.     print bits
  13.     year = int(bits[:5], 2) + 1980
  14.     month = int(bits[5:9], 2)
  15.     day = int(bits[9:14], 2)
  16.     hour = int(bits[16:20], 2)
  17.     minute = int(bits[20:25], 2)
  18.     second = int(bits[25:30], 2)
  19.     print (year, month, day, hour, minute, second)
  20.     t = time.mktime((year, month, day, hour, minute, second, -1, -1, -1))
  21.     return t
  22.  
  23. def unix_to_mtime(unix):
  24.     year, month, day, hour, minute, second = [ to_bin(x) for x in time.localtime(unix)[:-3] ]
  25.     print time.localtime(unix)[:-3]
  26.     year = to_bin(int(year,2)-1980)
  27.     def pad(value, num):
  28.         return ("0"*num)[len(value):]+value
  29.     bits = ""
  30.     bits += pad(year, 5)
  31.     bits += pad(month, 4)
  32.     bits += pad(day, 5)
  33.     bits += "01" # BAD BAD BAD BAD, also, doesn't work
  34.     bits += pad(hour, 4)
  35.     bits += pad(minute, 5)
  36.     bits += pad(second, 5)
  37.     print bits
  38.     mt = int(bits, 2)
  39.     return mt
  40.  
  41. ### END CODE ###
  42. Example output:
  43.  
  44. >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(957849363))
  45. 111001000101111001111100010011
  46. (2008, 8, 23, 7, 24, 19)
  47. (2008, 8, 23, 7, 24, 19, 5, 236, 1)
  48. 111001000101110101111100010011
  49. 957832979
  50. >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(957240124))
  51. 111001000011100101001100111100
  52. (2008, 8, 14, 4, 25, 28)
  53. (2008, 8, 14, 4, 25, 28, 3, 227, 1)
  54. 111001000011100101001100111100
  55. 957240124
  56. >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(953842093))
  57. 111000110110100111100110101101
  58. (2008, 6, 26, 14, 13, 13)
  59. (2008, 6, 26, 14, 13, 13, 3, 178, 1)
  60. 111000110110100111100110101101
  61. 953842093
  62. >>> rbdb.unix_to_mtime(rbdb.mtime_to_unix(953842177))
  63. 111000110110100111101000000001
  64. (2008, 6, 26, 14, 16, 1)
  65. (2008, 6, 26, 14, 16, 1, 3, 178, 1)
  66. 111000110110100111101000000001
  67. 953842177

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right