blob: a7075a463ddfe6a7a7dce99cec573994d1cbe2b1 [file] [log] [blame]
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Utility for determining whether we need to rate limit.
Don't rate limit on:
- Weekends
- PST Nighttime
- U.S. Holidays
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import datetime
import holidays
import pytz
_US_HOLIDAYS = holidays.UnitedStates()
_PST = pytz.timezone('US/Pacific')
def HaveUnlimitedResources():
"""Returns whether it is currently a time with low expected builder load."""
# App engine is always UTC
now = pytz.utc.localize(datetime.datetime.now())
now = now.astimezone(_PST)
return (
now.weekday() >= 5 # Is a weekend
or _IsNightTime(now)
or _IsHoliday(now))
def _IsNightTime(now):
"""Whether we are sufficiently outside of work hours to not rate limit.
Args:
now: A (PST-localized) datetime.datetime instance.
"""
return now.hour < 9 or now.hour > 18
def _IsHoliday(now):
"""Whether today is a US holiday.
Args:
now: A (PST-localized) datetime.datetime instance.
"""
return now.date() in _US_HOLIDAYS