##// END OF EJS Templates
setup: change url to github
setup: change url to github

File last commit:

r153:32f4b641
r196:472d1df0 master
Show More
date_utils.py
56 lines | 1.9 KiB | text/x-python | PythonLexer
project: add missing lib directory
r2 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: add missing lib directory
r2 #
license: change the license to Apache 2.0
r112 # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
project: add missing lib directory
r2 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: add missing lib directory
r2 #
license: change the license to Apache 2.0
r112 # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project: add missing lib directory
r2
from datetime import tzinfo, timedelta, datetime
from dateutil.relativedelta import relativedelta
import logging
log = logging.getLogger(__name__)
def to_relativedelta(time_delta):
black: reformat source
r153 return relativedelta(
seconds=int(time_delta.total_seconds()), microseconds=time_delta.microseconds
)
project: add missing lib directory
r2
black: reformat source
r153 def convert_date(date_str, return_utcnow_if_wrong=True, normalize_future=False):
project: add missing lib directory
r2 utcnow = datetime.utcnow()
if isinstance(date_str, datetime):
# get rid of tzinfo
return date_str.replace(tzinfo=None)
if not date_str and return_utcnow_if_wrong:
return utcnow
try:
try:
black: reformat source
r153 if "Z" in date_str:
date_str = date_str[: date_str.index("Z")]
if "." in date_str:
date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%f")
project: add missing lib directory
r2 else:
black: reformat source
r153 date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S")
project: add missing lib directory
r2 except Exception:
# bw compat with old client
black: reformat source
r153 date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S,%f")
project: add missing lib directory
r2 except Exception:
if return_utcnow_if_wrong:
date = utcnow
else:
date = None
if normalize_future and date and date > (utcnow + timedelta(minutes=3)):
black: reformat source
r153 log.warning("time %s in future + 3 min, normalizing" % date)
project: add missing lib directory
r2 return utcnow
return date