def timesince(uploadtime): """ Возвращает строку, описывающую, как долго прошло с…
- User
def time_since(upload_time): """ Возвращает строку, описывающую, как долго прошло с момента загрузки, с учетом поправки на +3 часа. """ # Добавляем 3 часа к upload_time adjusted_upload_time = upload_time + timedelta(hours=3) now = datetime.utcnow() diff = now - adjusted_upload_time seconds = diff.total_seconds()
codeif seconds < 0: return "только что" # В случае, если время загрузки в будущем if seconds < 30: return "только что" elif seconds < 60: sec = math.floor(seconds) return f"{sec} {decline(sec, 'секунда', 'секунды', 'секунд')}" elif seconds < 3600: minutes = math.floor(seconds / 60) return f"{minutes} {decline(minutes, 'минута', 'минуты', 'минут')}" elif seconds < 86400: hours = math.floor(seconds / 3600) return f"{hours} {decline(hours, 'час', 'часа', 'часов')}" elif seconds < 604800: days = math.floor(seconds / 86400) return f"{days} {decline(days, 'день', 'дня', 'дней')}" elif seconds < 2592000: weeks = math.floor(seconds / 604800) return f"{weeks} {decline(weeks, 'неделя', 'недели', 'недель')}" elif seconds < 31536000: months = math.floor(seconds / 2592000) return f"{months} {decline(months, 'месяц', 'месяца', 'месяцев')}" elif seconds < 3153600000: years = math.floor(seconds / 31536000) return f"{years} {decline(years, 'год', 'года', 'лет')}" elif seconds < 3153600000000: centuries = math.floor(seconds / 3153600000) return f"{centuries} {decline(centuries, 'век', 'века', 'веков')}" else: millennia = math.floor(seconds / 3153600000000) return f"{millennia} {decline(millennia, 'тысячелетие', 'тысячелетия', 'тысячелетий')}"@app.template_filter('timesince') def timesince_filter(upload_time): # Добавляем 3 часа к upload_time
- Assistant
- User
i don't see where is adjusted_upload_time connected to the filter
- Assistant
The function
time_sincealready takes care of adjusting theupload_timeby adding 3 hours, which is then used in the time difference calculation. When thetimesince_filteris applied, it calls thetime_sincefunction where this adjustment takes place.Let's clarify how this works:
-
Time Adjustment in
time_since: Thetime_sincefunction directly processesupload_time, adding 3 hours to it viatimedelta(hours=3). -
Filter
timesince_filter: The filtertimesince_filterpasses its argument (upload_time) to thetime_sincefunction. Here's how the adjustment is implicitly included through function calls:python
-