comparison sat/tools/common/date_utils.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 3e4e78de9cca
children 3ba53b1cd1e6
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
34 @return (int): timestamp 34 @return (int): timestamp
35 """ 35 """
36 return calendar.timegm(dateutil_parser.parse(unicode(value)).utctimetuple()) 36 return calendar.timegm(dateutil_parser.parse(unicode(value)).utctimetuple())
37 37
38 38
39 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): 39 def date_fmt(
40 timestamp,
41 fmt="short",
42 date_only=False,
43 auto_limit=7,
44 auto_old_fmt="short",
45 auto_new_fmt="relative",
46 locale_str=C.DEFAULT_LOCALE,
47 ):
40 """format date according to locale 48 """format date according to locale
41 49
42 @param timestamp(basestring, int): unix time 50 @param timestamp(basestring, int): unix time
43 @param fmt(str): one of: 51 @param fmt(str): one of:
44 - short: e.g. u'31/12/17' 52 - short: e.g. u'31/12/17'
61 @param auto_old_fmt(unicode): format to use when date is older than limit 69 @param auto_old_fmt(unicode): format to use when date is older than limit
62 @param auto_new_fmt(unicode): format to use when date is equal to or more recent 70 @param auto_new_fmt(unicode): format to use when date is equal to or more recent
63 than limit 71 than limit
64 72
65 """ 73 """
66 if fmt == 'auto_day': 74 if fmt == "auto_day":
67 fmt, auto_limit, auto_old_fmt, auto_new_fmt = 'auto', 0, 'short', 'HH:mm' 75 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm"
68 if fmt == 'auto': 76 if fmt == "auto":
69 if auto_limit == 0: 77 if auto_limit == 0:
70 today = time.mktime(datetime.date.today().timetuple()) 78 today = time.mktime(datetime.date.today().timetuple())
71 if int(timestamp) < today: 79 if int(timestamp) < today:
72 fmt = auto_old_fmt 80 fmt = auto_old_fmt
73 else: 81 else:
77 if days_delta > (auto_limit or 7): 85 if days_delta > (auto_limit or 7):
78 fmt = auto_old_fmt 86 fmt = auto_old_fmt
79 else: 87 else:
80 fmt = auto_new_fmt 88 fmt = auto_new_fmt
81 89
82 if fmt == 'relative': 90 if fmt == "relative":
83 delta = int(timestamp) - time.time() 91 delta = int(timestamp) - time.time()
84 return dates.format_timedelta(delta, granularity="minute", add_direction=True, locale=locale_str) 92 return dates.format_timedelta(
85 elif fmt in ('short', 'long'): 93 delta, granularity="minute", add_direction=True, locale=locale_str
94 )
95 elif fmt in ("short", "long"):
86 formatter = dates.format_date if date_only else dates.format_datetime 96 formatter = dates.format_date if date_only else dates.format_datetime
87 return formatter(int(timestamp), format=fmt, locale=locale_str) 97 return formatter(int(timestamp), format=fmt, locale=locale_str)
88 elif fmt == 'iso': 98 elif fmt == "iso":
89 if date_only: 99 if date_only:
90 fmt = 'yyyy-MM-dd' 100 fmt = "yyyy-MM-dd"
91 else: 101 else:
92 fmt = "yyyy-MM-ddTHH:mm:ss'Z'" 102 fmt = "yyyy-MM-ddTHH:mm:ss'Z'"
93 return dates.format_datetime(int(timestamp), format=fmt) 103 return dates.format_datetime(int(timestamp), format=fmt)
94 else: 104 else:
95 return dates.format_datetime(int(timestamp), format=fmt, locale=locale_str) 105 return dates.format_datetime(int(timestamp), format=fmt, locale=locale_str)