1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-01 15:23:05 +00:00

add utc_datetime_to_isoformat_timestr func (#2387)

This commit is contained in:
lian 2018-09-20 15:09:52 +08:00 committed by Daniel Pan
parent d18dea8c0b
commit dddf7aee24
2 changed files with 24 additions and 5 deletions

View File

@ -15,7 +15,7 @@ from seahub.api2.authentication import TokenAuthentication
from seahub.api2.utils import api_error
from seahub.api2.endpoints.utils import generate_links_header_for_paginator
from seahub.utils import get_file_history, get_site_scheme_and_netloc
from seahub.utils.timeutils import datetime_to_isoformat_timestr, timestamp_to_isoformat_timestr
from seahub.utils.timeutils import utc_datetime_to_isoformat_timestr, timestamp_to_isoformat_timestr
from seahub.utils.file_revisions import get_file_revisions_within_limit
from seahub.views import check_folder_permission
from seahub.avatar.templatetags.avatar_tags import api_avatar_url
@ -36,7 +36,7 @@ def get_new_file_history_info(ent, avatar_size):
info['creator_name'] = email2nickname(creator_name)
info['creator_contact_email'] = email2contact_email(creator_name)
info['op_type'] = ent.op_type
info['ctime'] = datetime_to_isoformat_timestr(ent.timestamp)
info['ctime'] = utc_datetime_to_isoformat_timestr(ent.timestamp)
info['commit_id'] = ent.commit_id
info['size'] = ent.size
info['rev_file_id'] = ent.file_id

View File

@ -5,9 +5,13 @@ import datetime
from django.conf import settings
from django.utils import six
from django.utils import timezone
from django.utils.timezone import get_current_timezone
logger = logging.getLogger(__name__)
# https://docs.djangoproject.com/en/1.11/ref/utils/#django.utils.timezone.get_current_timezone
current_timezone = get_current_timezone()
def dt(value):
"""Convert 32/64 bits timestamp to datetime object.
"""
@ -38,7 +42,6 @@ def utc_to_local(dt):
local = timezone.make_naive(utc, tz)
return local
pytz_obj = pytz.timezone(settings.TIME_ZONE)
def timestamp_to_isoformat_timestr(timestamp):
try:
min_ts = -(1 << 31)
@ -49,16 +52,32 @@ def timestamp_to_isoformat_timestr(timestamp):
dt_obj = datetime.datetime.fromtimestamp(timestamp/1000000)
dt_obj = dt_obj.replace(microsecond=0)
isoformat_timestr = pytz_obj.localize(dt_obj).isoformat()
isoformat_timestr = current_timezone.localize(dt_obj).isoformat()
return isoformat_timestr
except Exception as e:
logger.error(e)
return ''
# https://pypi.org/project/pytz/
def datetime_to_isoformat_timestr(datetime):
try:
# This library only supports two ways of building a localized time.
# The first is to use the localize() method provided by the pytz library.
# This is used to localize a naive datetime (datetime with no timezone information):
datetime = datetime.replace(microsecond=0)
isoformat_timestr = pytz_obj.localize(datetime).isoformat()
isoformat_timestr = current_timezone.localize(datetime).isoformat()
return isoformat_timestr
except Exception as e:
logger.error(e)
return ''
def utc_datetime_to_isoformat_timestr(utc_datetime):
try:
# The second way of building a localized time is by converting an existing
# localized time using the standard astimezone() method:
utc_datetime = utc_datetime.replace(microsecond=0)
utc_datetime = pytz.utc.localize(utc_datetime)
isoformat_timestr = utc_datetime.astimezone(current_timezone).isoformat()
return isoformat_timestr
except Exception as e:
logger.error(e)