annotate sat/tools/common/date_utils.py @ 2656:8cacf7929f3c

tools (common/data_format): added serialise and deserialise methods (using json for now)
author Goffi <goffi@goffi.org>
date Sat, 11 Aug 2018 18:24:55 +0200
parents 56f94936df1e
children 3ba53b1cd1e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
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
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
23 import datetime
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
24 from dateutil import parser as dateutil_parser
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
25 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
26 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
27 import 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
28
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
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 def date_parse(value):
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
31 """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
32
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
33 @param value(unicode): date to parse, in any format supported by dateutil.parser
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
34 @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
35 """
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
36 return calendar.timegm(dateutil_parser.parse(unicode(value)).utctimetuple())
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
37
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
38
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
39 def date_fmt(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
40 timestamp,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
41 fmt="short",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
42 date_only=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
43 auto_limit=7,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
44 auto_old_fmt="short",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
45 auto_new_fmt="relative",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
46 locale_str=C.DEFAULT_LOCALE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
47 ):
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
48 """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
49
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
50 @param timestamp(basestring, int): unix 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
51 @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
52 - 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
53 - 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
54 - 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
55 - 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
56 - 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
57 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
58 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
59 - 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
60 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
61 - 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
62 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
63 - 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
64 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
65 or a free value which is passed to babel.dates.format_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
66 @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
67 @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
68 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
69 @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
70 @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
71 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
72
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
73 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
74 if fmt == "auto_day":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
75 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
76 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
77 if auto_limit == 0:
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
78 today = time.mktime(datetime.date.today().timetuple())
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
79 if int(timestamp) < today:
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
80 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
81 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
82 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
83 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
84 days_delta = (time.time() - int(timestamp)) / 3600
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
85 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
86 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
87 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
88 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
89
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
90 if fmt == "relative":
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
91 delta = int(timestamp) - time.time()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
92 return dates.format_timedelta(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
93 delta, granularity="minute", add_direction=True, locale=locale_str
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
94 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
95 elif fmt in ("short", "long"):
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
96 formatter = dates.format_date if date_only else dates.format_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
97 return formatter(int(timestamp), format=fmt, locale=locale_str)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
98 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
99 if date_only:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
100 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
101 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
102 fmt = "yyyy-MM-ddTHH:mm:ss'Z'"
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 return dates.format_datetime(int(timestamp), format=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
104 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
105 return dates.format_datetime(int(timestamp), format=fmt, locale=locale_str)