Mercurial > libervia-backend
annotate sat/tools/common/date_utils.py @ 2774:95321f233387
plugin pubsub schema: code formatting minor update
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 12 Jan 2019 16:50:13 +0100 |
parents | 003b8b4b56a7 |
children | 7968c8b7b5e1 |
rev | line source |
---|---|
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """tools to help manipulating time and dates""" |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
23 from sat.core.i18n import _ |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import datetime |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
25 import dateutil |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
26 from dateutil import tz |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
27 from dateutil.relativedelta import relativedelta |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
28 from dateutil.utils import default_tzinfo |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from babel import dates |
2612
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
30 import calendar |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 import time |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
32 import re |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
33 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
34 RELATIVE_RE = re.compile(ur"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *" |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
35 ur"(?P<unit>(second|minute|hour|day|week|month|year))s?" |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
36 ur"(?P<ago> +ago)?", re.I) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
37 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
38 TZ_UTC = tz.tzutc() |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
39 TZ_LOCAL = tz.gettz() |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
42 def date_parse(value, default_tz=TZ_UTC): |
2612
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
43 """Parse a date and return corresponding unix timestamp |
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
44 |
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
45 @param value(unicode): date to parse, in any format supported by dateutil.parser |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
46 @param default_tz(datetime.tzinfo): default timezone |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
47 @return (int): timestamp |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
48 """ |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
49 dt = default_tzinfo(dateutil.parser.parse(unicode(value), dayfirst=True), default_tz) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
50 return calendar.timegm(dt.utctimetuple()) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
51 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
52 def date_parse_ext(value, default_tz=TZ_UTC): |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
53 """Extended date parse which accept relative date |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
54 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
55 @param value(unicode): date to parse, in any format supported by dateutil.parser |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
56 and with the hability to specify X days/weeks/months/years in the past or future. |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
57 Relative date are specified either with something like `[main_date] +1 week` |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
58 or with something like `3 days ago`, and it is case insensitive. [main_date] is |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
59 a date parsable by dateutil.parser, or empty to specify current date/time. |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
60 "now" can also be used to specify current date/time. |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
61 @param default_tz(datetime.tzinfo): same as for date_parse |
2612
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
62 @return (int): timestamp |
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
63 """ |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
64 m = RELATIVE_RE.match(value) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
65 if m is None: |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
66 return date_parse(value, default_tz=default_tz) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
67 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
68 if m.group(u"direction") and m.group(u"ago"): |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
69 raise ValueError( |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
70 _(u"You can't use a direction (+ or -) and \"ago\" at the same time")) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
71 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
72 if m.group(u"direction") == u'-' or m.group(u"ago"): |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
73 direction = -1 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
74 else: |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
75 direction = 1 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
76 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
77 date = m.group(u"date").strip().lower() |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
78 if not date or date == u"now": |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
79 dt = datetime.datetime.now(tz.tzutc()) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
80 else: |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
81 dt = default_tzinfo(dateutil.parser.parse(date, dayfirst=True)) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
82 |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
83 quantity = int(m.group(u"quantity")) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
84 key = m.group(u"unit").lower() + u"s" |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
85 delta_kw = {key: direction * quantity} |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
86 dt = dt + relativedelta(**delta_kw) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
87 return calendar.timegm(dt.utctimetuple()) |
2612
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
88 |
3e4e78de9cca
tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents:
2599
diff
changeset
|
89 |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
90 def date_fmt(timestamp, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short", |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
91 auto_new_fmt="relative", locale_str=C.DEFAULT_LOCALE, tz_info=TZ_UTC): |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 """format date according to locale |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
94 @param timestamp(basestring, float): unix time |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 @param fmt(str): one of: |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 - short: e.g. u'31/12/17' |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 - medium: e.g. u'Apr 1, 2007' |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 - long: e.g. u'April 1, 2007' |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 - full: e.g. u'Sunday, April 1, 2007' |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 - relative: format in relative time |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 e.g.: 3 hours |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 note that this format is not precise |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 - iso: ISO 8601 format |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 e.g.: u'2007-04-01T19:53:23Z' |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 - auto: use auto_old_fmt if date is older than auto_limit |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 else use auto_new_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 - auto_day: shorcut to set auto format with change on day |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 old format will be short, and new format will be time only |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 or a free value which is passed to babel.dates.format_datetime |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
110 (see http://babel.pocoo.org/en/latest/dates.html?highlight=pattern#pattern-syntax) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 @param date_only(bool): if True, only display date (not datetime) |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 @param auto_limit (int): limit in days before using auto_old_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 use 0 to have a limit at last midnight (day change) |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 @param auto_old_fmt(unicode): format to use when date is older than limit |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 @param auto_new_fmt(unicode): format to use when date is equal to or more recent |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 than limit |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
117 @param locale_str(unicode): locale to use (as understood by babel) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
118 @param tz_info(datetime.tzinfo): time zone to use |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 """ |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
121 timestamp = float(timestamp) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
122 if fmt == "auto_day": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
123 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
124 if fmt == "auto": |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 if auto_limit == 0: |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
126 now = datetime.datetime.now(tz_info) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
127 # we want to use given tz_info, so we don't use date() or today() |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
128 today = datetime.datetime(year=now.year, month=now.month, day=now.day, |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
129 tzinfo=now.tzinfo) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
130 today = calendar.timegm(today.utctimetuple()) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
131 if timestamp < today: |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 fmt = auto_old_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 else: |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 fmt = auto_new_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 else: |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
136 days_delta = (time.time() - timestamp) / 3600 |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 if days_delta > (auto_limit or 7): |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 fmt = auto_old_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 else: |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 fmt = auto_new_fmt |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
142 if fmt == "relative": |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
143 delta = timestamp - time.time() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
144 return dates.format_timedelta( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
145 delta, granularity="minute", add_direction=True, locale=locale_str |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
146 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
147 elif fmt in ("short", "long"): |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
148 if date_only: |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
149 dt = datetime.fromtimestamp(timestamp, tz_info) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
150 return dates.format_date(dt, format=fmt, locale=locale_str) |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
151 else: |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
152 return dates.format_datetime(timestamp, format=fmt, locale=locale_str, |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
153 tzinfo=tz_info) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
154 elif fmt == "iso": |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 if date_only: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
156 fmt = "yyyy-MM-dd" |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 else: |
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 fmt = "yyyy-MM-ddTHH:mm:ss'Z'" |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
159 return dates.format_datetime(timestamp, format=fmt) |
2599
5b26033c49a8
tools (common): moved date_fmt function from template filters to new date_utils module, so it can be used everywhere.
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 else: |
2703
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
161 return dates.format_datetime(timestamp, format=fmt, locale=locale_str, |
3ba53b1cd1e6
tools (common/date_utils): date_parse_ext + timezone handling:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
162 tzinfo=tz_info) |