def parse_with_formats(date_string, date_formats, settings): """ Parse with formats and return a dictionary with 'period' and 'obj_date'. :returns: :class:`datetime.datetime`, dict or None """ period = 'day' for date_format in date_formats: try: date_obj = datetime.strptime(date_string, date_format) except ValueError: continue else: # If format does not include the day, use last day of the month # instead of first, because the first is usually out of range. if '%d' not in date_format: period = 'month' date_obj = date_obj.replace( day=get_last_day_of_month(date_obj.year, date_obj.month)) if not ('%y' in date_format or '%Y' in date_format): today = datetime.today() date_obj = date_obj.replace(year=today.year) date_obj = apply_timezone_from_settings(date_obj, settings) return {'date_obj': date_obj, 'period': period} else: return {'date_obj': None, 'period': period}
def parse_with_formats(date_string, date_formats, settings): """ Parse with formats and return a dictionary with 'period' and 'obj_date'. :returns: :class:`datetime.datetime`, dict or None """ period = 'day' for date_format in date_formats: try: date_obj = datetime.strptime(date_string, date_format) except ValueError: continue else: if '%d' not in date_format: period = 'month' date_obj = set_correct_day_from_settings(date_obj, settings) if not ('%y' in date_format or '%Y' in date_format): today = datetime.today() date_obj = date_obj.replace(year=today.year) date_obj = apply_timezone_from_settings(date_obj, settings) return DateData(date_obj=date_obj, period=period) else: return DateData(date_obj=None, period=period)
def parse_with_formats(date_string, date_formats, settings): """ Parse with formats and return a dictionary with 'period' and 'obj_date'. :returns: :class:`datetime.datetime`, dict or None """ if isinstance(date_formats, six.string_types): warn(_DateLocaleParser.DATE_FORMATS_ERROR_MESSAGE, FutureWarning) date_formats = [date_formats] period = 'day' for date_format in date_formats: try: date_obj = datetime.strptime(date_string, date_format) except ValueError: continue else: if '%d' not in date_format: period = 'month' date_obj = set_correct_day_from_settings(date_obj, settings) if not ('%y' in date_format or '%Y' in date_format): today = datetime.today() date_obj = date_obj.replace(year=today.year) date_obj = apply_timezone_from_settings(date_obj, settings) return {'date_obj': date_obj, 'period': period} else: return {'date_obj': None, 'period': period}
def test_apply_timezone_from_settings_function(self, date, timezone, expected): result = apply_timezone_from_settings( date, settings.replace(**{ 'TO_TIMEZONE': timezone, 'TIMEZONE': 'UTC' })) self.assertEqual(expected, result)
def get_date_from_timestamp(date_string, settings): match = RE_SEARCH_TIMESTAMP.search(date_string) if match: seconds = int(match.group(1)) millis = int(match.group(2) or 0) micros = int(match.group(3) or 0) date_obj = datetime.fromtimestamp(seconds) date_obj = date_obj.replace(microsecond=millis * 1000 + micros) date_obj = apply_timezone_from_settings(date_obj, settings) return date_obj
def get_date_from_timestamp(date_string, settings): if RE_SEARCH_TIMESTAMP.search(date_string): date_obj = datetime.fromtimestamp(int(date_string[:10])) date_obj = apply_timezone_from_settings(date_obj, settings) return date_obj
def test_apply_timezone_from_settings_function_should_return_tz( self, date): result = apply_timezone_from_settings( date, settings.replace(**{'RETURN_AS_TIMEZONE_AWARE': True})) self.assertTrue(bool(result.tzinfo))
def get_date_from_timestamp(date_string, settings): if re.search(r'^\d{10}(?![^\d.])', date_string): date_obj = datetime.fromtimestamp(int(date_string[:10])) date_obj = apply_timezone_from_settings(date_obj, settings) return date_obj
def test_apply_timezone_from_settings_function_none_settings( self, date, expected): result = apply_timezone_from_settings(date, None) self.assertEqual(expected, result)
def test_apply_timezone_from_settings_function_should_return_tz(self, date): result = apply_timezone_from_settings(date, settings.replace(**{'RETURN_AS_TIMEZONE_AWARE': True})) self.assertTrue(bool(result.tzinfo))
def test_apply_timezone_from_settings_function_none_settings(self, date, expected): result = apply_timezone_from_settings(date, None) self.assertEqual(expected, result)
def test_apply_timezone_from_settings_function(self, date, timezone, expected): result = apply_timezone_from_settings(date, settings.replace(**{'TO_TIMEZONE': timezone, 'TIMEZONE': 'UTC'})) self.assertEqual(expected, result)