Created
June 5, 2019 17:04
-
-
Save chilcote/a00af7516d7a96c3aef927e94a35e840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, datetime, time, subprocess | |
def apple_year_offset(dateobj, years=0): | |
# Convert to a maleable format | |
mod_time = dateobj.timetuple() | |
# Offset year by number of years | |
mod_time = time.struct_time(tuple([mod_time[0]+years]) + mod_time[1:]) | |
# Convert back to a datetime obj | |
return datetime.datetime.fromtimestamp(int(time.mktime(mod_time))) | |
def get_serial(): | |
'''Returns the serial number of this Mac''' | |
cmd = ['/usr/sbin/ioreg', '-c', 'IOPlatformExpertDevice', '-d', '2'] | |
output = subprocess.check_output(cmd) | |
for line in output.splitlines(): | |
if 'IOPlatformSerialNumber' in line: | |
return line.split(' = ')[1].replace('\"','') | |
return None | |
def manufacture_date(serial): | |
# http://www.macrumors.com/2010/04/16/apple-tweaks-serial-number-format-with-new-macbook-pro/ | |
est_date = u'' | |
if 10 < len(serial) < 13: | |
if len(serial) == 11: | |
# Old format | |
year = serial[2].lower() | |
est_year = 2000 + ' 3456789012'.index(year) | |
week = int(serial[3:5]) - 1 | |
year_time = datetime.date(year=est_year, month=1, day=1) | |
if (week): | |
week_dif = datetime.timedelta(weeks=week) | |
year_time += week_dif | |
else: | |
# New format | |
alpha_year = 'cdfghjklmnpqrstvwxyz' | |
year = serial[3].lower() | |
est_year = 2010 + (alpha_year.index(year) / 2) | |
# 1st or 2nd half of the year | |
est_half = alpha_year.index(year) % 2 | |
week = serial[4].lower() | |
alpha_week = ' 123456789cdfghjklmnpqrtvwxy' | |
est_week = alpha_week.index(week) + (est_half * 26) - 1 | |
year_time = datetime.date(year=est_year, month=1, day=1) | |
if (est_week): | |
week_dif = datetime.timedelta(weeks=est_week) | |
year_time += week_dif | |
return apple_year_offset(year_time).strftime('%Y-%m-%d %H:%M:%S') | |
def main(): | |
serial = get_serial() | |
print "<result>" + manufacture_date(serial) + "</result>" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment