From e75c7078ce1337502429e2adb4e576cdc6996b47 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Fri, 22 Dec 2017 07:39:00 -0800 Subject: [PATCH] Move getTimeOrDeltaFromNow to same location as similar routines (#664) --- src/gam.py | 82 +++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/gam.py b/src/gam.py index 1a88d468..5877be78 100755 --- a/src/gam.py +++ b/src/gam.py @@ -200,6 +200,46 @@ def getYYYYMMDD(i, emptyOK=False, returnTimeStamp=False): print u'ERROR: expected a <{0}>'.format(YYYYMMDD_FORMAT_REQUIRED) sys.exit(2) +def getTimeOrDeltaFromNow(time_string): + """Get an ISO 8601 date/time or a positive/negative delta applied to now. + + Args: + time_string (string): The time or delta (e.g. '2017-09-01T12:34:56Z' or '-4h') + + Returns: + string: iso8601 formatted datetime in UTC. + + Exits: + 2: Not a valid delta. + + """ + DELTA_PATTERN = re.compile(r'^([+-])(\d{1,4})([wdhm])$') + time_string = time_string.strip() + if not time_string or time_string[0] not in [u'+', u'-']: + return time_string + mg = re.match(DELTA_PATTERN, time_string.lower()) + if mg is None: + print u'ERROR: %s is not a valid delta, expected (+|-)(m|h|d|w)' % time_string + sys.exit(2) + + sign = mg.group(1) + delta = int(mg.group(2)) + unit = mg.group(3) + + if unit == u'w': + deltaTime = datetime.timedelta(weeks=delta) + elif unit == u'd': + deltaTime = datetime.timedelta(days=delta) + elif unit == u'h': + deltaTime = datetime.timedelta(hours=delta) + elif unit == u'm': + deltaTime = datetime.timedelta(minutes=delta) + + if sign == u'-': + return (datetime.datetime.utcnow() - deltaTime).isoformat() + u'Z' + else: + return (datetime.datetime.utcnow() + deltaTime).isoformat() + u'Z' + # Get domain from email address def getEmailAddressDomain(emailAddress): atLoc = emailAddress.find(u'@') @@ -964,46 +1004,6 @@ def convertEmailAddressToUID(emailAddressOrUID, cd=None, email_type=u'user'): return None return normalizedEmailAddressOrUID -def getTimeOrDeltaFromNow(time_string): - """Get an ISO 8601 date/time or a positive/negative delta applied to now. - - Args: - time_string (string): The time or delta (e.g. '2017-09-01T12:34:56Z' or '-4h') - - Returns: - string: iso8601 formatted datetime in UTC. - - Exits: - 2: Not a valid delta. - - """ - DELTA_PATTERN = re.compile(r'^([+-])(\d{1,4})([wdhm])$') - time_string = time_string.strip() - if not time_string or time_string[0] not in [u'+', u'-']: - return time_string - mg = re.match(DELTA_PATTERN, time_string.lower()) - if mg is None: - print u'ERROR: %s is not a valid delta, expected (+|-)(m|h|d|w)' % time_string - sys.exit(2) - - sign = mg.group(1) - delta = int(mg.group(2)) - unit = mg.group(3) - - if unit == u'w': - deltaTime = datetime.timedelta(weeks=delta) - elif unit == u'd': - deltaTime = datetime.timedelta(days=delta) - elif unit == u'h': - deltaTime = datetime.timedelta(hours=delta) - elif unit == u'm': - deltaTime = datetime.timedelta(minutes=delta) - - if sign == u'-': - return (datetime.datetime.utcnow() - deltaTime).isoformat() + u'Z' - else: - return (datetime.datetime.utcnow() + deltaTime).isoformat() + u'Z' - def buildGAPIServiceObject(api, act_as, showAuthError=True): http = httplib2.Http(disable_ssl_certificate_validation=GC_Values[GC_NO_VERIFY_SSL], cache=GM_Globals[GM_CACHE_DIR]) @@ -11210,7 +11210,7 @@ def doPrintCrosDevices(): projection = u'FULL' selectDeviceFiles = True noLists = False - if field in CROS_RECENT_USERS_ARGUMENTS: + elif field in CROS_RECENT_USERS_ARGUMENTS: projection = u'FULL' selectRecentUsers = True noLists = False