Mercurial > libervia-backend
comparison sat/tools/common/date_utils.py @ 4028:883db2790b11
tools (common/date_utils): let use a timezone name as str in `date_fmt`
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Mar 2023 16:51:09 +0200 |
parents | 92482cc80d0b |
children | 432f7e422a27 |
comparison
equal
deleted
inserted
replaced
4027:26c3e1bc7fb7 | 4028:883db2790b11 |
---|---|
136 delta_kw = {f"{unit}s": direction * quantity} | 136 delta_kw = {f"{unit}s": direction * quantity} |
137 dt = dt + relativedelta(**delta_kw) | 137 dt = dt + relativedelta(**delta_kw) |
138 return calendar.timegm(dt.utctimetuple()) | 138 return calendar.timegm(dt.utctimetuple()) |
139 | 139 |
140 | 140 |
141 def date_fmt(timestamp, fmt="short", date_only=False, auto_limit=7, auto_old_fmt="short", | 141 def date_fmt( |
142 auto_new_fmt="relative", locale_str=C.DEFAULT_LOCALE, tz_info=TZ_UTC): | 142 timestamp: Union[float, int, str], |
143 """format date according to locale | 143 fmt: str = "short", |
144 | 144 date_only: bool = False, |
145 @param timestamp(basestring, float): unix time | 145 auto_limit: int = 7, |
146 @param fmt(str): one of: | 146 auto_old_fmt: str = "short", |
147 auto_new_fmt: str = "relative", | |
148 locale_str: str = C.DEFAULT_LOCALE, | |
149 tz_info: Union[datetime.tzinfo, str] = TZ_UTC | |
150 ) -> str: | |
151 """Format date according to locale | |
152 | |
153 @param timestamp: unix time | |
154 @param fmt: one of: | |
147 - short: e.g. u'31/12/17' | 155 - short: e.g. u'31/12/17' |
148 - medium: e.g. u'Apr 1, 2007' | 156 - medium: e.g. u'Apr 1, 2007' |
149 - long: e.g. u'April 1, 2007' | 157 - long: e.g. u'April 1, 2007' |
150 - full: e.g. u'Sunday, April 1, 2007' | 158 - full: e.g. u'Sunday, April 1, 2007' |
151 - relative: format in relative time | 159 - relative: format in relative time |
157 else use auto_new_fmt | 165 else use auto_new_fmt |
158 - auto_day: shorcut to set auto format with change on day | 166 - auto_day: shorcut to set auto format with change on day |
159 old format will be short, and new format will be time only | 167 old format will be short, and new format will be time only |
160 or a free value which is passed to babel.dates.format_datetime | 168 or a free value which is passed to babel.dates.format_datetime |
161 (see http://babel.pocoo.org/en/latest/dates.html?highlight=pattern#pattern-syntax) | 169 (see http://babel.pocoo.org/en/latest/dates.html?highlight=pattern#pattern-syntax) |
162 @param date_only(bool): if True, only display date (not datetime) | 170 @param date_only: if True, only display date (not datetime) |
163 @param auto_limit (int): limit in days before using auto_old_fmt | 171 @param auto_limit: limit in days before using auto_old_fmt |
164 use 0 to have a limit at last midnight (day change) | 172 use 0 to have a limit at last midnight (day change) |
165 @param auto_old_fmt(unicode): format to use when date is older than limit | 173 @param auto_old_fmt: format to use when date is older than limit |
166 @param auto_new_fmt(unicode): format to use when date is equal to or more recent | 174 @param auto_new_fmt: format to use when date is equal to or more recent |
167 than limit | 175 than limit |
168 @param locale_str(unicode): locale to use (as understood by babel) | 176 @param locale_str: locale to use (as understood by babel) |
169 @param tz_info(datetime.tzinfo): time zone to use | 177 @param tz_info: time zone to use |
170 | 178 |
171 """ | 179 """ |
172 timestamp = float(timestamp) | 180 timestamp = float(timestamp) |
181 if isinstance(tz_info, str): | |
182 tz_info = tz.gettz(tz_info) | |
173 if fmt == "auto_day": | 183 if fmt == "auto_day": |
174 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" | 184 fmt, auto_limit, auto_old_fmt, auto_new_fmt = "auto", 0, "short", "HH:mm" |
175 if fmt == "auto": | 185 if fmt == "auto": |
176 if auto_limit == 0: | 186 if auto_limit == 0: |
177 now = datetime.datetime.now(tz_info) | 187 now = datetime.datetime.now(tz_info) |