Last active
March 10, 2019 11:49
-
-
Save pudquick/05e877faee1ad5059690 to your computer and use it in GitHub Desktop.
A simple estimated warranty date output script - calculates date as manufacture date of your Mac + 3 years
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 | |
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 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, 3).strftime('%Y-%m-%d') | |
def main(): | |
try: | |
serial = sys.argv[1].strip() | |
print manufacture_date(serial) | |
except: | |
print "Error - Invalid Serial Number" | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment