# -*- coding: utf-8 -*-fromdjango.utilsimporttimezoneasdjango_tzimportdatetimeimportpytzUTC=pytz.utcEPOCH_START=datetime.datetime(1970,1,1,0,0,0).replace(tzinfo=UTC)defutcfromtimestamp(timestamp):returndatetime.datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc)defutcfromtimestamp_as_local(timestamp):returnto_local_tz(datetime.datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc))defnow_timestamp():return(now()-EPOCH_START).total_seconds()defget_tz():returndjango_tz.get_current_timezone()defnow_local():returndjango_tz.now().astimezone(django_tz.get_current_timezone())defnow_utc():returndjango_tz.now().astimezone(django_tz.utc)defto_local_tz(*args):""" Аргументы datetime либо год, месяц, день и т.д для создания datetime """dt=args[0]iflen(args)==1andisinstance(args[0],datetime.datetime)elsedatetime.datetime(*args)ifdjango_tz.is_naive(dt):_dt=django_tz.make_aware(dt,django_tz.get_current_timezone())else:_dt=dtreturn_dt.astimezone(django_tz.get_current_timezone())defget_current_offset():""" Returns current UTC offset in seconds """tz=get_tz()returntz.utcoffset(django_tz.now()).total_seconds()
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.