Mercurial > libervia-backend
comparison sat/tools/common/date_utils.py @ 2599:5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Jun 2018 12:04:06 +0200 |
parents | |
children | 3e4e78de9cca |
comparison
equal
deleted
inserted
replaced
2598:0b6adc2672d9 | 2599:5b26033c49a8 |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """tools to help manipulating time and dates""" | |
21 | |
22 from sat.core.constants import Const as C | |
23 import datetime | |
24 from babel import dates | |
25 import time | |
26 | |
27 | |
28 def date_fmt(timestamp, fmt='short', date_only=False, auto_limit=7, auto_old_fmt='short', auto_new_fmt='relative', locale_str=C.DEFAULT_LOCALE): | |
29 """format date according to locale | |
30 | |
31 @param timestamp(basestring, int): unix time | |
32 @param fmt(str): one of: | |
33 - short: e.g. u'31/12/17' | |
34 - medium: e.g. u'Apr 1, 2007' | |
35 - long: e.g. u'April 1, 2007' | |
36 - full: e.g. u'Sunday, April 1, 2007' | |
37 - relative: format in relative time | |
38 e.g.: 3 hours | |
39 note that this format is not precise | |
40 - iso: ISO 8601 format | |
41 e.g.: u'2007-04-01T19:53:23Z' | |
42 - auto: use auto_old_fmt if date is older than auto_limit | |
43 else use auto_new_fmt | |
44 - auto_day: shorcut to set auto format with change on day | |
45 old format will be short, and new format will be time only | |
46 or a free value which is passed to babel.dates.format_datetime | |
47 @param date_only(bool): if True, only display date (not datetime) | |
48 @param auto_limit (int): limit in days before using auto_old_fmt | |
49 use 0 to have a limit at last midnight (day change) | |
50 @param auto_old_fmt(unicode): format to use when date is older than limit | |
51 @param auto_new_fmt(unicode): format to use when date is equal to or more recent | |
52 than limit | |
53 | |
54 """ | |
55 if fmt == 'auto_day': | |
56 fmt, auto_limit, auto_old_fmt, auto_new_fmt = 'auto', 0, 'short', 'HH:mm' | |
57 if fmt == 'auto': | |
58 if auto_limit == 0: | |
59 today = time.mktime(datetime.date.today().timetuple()) | |
60 if int(timestamp) < today: | |
61 fmt = auto_old_fmt | |
62 else: | |
63 fmt = auto_new_fmt | |
64 else: | |
65 days_delta = (time.time() - int(timestamp)) / 3600 | |
66 if days_delta > (auto_limit or 7): | |
67 fmt = auto_old_fmt | |
68 else: | |
69 fmt = auto_new_fmt | |
70 | |
71 if fmt == 'relative': | |
72 delta = int(timestamp) - time.time() | |
73 return dates.format_timedelta(delta, granularity="minute", add_direction=True, locale=locale_str) | |
74 elif fmt in ('short', 'long'): | |
75 formatter = dates.format_date if date_only else dates.format_datetime | |
76 return formatter(int(timestamp), format=fmt, locale=locale_str) | |
77 elif fmt == 'iso': | |
78 if date_only: | |
79 fmt = 'yyyy-MM-dd' | |
80 else: | |
81 fmt = "yyyy-MM-ddTHH:mm:ss'Z'" | |
82 return dates.format_datetime(int(timestamp), format=fmt) | |
83 else: | |
84 return dates.format_datetime(int(timestamp), format=fmt, locale=locale_str) |