def get_appversion(request): # only check once in 24 hours the current version and if there's a new version on the Github release page: date_check = getter_settings_cookie('date_check', request) # because windows use some unknown locale, we set standard locale. Else, datetime is not working locale.setlocale(locale.LC_ALL, '') saved = locale._setlocale(locale.LC_TIME) locale.setlocale(locale.LC_TIME, 'C') now = datetime.now().timestamp() if not date_check or date_check - now > 86400: # 24*3600 # Time to check... # the app version is taken from the file appversion.txt which contains the date of the release # (this release date txt file is updated when building the Linux version) appversiontxt_path = os.path.join(settings.BASE_DIR, 'lwt', 'appversion.txt') with open(appversiontxt_path, 'r', encoding="utf8") as appversion_f: current_date = appversion_f.read().strip() # get the date of the latest release on GIthub: try: resp = requests.get( 'https://api.github.com/repos/gustavklopp/lingl/releases') if resp.status_code == 200: releases = resp.json()[:3] for release in releases: if release['name'].startswith('linux'): release_date_str = release['name'][ -10:] # the date is in the end of the string 'linux LingLibre 2016.06.20' release_date_obj = datetime.strptime( release_date_str, '%Y.%m.%d') # '2021.06.20' current_date_obj = datetime.strptime( current_date, '%Y.%m.%d') is_outdated = current_date_obj < release_date_obj break # and update the cookies setter_settings_cookie('date_check', now, request) setter_settings_cookie('current_date', current_date, request) else: # error accessing Github release page return { 'current_date': _('<Error accessing Github Server>'), 'is_outdated': True } except: # error accessing Github release page return { 'current_date': _('<Error accessing Github Server>'), 'is_outdated': True } else: current_date = getter_settings_cookie('current_date', request) is_outdated = False # we assume it's not outdated even if we don't check... # set back the normal locale locale.setlocale(locale.LC_TIME, saved) return {'current_date': current_date, 'is_outdated': is_outdated}
def getlocale(category=_locale.LC_CTYPE): localename = _locale._setlocale(category) if category == _locale.LC_ALL and ';' in localename: raise TypeError('category LC_ALL is not supported') locale = _locale._parse_localename(localename) code_page = locale[1] if code_page.isdigit() and int(code_page) in Locale.CODE_PAGES: code_page = Locale.CODE_PAGES[int(code_page)].replace('windows-', 'cp') return locale[0], code_page
from datetime import datetime from datetime import date, timedelta import locale locale._setlocale(locale.LC_ALL, "rus_rus") dt_now = datetime.now() a = dt_now.strftime('%d.%m.%Y %H:%m') print(a) a = dt_now.strftime('%A %d %B %Y') print(a) a = dt_now.strftime('%Y-%m-%d') print("Сегодня: ", a) delta = timedelta(days=1) a = dt_now - delta print("Вчера :", a) delta = timedelta(days=30) a = dt_now - delta print("Месяц назад :", a) date_string = '01/01/17 12:10:03.234567' date_dt = datetime.strptime(date_string, '%m/%d/%y %H:%M:%S.%f') print("Преобразовали строку в дату: ", date_dt) b = [] k = dt_now.strftime('%Y.%m.%d') for i in range(6): delta = timedelta(days=1) c = k - delta b.append(c) print(b)
def try_locale(category, locale): try: return _locale._setlocale(category, locale) except _locale.Error: return False