# # This file is part of pretix (Community Edition). # # Copyright (C) 2014-2020 Raphael Michel and contributors # Copyright (C) 2020-today pretix GmbH and contributors # # This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General # Public License as published by the Free Software Foundation in version 3 of the License. # # ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are # applicable granting you additional permissions and placing additional restrictions on your usage of this software. # Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive # this file, see . # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more # details. # # You should have received a copy of the GNU Affero General Public License along with this program. If not, see # . # import gettext as gettext_module import json import os import re from datetime import datetime from functools import lru_cache from typing import Optional from django.apps import apps from django.conf import settings from django.utils import translation from django.utils.formats import get_format from django.utils.translation import to_locale date_conversion_to_moment = { '%a': 'ddd', '%A': 'dddd', '%w': 'd', '%d': 'DD', '%b': 'MMM', '%B': 'MMMM', '%m': 'MM', '%y': 'YY', '%Y': 'YYYY', '%H': 'HH', '%I': 'hh', '%p': 'a', '%M': 'mm', '%S': 'ss', '%f': 'SSSSSS', '%z': 'ZZ', '%Z': 'zz', '%j': 'DDDD', '%U': 'ww', # fuzzy translation '%W': 'WW', '%c': '', '%x': '', '%X': '' } out_date_conversion_to_moment = { 'a': 'a', 'A': 'A', 'b': 'MMM', 'c': 'YYYY-MM-DDTHH:mm:ss.SSSSSSZ', 'd': 'DD', 'e': 'zz', 'E': 'MMMM', 'f': 'h:mm', 'F': 'MMMM', 'g': 'h', 'G': 'H', 'h': 'hh', 'H': 'HH', 'i': 'mm', 'I': '', 'j': 'D', 'l': 'dddd', 'L': '', 'm': 'MM', 'M': 'MMM', 'n': 'M', 'N': 'MMM', # fuzzy 'o': 'GGGG', 'O': 'ZZ', 'P': 'h:mm a', 'r': 'ddd, D MMM YYYY HH:mm:ss Z', 's': 'ss', 'S': 'Do', # fuzzy 't': '', 'T': 'z', 'u': 'SSSSSS', 'U': 'X', 'w': 'd', 'W': 'W', 'y': 'YY', 'Y': 'YYYY', 'z': 'DDD', 'Z': '' } moment_locales = { 'af', 'az', 'bs', 'de-at', 'en-gb', 'et', 'fr-ch', 'hi', 'it', 'ko', 'me', 'ms-my', 'pa-in', 'se', 'sr', 'th', 'tzm-latn', 'zh-hk', 'ar', 'be', 'ca', 'de', 'en-ie', 'eu', 'fr', 'hr', 'ja', 'ky', 'mi', 'my', 'pl', 'si', 'ss', 'tlh', 'uk', 'zh-tw', 'ar-ly', 'bg', 'cs', 'dv', 'en-nz', 'fa', 'fy', 'hu', 'jv', 'lb', 'mk', 'nb', 'pt-br', 'sk', 'sv', 'tl-ph', 'uz', 'ar-ma', 'bn', 'cv', 'el', 'eo', 'fi', 'gd', 'hy-am', 'ka', 'lo', 'ml', 'ne', 'pt', 'sl', 'sw', 'tr', 'vi', 'ar-sa', 'bo', 'cy', 'en-au', 'es-do', 'fo', 'gl', 'id', 'kk', 'lt', 'mr', 'nl', 'ro', 'sq', 'ta', 'tzl', 'x-pseudo', 'ar-tn', 'br', 'da', 'en-ca', 'es', 'fr-ca', 'he', 'is', 'km', 'lv', 'ms', 'nn', 'ru', 'sr-cyrl', 'te', 'tzm', 'zh-cn', } toJavascript_re = re.compile(r'(? Optional[datetime]: """Parses a date according to the localized date input formats. Returns None if invalid.""" dt = None for f in get_format('DATE_INPUT_FORMATS'): try: dt = datetime.strptime(date_str, f) break except (ValueError, TypeError): continue return dt