"""Returns a timestamp that approximates the build date.
build_type impacts the timestamp generated, both relative to the date of the
last recent commit:
- default: the build date is set to the most recent first Sunday of a month at
5:00am. The reason is that it is a time where invalidating the build cache
shouldn't have major repercussions (due to lower load).
- official: the build date is set to the time of the most recent commit.
Either way, it is guaranteed to be in the past and always in UTC.
"""
import argparse
import calendar
import datetime
import doctest
import os
import sys
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
def GetFirstSundayOfMonth(year, month):
"""Returns the first sunday of the given month of the given year.
>>> GetFirstSundayOfMonth(2016, 2)
7
>>> GetFirstSundayOfMonth(2016, 3)
6
>>> GetFirstSundayOfMonth(2000, 1)
2
"""
weeks = calendar.Calendar().monthdays2calendar(year, month)
return [date_day[0] for date_day in weeks[0] if date_day[1] == 6][0]
def GetUnofficialBuildDate(build_date):
"""Gets the approximate build date given the specific build type.
>>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 6, 1, 2, 3))
datetime.datetime(2016, 1, 3, 5, 0)
>>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 7, 5))
datetime.datetime(2016, 2, 7, 5, 0)
>>> GetUnofficialBuildDate(datetime.datetime(2016, 2, 8, 5))
datetime.datetime(2016, 2, 7, 5, 0)
"""
if build_date.hour < 5:
build_date = build_date - datetime.timedelta(days=1)
build_date = datetime.datetime(build_date.year, build_date.month,
build_date.day, 5, 0, 0)
day = build_date.day
month = build_date.month
year = build_date.year
first_sunday = GetFirstSundayOfMonth(year, month)
if day >= first_sunday:
day = first_sunday
else:
month -= 1
if month == 0:
month = 12
year -= 1
day = GetFirstSundayOfMonth(year, month)
return datetime.datetime(
year, month, day, build_date.hour, build_date.minute, build_date.second)
def main():
if doctest.testmod()[0]:
return 1
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument(
'build_type', help='The type of build', choices=('official', 'default'))
args = argument_parser.parse_args()
lastchange_file = os.path.join(THIS_DIR, 'util', 'LASTCHANGE.committime')
last_commit_timestamp = int(open(lastchange_file).read())
build_date = datetime.datetime.utcfromtimestamp(last_commit_timestamp)
offset = 0
if args.build_type == 'official':
if os.name == 'nt':
version_path = os.path.join(THIS_DIR, os.pardir, 'chrome', 'VERSION')
with open(version_path) as f:
patch_line = f.readlines()[3].strip()
assert patch_line.startswith('PATCH=')
offset = int(patch_line[6:])
else:
build_date = GetUnofficialBuildDate(build_date)
print(offset + int(calendar.timegm(build_date.utctimetuple())))
return 0
if __name__ == '__main__':
sys.exit(main())